1
//! The bindings the focused chain offers, shadowing resolved: what the status bar,
2
//! Help and the footers show.
3

            
4
use tvui::keymap::Chord;
5

            
6
use super::scopes;
7
use crate::app::App;
8
use crate::event::{Intent, acts, resolve_overlay};
9
use crate::form::Form;
10
use crate::modal::Modal;
11
use crate::widgets::WidgetKind;
12
use crate::window::WindowId;
13

            
14
/// A binding some key press reaches from the focused chain.
15
pub(super) struct Shown<'a> {
16
    pub scope: &'static str,
17
    pub chords: &'a [Chord],
18
    pub intent: Intent,
19
}
20

            
21
/// The chords as typed, `C-x C-s`.
22
23904
pub(super) fn chord_text(chords: &[Chord]) -> String {
23
23904
    let names: Vec<String> = chords.iter().map(Chord::to_string).collect();
24
23904
    names.join(" ")
25
23904
}
26

            
27
/// The keys of one scope chain that do something now: innermost scope first; a chord an
28
/// inner scope binds hides the outer binding of it, and an intent that does nothing in
29
/// the focus of `app` is left out.
30
pub struct KeyView<'a> {
31
    pub(super) shown: Vec<Shown<'a>>,
32
}
33

            
34
impl<'a> KeyView<'a> {
35
3274
    pub(super) fn of(app: &'a App, chain: &[&'static str]) -> Self {
36
127999
        Self::collect(app, chain, |intent| acts(app, intent))
37
3274
    }
38

            
39
    /// The keys of `chain` whose intent satisfies `keep`.
40
3441
    fn collect(app: &'a App, chain: &[&'static str], keep: impl Fn(Intent) -> bool) -> Self {
41
3441
        let map = &app.keys.map;
42
3441
        let mut shown: Vec<Shown<'a>> = Vec::new();
43
3441
        for (depth, scope) in chain.iter().enumerate() {
44
12349
            let inner = &chain[..depth];
45
12349
            for (chords, intent) in map.bindings(scope) {
46
140756
                let hidden = inner
47
140756
                    .iter()
48
1928041
                    .any(|outer| map.bindings(outer).any(|(other, _)| other == chords));
49
140756
                if !hidden && keep(*intent) {
50
98381
                    shown.push(Shown {
51
98381
                        scope,
52
98381
                        chords,
53
98381
                        intent: *intent,
54
98381
                    });
55
42375
                }
56
            }
57
        }
58
3441
        KeyView { shown }
59
3441
    }
60

            
61
    /// The first key of the chain that does `intent`, as typed.
62
    #[must_use]
63
1086
    pub fn key(&self, intent: Intent) -> Option<String> {
64
12403
        let found = self.shown.iter().find(|shown| shown.intent == intent);
65
1086
        found.map(|shown| chord_text(shown.chords))
66
1086
    }
67

            
68
    /// The key of the outermost scope of the chain that does `intent`, as typed: the
69
    /// primary chord, where inner scopes only alias it.
70
    #[must_use]
71
1800
    pub fn outermost_key(&self, intent: Intent) -> Option<String> {
72
1800
        let outer = self
73
1800
            .shown
74
1800
            .iter()
75
1800
            .rev()
76
76031
            .find(|shown| shown.intent == intent)?;
77
260
        let first = self
78
260
            .shown
79
260
            .iter()
80
8550
            .find(|shown| shown.intent == intent && shown.scope == outer.scope);
81
260
        first.map(|shown| chord_text(shown.chords))
82
1800
    }
83

            
84
    /// The intent the chain binds to `key`, as typed.
85
    #[must_use]
86
1161
    pub fn bound(&self, key: &str) -> Option<Intent> {
87
1161
        let found = self
88
1161
            .shown
89
1161
            .iter()
90
12357
            .find(|shown| chord_text(shown.chords) == key);
91
1161
        found.map(|shown| shown.intent)
92
1161
    }
93

            
94
    /// Every key of the chain that does `intent`, as `Esc / q`.
95
    #[must_use]
96
3
    pub fn all(&self, intent: Intent) -> String {
97
3
        let keys: Vec<String> = self
98
3
            .shown
99
3
            .iter()
100
100
            .filter(|shown| shown.intent == intent)
101
6
            .map(|shown| chord_text(shown.chords))
102
            .collect();
103
3
        keys.join(" / ")
104
3
    }
105
}
106

            
107
/// The keys of the chain the focused layer uses.
108
#[must_use]
109
99
pub fn here(app: &App) -> KeyView<'_> {
110
99
    KeyView::of(app, &scopes::chain(app).names)
111
99
}
112

            
113
/// The keys of the view scopes of the active tab, whatever has the focus.
114
#[must_use]
115
88
pub fn in_view(app: &App) -> KeyView<'_> {
116
88
    KeyView::of(app, &scopes::front_view(app).names)
117
88
}
118

            
119
/// The keys of the view scopes of window `id`, whatever has the focus.
120
#[must_use]
121
75
pub fn in_window(app: &App, id: WindowId) -> KeyView<'_> {
122
75
    KeyView::of(app, &scopes::window_view(app, id).names)
123
75
}
124

            
125
/// The keys of the view scopes of the active tab, those that do nothing now included.
126
#[must_use]
127
167
pub fn in_view_all(app: &App) -> KeyView<'_> {
128
167
    KeyView::collect(app, &scopes::front_view(app).names, |_| true)
129
167
}
130

            
131
/// The keys of `modal` when it is the topmost overlay.
132
#[must_use]
133
706
pub fn in_modal<'a>(app: &'a App, modal: &Modal) -> KeyView<'a> {
134
706
    KeyView::of(app, &scopes::overlay(modal).names)
135
706
}
136

            
137
/// The keys of `form` as its focused field has them.
138
#[must_use]
139
463
pub fn in_form<'a>(app: &'a App, form: &Form) -> KeyView<'a> {
140
463
    KeyView::of(
141
463
        app,
142
463
        &scopes::form_scopes(form.focused_widget_kind(), form.focused_select()).names,
143
463
    )
144
463
}
145

            
146
/// The keys of a rows field, whether or not it has the focus.
147
#[must_use]
148
463
pub fn in_rows(app: &App) -> KeyView<'_> {
149
463
    KeyView::of(
150
463
        app,
151
463
        &scopes::form_scopes(Some(WidgetKind::Rows), None).names,
152
463
    )
153
463
}
154

            
155
/// A status bar item: the intent a click on it applies, the hotkey and the label.
156
#[derive(Debug, Clone, PartialEq, Eq)]
157
pub struct Hint {
158
    pub intent: Intent,
159
    pub key: String,
160
    pub label: String,
161
}
162

            
163
impl Hint {
164
    #[must_use]
165
8271
    pub fn pair(&self) -> (String, String) {
166
8271
        (self.key.clone(), self.label.clone())
167
8271
    }
168
}
169

            
170
/// The intent, hotkey and label of each action the chain's scopes list as hints: `global` first, a
171
/// fixed place on the bar, then the inner scopes innermost first. The key is the innermost
172
/// binding of the intent from the hinting scope outwards, if it does something now.
173
#[must_use]
174
1380
pub fn hinted(app: &App) -> Vec<Hint> {
175
1380
    let chain = scopes::chain(app).names;
176
1380
    let view = KeyView::of(app, &chain);
177
1380
    let (global, inner) = chain
178
1380
        .split_last()
179
1380
        .map_or((None, &[][..]), |(g, i)| (Some(g), i));
180
1380
    let mut done: Vec<Intent> = Vec::new();
181
1380
    let mut bar = Vec::new();
182
1380
    for scope in global.into_iter().chain(inner) {
183
4709
        for intent in app.keys.map.hinted(scope) {
184
28352
            let from = chain.iter().position(|name| name == scope).unwrap_or(0);
185
11535
            let own = view
186
11535
                .shown
187
11535
                .iter()
188
202706
                .find(|shown| shown.intent == *intent && chain[from..].contains(&shown.scope));
189
11535
            if let (Some(own), Some(label), false) =
190
11535
                (own, hint_label(app, *intent), done.contains(intent))
191
            {
192
8316
                bar.push(Hint {
193
8316
                    intent: *intent,
194
8316
                    key: chord_text(own.chords),
195
8316
                    label: button_label(app, *intent, label),
196
8316
                });
197
8316
                done.push(*intent);
198
3219
            }
199
        }
200
    }
201
1380
    bar
202
1380
}
203

            
204
/// The label of `intent` as what it does now: a key that tests a template form says so.
205
11535
fn hint_label(app: &App, intent: Intent) -> Option<&'static str> {
206
11535
    match resolve_overlay(app, intent) {
207
2
        resolved @ Intent::Template(_) => resolved.label(),
208
11533
        _ => intent.label(),
209
    }
210
11535
}
211

            
212
/// The label of the top modal's button that presses `intent`, else `label`.
213
8316
fn button_label(app: &App, intent: Intent, label: &str) -> String {
214
8316
    let specs = app.overlays.top().map_or(&[][..], Modal::button_specs);
215
8316
    specs
216
8316
        .iter()
217
6235
        .find(|&&(_, pressed)| pressed == intent)
218
7029
        .map_or_else(|| label.to_string(), |&(text, _)| tvui::plain(text))
219
8316
}