1
//! The status-bar hints of the focused chain, and how they follow the keymap.
2

            
3
use super::shown::{KeyView, in_modal};
4
use crate::app::App;
5
use crate::event::Intent;
6
use crate::form::Form;
7
use crate::modal::{Csv, Draft, Modal};
8
use crate::test_support::hint_pairs;
9
use crate::view::Tab;
10

            
11
29
fn labels(app: &App) -> Vec<String> {
12
29
    hint_pairs(app)
13
29
        .into_iter()
14
189
        .map(|(key, label)| format!("{key} {label}"))
15
        .collect()
16
29
}
17

            
18
27
fn has(app: &App, hint: &str) -> bool {
19
139
    labels(app).iter().any(|shown| shown == hint)
20
27
}
21

            
22
4
fn app_with(overrides: &str) -> App {
23
4
    let mut app = crate::test_support::app();
24
4
    app.keys = match app.keys.overridden(overrides) {
25
4
        Ok(keys) => keys,
26
        Err(err) => panic!("the override parses: {err}"),
27
    };
28
4
    app
29
4
}
30

            
31
2
fn csv() -> Modal {
32
2
    Modal::Csv(Csv::new(String::new(), String::new()))
33
2
}
34

            
35
1
fn draft() -> Modal {
36
1
    Modal::Draft(Draft::new(Vec::new()))
37
1
}
38

            
39
#[test]
40
1
fn cua_shows_its_menu_help_and_quit_keys_first() {
41
1
    let mut app = crate::test_support::app();
42
1
    app.switch_tab(Tab::Transactions);
43
1
    let shown = labels(&app);
44
1
    assert_eq!(shown[..3], ["F10 Menu", "F1 Help", "M-x Quit"], "{shown:?}");
45
1
    assert!(!has(&app, "q Quit"), "{shown:?}");
46
1
}
47

            
48
#[test]
49
1
fn emacs_shows_its_quit_help_and_command_keys() {
50
1
    let app = crate::test_support::emacs_app();
51
1
    for hint in ["q Close/Quit", "? Help", ": Cmd"] {
52
3
        assert!(has(&app, hint), "{hint}: {:?}", labels(&app));
53
    }
54
1
    assert!(!has(&app, "F1 Help"), "{:?}", labels(&app));
55
1
}
56

            
57
#[test]
58
1
fn the_chain_decides_which_hints_show() {
59
1
    let mut app = crate::test_support::app();
60
1
    app.switch_tab(Tab::Transactions);
61
1
    assert!(has(&app, "f Filter"), "{:?}", labels(&app));
62
1
    app.switch_tab(Tab::Accounts);
63
1
    assert!(!has(&app, "f Filter"), "{:?}", labels(&app));
64
1
    crate::test_support::open_draft(&mut app);
65
1
    assert!(has(&app, "Esc Close"), "{:?}", labels(&app));
66
1
    assert!(!has(&app, "c New"), "{:?}", labels(&app));
67
1
}
68

            
69
#[test]
70
1
fn only_keys_that_act_are_shown() {
71
1
    let mut app = crate::test_support::app();
72
1
    app.switch_tab(Tab::Accounts);
73
1
    assert!(
74
1
        has(&app, "c New") && !has(&app, "d Delete"),
75
        "{:?}",
76
        labels(&app)
77
    );
78
1
    app.switch_tab(Tab::Transactions);
79
1
    assert!(
80
1
        has(&app, "d Delete") && has(&app, "c New"),
81
        "{:?}",
82
        labels(&app)
83
    );
84
1
    app.switch_tab(Tab::Commodities);
85
1
    assert!(
86
1
        has(&app, "c New") && !has(&app, "d Delete"),
87
        "{:?}",
88
        labels(&app)
89
    );
90
1
    app.overlays.push(Modal::Form(Form::config_set()));
91
1
    assert!(!has(&app, "M-x Quit"), "{:?}", labels(&app));
92
1
    assert!(has(&app, "Esc Close"), "{:?}", labels(&app));
93
1
}
94

            
95
#[test]
96
1
fn a_rebound_key_keeps_its_hint() {
97
1
    let app = app_with("global: F1 = none; global: F2 = open-help");
98
1
    assert!(has(&app, "F2 Help"), "{:?}", labels(&app));
99
1
    assert!(!has(&app, "F1 Help"), "{:?}", labels(&app));
100
1
}
101

            
102
#[test]
103
1
fn an_override_can_change_the_hint_list() {
104
1
    let app = app_with("global: hint = quit");
105
1
    assert!(has(&app, "M-x Quit"), "{:?}", labels(&app));
106
1
    assert!(!has(&app, "F1 Help"), "{:?}", labels(&app));
107
1
}
108

            
109
#[test]
110
1
fn an_action_hinted_by_an_inner_scope_is_not_hinted_again() {
111
1
    let mut app = app_with("modal-csv: F5 = close; modal-csv: hint = close");
112
1
    app.overlays.push(csv());
113
1
    assert!(has(&app, "F5 Close"), "{:?}", labels(&app));
114
1
    assert!(!has(&app, "Esc Close"), "{:?}", labels(&app));
115
1
}
116

            
117
#[test]
118
1
fn an_inner_binding_of_a_chord_hides_the_outer_one() {
119
1
    let app = app_with("modal-csv: q = copy-csv");
120
1
    let view = in_modal(&app, &csv());
121
1
    assert_eq!(view.all(Intent::CloseTopmost), "Esc / c");
122
1
    assert_eq!(view.all(Intent::CsvCopy), "y / q");
123
1
}
124

            
125
#[test]
126
1
fn several_chords_of_one_action_in_one_scope_all_show() {
127
1
    let app = crate::test_support::app();
128
1
    let view: KeyView<'_> = in_modal(&app, &draft());
129
1
    assert_eq!(view.all(Intent::ScrollLineUp), "Up / k");
130
1
}
131

            
132
#[test]
133
1
fn the_confirm_prompt_hints_yes_and_no() {
134
1
    let mut app = crate::test_support::app();
135
1
    app.overlays.push(Modal::Confirm {
136
1
        focus: None,
137
1
        prompt: "Sure?".to_string(),
138
1
        action: crate::modal::ConfirmAction::DiscardForm,
139
1
    });
140
1
    let shown = labels(&app);
141
1
    assert!(has(&app, "y Yes") && has(&app, "n No"), "{shown:?}");
142
1
}
143

            
144
#[test]
145
1
fn a_detail_window_hints_close_and_edit() {
146
1
    let mut app = crate::test_support::app();
147
1
    app.show_viewer(
148
1
        crate::viewer::Source::Detail { id: String::new() },
149
1
        Vec::new(),
150
1
    );
151
1
    for hint in ["Esc Close", "e Edit"] {
152
2
        assert!(has(&app, hint), "{hint}: {:?}", labels(&app));
153
    }
154
1
}