Lines
97.83 %
Functions
50 %
Branches
100 %
//! The Help text: the keys of the focused chain, then the `:` commands.
use super::scopes::{SCOPES, chain};
use super::shown::{Shown, chord_text, here};
use crate::app::App;
use crate::event::Intent;
use crate::palette;
const KEYS_WIDTH: usize = 16;
const NAME_WIDTH: usize = 26;
/// One row per action of a scope, its chords gathered.
fn rows<'a>(entries: impl Iterator<Item = &'a Shown<'a>>) -> Vec<(Intent, Vec<String>)> {
let mut rows: Vec<(Intent, Vec<String>)> = Vec::new();
for entry in entries {
let keys = chord_text(entry.chords);
match rows.iter_mut().find(|(intent, _)| *intent == entry.intent) {
Some((_, all)) => all.push(keys),
None => rows.push((entry.intent, vec![keys])),
}
rows
/// The action name as a keymap file writes it: `select-tab accounts`.
fn action_name(intent: Intent) -> String {
match intent.name() {
Some((name, Some(arg))) => format!("{name} {arg}"),
Some((name, None)) => name.to_string(),
None => String::new(),
/// The innermost scope of the context `app` is in: what a Help window is about.
#[must_use]
pub fn help_topic(app: &App) -> &'static str {
chain(app).innermost
/// The lines of Help for the context `app` is in now.
pub fn help_lines(app: &App) -> Vec<String> {
let names = chain(app).names;
let view = here(app);
let mut lines = vec![
format!("Keys here ({})", app.keys.indicator()),
"Rebind them in the tui-keymap-overrides config key.".to_string(),
];
for scope in names {
let rows = rows(view.shown.iter().filter(|entry| entry.scope == scope));
let title = SCOPES
.iter()
.find(|known| known.name == scope)
.map(|known| known.title);
let Some(title) = title.filter(|_| !rows.is_empty()) else {
continue;
};
lines.push(String::new());
lines.push(title.to_string());
lines.extend(rows.into_iter().map(|(intent, keys)| {
let text = intent.description().unwrap_or_default();
let (keys, name) = (keys.join(", "), action_name(intent));
format!(" {keys:<KEYS_WIDTH$}{name:<NAME_WIDTH$}{text}")
}));
lines.push(match view.key(Intent::OpenCommandLine) {
Some(key) => format!("Commands, after {key}"),
None => "Commands".to_string(),
});
lines.extend(palette::command_lines());
lines