streampager/
bindings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Key bindings.

use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use indexmap::IndexMap;
use thiserror::Error;

use crate::action::Action;
use crate::file::FileIndex;

/// Key codes for key bindings.
///
pub use termwiz::input::KeyCode;

/// Keyboard modifiers for key bindings.
///
pub use termwiz::input::Modifiers;

/// Errors specific to bindings.
#[derive(Debug, Error)]
pub enum BindingError {
    /// Error when a binding is invalid.
    #[error("invalid keybinding: {0}")]
    Invalid(String),

    /// Binding is missing a parameter.
    #[error("{0} missing parameter {1}")]
    MissingParameter(String, usize),

    /// Integer parsing error.
    #[error("invalid integer")]
    InvalidInt(#[from] std::num::ParseIntError),

    /// Wrapped error within the context of a binding parameter.
    #[error("invalid {binding} parameter {index}")]
    ForParameter {
        /// Wrapped error.
        #[source]
        error: Box<BindingError>,

        /// Binding.
        binding: String,

        /// Parameter index.
        index: usize,
    },
}

impl BindingError {
    fn for_parameter(self, binding: String, index: usize) -> Self {
        Self::ForParameter {
            error: Box::new(self),
            binding,
            index,
        }
    }
}

type Result<T> = std::result::Result<T, BindingError>;

/// A key binding category.
///
/// Key bindings are listed by category in the help screen.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Category {
    /// Uncategorized actions.
    None,

    /// Actions for controlling the pager.
    General,

    /// Actions for moving around the file.
    Navigation,

    /// Actions that affect the presentation of the file.
    Presentation,

    /// Actions that initiate or modify searches.
    Searching,

    /// Actions that are hidden in help view (for example, too verbose).
    Hidden,
}

impl Category {
    /// Non-hidden categories.
    pub(crate) fn categories() -> impl Iterator<Item = Category> {
        [
            Category::General,
            Category::Navigation,
            Category::Presentation,
            Category::Searching,
            Category::None,
        ]
        .iter()
        .cloned()
    }
}

impl std::fmt::Display for Category {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Category::None => f.write_str("Other"),
            Category::General => f.write_str("General"),
            Category::Navigation => f.write_str("Navigation"),
            Category::Presentation => f.write_str("Presentation"),
            Category::Searching => f.write_str("Searching"),
            Category::Hidden => f.write_str("Hidden"),
        }
    }
}

/// An action that may be bound to a key.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Binding {
    /// An action.
    Action(Action),

    /// A custom binding.
    Custom(CustomBinding),

    /// An unrecognised binding.
    Unrecognized(String),
}

impl Binding {
    /// Create new custom binding.
    ///
    /// When this binding is invoked, the callback is called.  The callback is provided with the
    /// file index of the file that is currently being displayed.  Note that this may differ from
    /// any of the file indexes returned by the `add` methods on the `Pager`, as additional file
    /// indexes can be allocated, e.g. for the help screen.
    pub fn custom(
        category: Category,
        description: impl Into<String>,
        callback: impl Fn(FileIndex) + Send + Sync + 'static,
    ) -> Self {
        Binding::Custom(CustomBinding::new(category, description, callback))
    }

    pub(crate) fn category(&self) -> Category {
        match self {
            Binding::Action(action) => {
                use Action::*;
                match action {
                    Quit | Refresh | Help | Cancel => Category::General,
                    PreviousFile
                    | NextFile
                    | ScrollUpLines(_)
                    | ScrollDownLines(_)
                    | ScrollUpScreenFraction(_)
                    | ScrollDownScreenFraction(_)
                    | ScrollToTop
                    | ScrollToBottom
                    | ScrollLeftColumns(_)
                    | ScrollRightColumns(_)
                    | ScrollLeftScreenFraction(_)
                    | ScrollRightScreenFraction(_)
                    | PromptGoToLine => Category::Navigation,
                    ToggleRuler | ToggleLineNumbers | ToggleLineWrapping => Category::Presentation,
                    PromptSearchFromStart
                    | PromptSearchForwards
                    | PromptSearchBackwards
                    | NextMatch
                    | PreviousMatch
                    | NextMatchLine
                    | PreviousMatchLine
                    | PreviousMatchScreen
                    | NextMatchScreen
                    | FirstMatch
                    | LastMatch => Category::Searching,
                    AppendDigitToRepeatCount(_) => Category::Hidden,
                }
            }
            Binding::Custom(binding) => binding.category,
            Binding::Unrecognized(_) => Category::None,
        }
    }

    /// Parse a keybinding identifier and list of parameters into a key binding.
    pub fn parse(ident: String, params: Vec<String>) -> Result<Self> {
        use Action::*;

        let param_usize = |index| -> Result<usize> {
            let value: &String = params
                .get(index)
                .ok_or_else(|| BindingError::MissingParameter(ident.clone(), index))?;
            let value = value
                .parse::<usize>()
                .map_err(|err| BindingError::from(err).for_parameter(ident.clone(), index))?;
            Ok(value)
        };

        let action = match ident.as_str() {
            "Quit" => Quit,
            "Refresh" => Refresh,
            "Help" => Help,
            "Cancel" => Cancel,
            "PreviousFile" => PreviousFile,
            "NextFile" => NextFile,
            "ToggleRuler" => ToggleRuler,
            "ScrollUpLines" => ScrollUpLines(param_usize(0)?),
            "ScrollDownLines" => ScrollDownLines(param_usize(0)?),
            "ScrollUpScreenFraction" => ScrollUpScreenFraction(param_usize(0)?),
            "ScrollDownScreenFraction" => ScrollDownScreenFraction(param_usize(0)?),
            "ScrollToTop" => ScrollToTop,
            "ScrollToBottom" => ScrollToBottom,
            "ScrollLeftColumns" => ScrollLeftColumns(param_usize(0)?),
            "ScrollRightColumns" => ScrollRightColumns(param_usize(0)?),
            "ScrollLeftScreenFraction" => ScrollLeftScreenFraction(param_usize(0)?),
            "ScrollRightScreenFraction" => ScrollRightScreenFraction(param_usize(0)?),
            "ToggleLineNumbers" => ToggleLineNumbers,
            "ToggleLineWrapping" => ToggleLineWrapping,
            "PromptGoToLine" => PromptGoToLine,
            "PromptSearchFromStart" => PromptSearchFromStart,
            "PromptSearchForwards" => PromptSearchForwards,
            "PromptSearchBackwards" => PromptSearchBackwards,
            "PreviousMatch" => PreviousMatch,
            "NextMatch" => NextMatch,
            "PreviousMatchLine" => PreviousMatchLine,
            "NextMatchLine" => NextMatchLine,
            "FirstMatch" => FirstMatch,
            "LastMatch" => LastMatch,
            _ => return Ok(Binding::Unrecognized(ident)),
        };

        Ok(Binding::Action(action))
    }
}

impl From<Action> for Binding {
    fn from(action: Action) -> Binding {
        Binding::Action(action)
    }
}

impl From<Action> for Option<Binding> {
    fn from(action: Action) -> Option<Binding> {
        Some(Binding::Action(action))
    }
}

impl std::fmt::Display for Binding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Binding::Action(ref a) => write!(f, "{}", a),
            Binding::Custom(ref b) => write!(f, "{}", b.description),
            Binding::Unrecognized(ref s) => write!(f, "Unrecognized binding ({})", s),
        }
    }
}

static CUSTOM_BINDING_ID: AtomicUsize = AtomicUsize::new(0);

/// A custom binding.  This can be used by applications using streampager
/// to add custom actions on keys.
#[derive(Clone)]
pub struct CustomBinding {
    /// The id of this binding.  This is unique for each binding.
    id: usize,

    /// The category of this binding.
    category: Category,

    /// The description of this binding.
    description: String,

    /// Called when the action is triggered.
    callback: Arc<dyn Fn(FileIndex) + Sync + Send>,
}

impl CustomBinding {
    /// Create a new custom binding.
    ///
    /// The category and description are used in the help screen.  The
    /// callback is executed whenever the binding is triggered.
    pub fn new(
        category: Category,
        description: impl Into<String>,
        callback: impl Fn(FileIndex) + Sync + Send + 'static,
    ) -> CustomBinding {
        CustomBinding {
            id: CUSTOM_BINDING_ID.fetch_add(1, Ordering::SeqCst),
            category,
            description: description.into(),
            callback: Arc::new(callback),
        }
    }

    /// Trigger the binding and run its callback.
    pub fn run(&self, file_index: FileIndex) {
        (self.callback)(file_index)
    }
}

impl PartialEq for CustomBinding {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for CustomBinding {}

impl std::hash::Hash for CustomBinding {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl std::fmt::Debug for CustomBinding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("CustomBinding")
            .field(&self.id)
            .field(&self.description)
            .finish()
    }
}

/// A binding to a key and its associated help visibility.  Used by
/// the keymaps macro to provide binding configuration.
#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct BindingConfig {
    /// The binding.
    pub binding: Binding,

    /// Whether this binding is visible in the help screen.
    pub visible: bool,
}

/// A collection of key bindings.
#[derive(PartialEq, Eq)]
pub struct Keymap {
    /// Map of bindings from keys.
    bindings: HashMap<(Modifiers, KeyCode), Binding>,

    /// Map of visible keys from bindings.
    keys: IndexMap<Binding, Vec<(Modifiers, KeyCode)>>,
}

impl<'a, I: IntoIterator<Item = &'a ((Modifiers, KeyCode), BindingConfig)>> From<I> for Keymap {
    fn from(iter: I) -> Keymap {
        let iter = iter.into_iter();
        let size_hint = iter.size_hint();
        let mut bindings = HashMap::with_capacity(size_hint.0);
        let mut keys = IndexMap::with_capacity(size_hint.0);
        for &((modifiers, keycode), ref binding_config) in iter {
            bindings.insert((modifiers, keycode), binding_config.binding.clone());
            if binding_config.visible {
                keys.entry(binding_config.binding.clone())
                    .or_insert_with(Vec::new)
                    .push((modifiers, keycode));
            }
        }
        Keymap { bindings, keys }
    }
}

impl Keymap {
    /// Create a new, empty, keymap.
    pub fn new() -> Self {
        Keymap {
            bindings: HashMap::new(),
            keys: IndexMap::new(),
        }
    }

    /// Get the binding associated with a key combination.
    pub fn get(&self, modifiers: Modifiers, keycode: KeyCode) -> Option<&Binding> {
        self.bindings.get(&(modifiers, keycode))
    }

    /// Bind (or unbind) a key combination.
    pub fn bind(
        &mut self,
        modifiers: Modifiers,
        keycode: KeyCode,
        binding: impl Into<Option<Binding>>,
    ) -> &mut Self {
        self.bind_impl(modifiers, keycode, binding.into(), true)
    }

    /// Bind (or unbind) a key combination, but exclude it from the help screen.
    pub fn bind_hidden(
        &mut self,
        modifiers: Modifiers,
        keycode: KeyCode,
        binding: impl Into<Option<Binding>>,
    ) -> &mut Self {
        self.bind_impl(modifiers, keycode, binding.into(), false)
    }

    fn bind_impl(
        &mut self,
        modifiers: Modifiers,
        keycode: KeyCode,
        binding: Option<Binding>,
        visible: bool,
    ) -> &mut Self {
        if let Some(old_binding) = self.bindings.remove(&(modifiers, keycode)) {
            if let Some(keys) = self.keys.get_mut(&old_binding) {
                keys.retain(|&item| item != (modifiers, keycode));
            }
        }
        if let Some(binding) = binding {
            self.bindings.insert((modifiers, keycode), binding.clone());
            if visible {
                self.keys
                    .entry(binding)
                    .or_insert_with(Vec::new)
                    .push((modifiers, keycode));
            }
        }
        self
    }

    pub(crate) fn iter_keys(&self) -> impl Iterator<Item = (&Binding, &Vec<(Modifiers, KeyCode)>)> {
        self.keys.iter()
    }
}

impl Default for Keymap {
    fn default() -> Self {
        Keymap::from(crate::keymaps::default::KEYMAP.iter())
    }
}

impl std::fmt::Debug for Keymap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Keymap")
            .field(&format!("<{} keys bound>", self.bindings.len()))
            .finish()
    }
}