1
//! The Help text: the keys of the focused chain, then the `:` commands.
2

            
3
use super::scopes::{SCOPES, chain};
4
use super::shown::{Shown, chord_text, here};
5
use crate::app::App;
6
use crate::event::Intent;
7
use crate::palette;
8

            
9
const KEYS_WIDTH: usize = 16;
10
const NAME_WIDTH: usize = 26;
11

            
12
/// One row per action of a scope, its chords gathered.
13
218
fn rows<'a>(entries: impl Iterator<Item = &'a Shown<'a>>) -> Vec<(Intent, Vec<String>)> {
14
218
    let mut rows: Vec<(Intent, Vec<String>)> = Vec::new();
15
218
    for entry in entries {
16
2219
        let keys = chord_text(entry.chords);
17
11622
        match rows.iter_mut().find(|(intent, _)| *intent == entry.intent) {
18
309
            Some((_, all)) => all.push(keys),
19
1910
            None => rows.push((entry.intent, vec![keys])),
20
        }
21
    }
22
218
    rows
23
218
}
24

            
25
/// The action name as a keymap file writes it: `select-tab accounts`.
26
1910
fn action_name(intent: Intent) -> String {
27
1910
    match intent.name() {
28
764
        Some((name, Some(arg))) => format!("{name} {arg}"),
29
1146
        Some((name, None)) => name.to_string(),
30
        None => String::new(),
31
    }
32
1910
}
33

            
34
/// The innermost scope of the context `app` is in: what a Help window is about.
35
#[must_use]
36
35
pub fn help_topic(app: &App) -> &'static str {
37
35
    chain(app).innermost
38
35
}
39

            
40
/// The lines of Help for the context `app` is in now.
41
#[must_use]
42
61
pub fn help_lines(app: &App) -> Vec<String> {
43
61
    let names = chain(app).names;
44
61
    let view = here(app);
45
61
    let mut lines = vec![
46
61
        format!("Keys here ({})", app.keys.indicator()),
47
61
        "Rebind them in the tui-keymap-overrides config key.".to_string(),
48
    ];
49
61
    for scope in names {
50
8303
        let rows = rows(view.shown.iter().filter(|entry| entry.scope == scope));
51
218
        let title = SCOPES
52
218
            .iter()
53
1594
            .find(|known| known.name == scope)
54
218
            .map(|known| known.title);
55
218
        let Some(title) = title.filter(|_| !rows.is_empty()) else {
56
3
            continue;
57
        };
58
215
        lines.push(String::new());
59
215
        lines.push(title.to_string());
60
1910
        lines.extend(rows.into_iter().map(|(intent, keys)| {
61
1910
            let text = intent.description().unwrap_or_default();
62
1910
            let (keys, name) = (keys.join(", "), action_name(intent));
63
1910
            format!("  {keys:<KEYS_WIDTH$}{name:<NAME_WIDTH$}{text}")
64
1910
        }));
65
    }
66
61
    lines.push(String::new());
67
61
    lines.push(match view.key(Intent::OpenCommandLine) {
68
44
        Some(key) => format!("Commands, after {key}"),
69
17
        None => "Commands".to_string(),
70
    });
71
61
    lines.extend(palette::command_lines());
72
61
    lines
73
61
}