tread 0.4.3

A terminal reader for markdown, CSV, JSON and source code. Zero dependencies.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! The single source of truth for keybindings.
//!
//! [`BINDINGS`] is used by *both* the dispatcher ([`lookup`]) and the help
//! overlay ([`help_rows`]), so the help text can never drift from what the keys
//! actually do (SPEC.md §Keybindings).
#![deny(unsafe_code)]

use crate::key::{Key, KeyEvent};

/// Everything the pager can be asked to do by a key press.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
    Quit,
    /// Ctrl-C: leave immediately, without stepping back through the nav stack.
    ForceQuit,
    /// `j` / `↓`: one visible row, in every format, always. A closed block is
    /// one row, so a coarser default unit could only ever skip rows that are
    /// on the screen.
    LineDown,
    LineUp,
    HalfDown,
    HalfUp,
    PageDown,
    PageUp,
    Top,
    Bottom,
    ScrollLeft,
    ScrollRight,
    /// `←`: scroll left on a row that scrolls, otherwise move the link focus
    /// back along the current row (SPEC.md §"Selecting links on a line").
    ArrowLeft,
    /// `→`: the same rule, forwards.
    ArrowRight,
    /// `w`: widen the column under the cursor, where the format has columns.
    Widen,
    /// Show or hide what the format hides by default (a listing's dotfiles).
    ToggleHidden,
    ToggleCollapse,
    /// `r`: show the raw record under the cursor, where a row stands for one —
    /// open its JSON tree, or shut the one that is open (SPEC.md §Lenses).
    OpenTree,
    OpenSection,
    CloseSection,
    CollapseAll,
    ExpandAll,
    NextHeading,
    PrevHeading,
    Outline,
    Help,
    /// Enter: follow the focused link, else toggle the section.
    Follow,
    Back,
    Forward,
    OpenIndex,
    NextDoc,
    PrevDoc,
    SearchForward,
    SearchBackward,
    NextMatch,
    PrevMatch,
    /// Enter/leave visual line-select mode.
    Visual,
    /// Yank the visual selection.
    Yank,
    /// Yank the whole section under the cursor.
    YankSection,
    /// Yank the code block under (or nearest below) the cursor, verbatim.
    YankCode,
}

/// One key (optionally behind a prefix such as `z`) that fires an action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Trigger {
    pub prefix: Option<char>,
    pub key: Key,
}

impl Trigger {
    const fn k(key: Key) -> Trigger {
        Trigger { prefix: None, key }
    }
    const fn c(ch: char) -> Trigger {
        Trigger {
            prefix: None,
            key: Key::Char(ch),
        }
    }
    const fn z(ch: char) -> Trigger {
        Trigger {
            prefix: Some('z'),
            key: Key::Char(ch),
        }
    }
}

/// A row of the keymap: what to press, what it does, and the action fired.
pub struct Binding {
    /// Human-readable key list, shown in the help overlay.
    pub keys: &'static str,
    pub desc: &'static str,
    pub action: Action,
    pub triggers: &'static [Trigger],
}

/// Keys that introduce a chord. Only `z` today.
pub const PREFIXES: &[char] = &['z'];

pub fn is_prefix(ev: KeyEvent) -> bool {
    match ev.key {
        Key::Char(c) => !ev.mods.ctrl && !ev.mods.alt && PREFIXES.contains(&c),
        _ => false,
    }
}

use Action as A;

pub const BINDINGS: &[Binding] = &[
    Binding {
        keys: "j / \u{2193}",
        desc: "one row down",
        action: A::LineDown,
        triggers: &[Trigger::c('j'), Trigger::k(Key::Down)],
    },
    Binding {
        keys: "k / \u{2191}",
        desc: "one row up",
        action: A::LineUp,
        triggers: &[Trigger::c('k'), Trigger::k(Key::Up)],
    },
    Binding {
        keys: "d",
        desc: "half page down",
        action: A::HalfDown,
        triggers: &[Trigger::c('d')],
    },
    Binding {
        keys: "u",
        desc: "half page up",
        action: A::HalfUp,
        triggers: &[Trigger::c('u')],
    },
    Binding {
        keys: "space / f",
        desc: "page down",
        action: A::PageDown,
        triggers: &[Trigger::c(' '), Trigger::c('f'), Trigger::k(Key::PageDown)],
    },
    Binding {
        keys: "b",
        desc: "page up",
        action: A::PageUp,
        triggers: &[Trigger::c('b'), Trigger::k(Key::PageUp)],
    },
    Binding {
        keys: "g",
        desc: "top of document",
        action: A::Top,
        triggers: &[Trigger::c('g'), Trigger::k(Key::Home)],
    },
    Binding {
        keys: "G",
        desc: "bottom of document",
        action: A::Bottom,
        triggers: &[Trigger::c('G'), Trigger::k(Key::End)],
    },
    Binding {
        keys: "h",
        desc: "scroll left \u{2014} code, wide tables, one column",
        action: A::ScrollLeft,
        triggers: &[Trigger::c('h')],
    },
    Binding {
        keys: "l",
        desc: "scroll right \u{2014} code, wide tables, one column",
        action: A::ScrollRight,
        triggers: &[Trigger::c('l')],
    },
    // `h`/`l` scroll everywhere; the arrows scroll only where scrolling means
    // something and otherwise walk the links on the row (SPEC.md §"Selecting
    // links on a line").
    Binding {
        keys: "\u{2190}",
        desc: "previous link on this row (scrolls left on a scrollable row)",
        action: A::ArrowLeft,
        triggers: &[Trigger::k(Key::Left)],
    },
    Binding {
        keys: "\u{2192}",
        desc: "next link on this row (scrolls right on a scrollable row)",
        action: A::ArrowRight,
        triggers: &[Trigger::k(Key::Right)],
    },
    Binding {
        keys: "w",
        desc: "widen the column under the cursor to fit the screen",
        action: A::Widen,
        triggers: &[Trigger::c('w')],
    },
    Binding {
        keys: "a",
        desc: "show or hide what this view hides (dotfiles, code bodies)",
        action: A::ToggleHidden,
        triggers: &[Trigger::c('a')],
    },
    Binding {
        keys: "za",
        desc: "toggle the section at the cursor",
        action: A::ToggleCollapse,
        triggers: &[Trigger::z('a')],
    },
    Binding {
        keys: "Enter",
        desc: "follow the focused link, else one level into the row",
        action: A::Follow,
        triggers: &[Trigger::k(Key::Enter)],
    },
    Binding {
        keys: "r",
        desc: "show the raw record under the cursor, from either level",
        action: A::OpenTree,
        triggers: &[Trigger::c('r')],
    },
    Binding {
        keys: "zo",
        desc: "open the section at the cursor",
        action: A::OpenSection,
        triggers: &[Trigger::z('o')],
    },
    Binding {
        keys: "zc",
        desc: "close the section at the cursor",
        action: A::CloseSection,
        triggers: &[Trigger::z('c')],
    },
    Binding {
        keys: "zM",
        desc: "collapse every section",
        action: A::CollapseAll,
        triggers: &[Trigger::z('M')],
    },
    Binding {
        keys: "zR",
        desc: "expand every section",
        action: A::ExpandAll,
        triggers: &[Trigger::z('R')],
    },
    Binding {
        keys: "Tab",
        desc: "next heading (next block under a lens)",
        action: A::NextHeading,
        triggers: &[Trigger::k(Key::Tab)],
    },
    Binding {
        keys: "S-Tab",
        desc: "previous heading (previous block under a lens)",
        action: A::PrevHeading,
        triggers: &[Trigger::k(Key::BackTab)],
    },
    Binding {
        keys: "o",
        desc: "outline overlay",
        action: A::Outline,
        triggers: &[Trigger::c('o')],
    },
    Binding {
        keys: "/",
        desc: "search forward",
        action: A::SearchForward,
        triggers: &[Trigger::c('/')],
    },
    Binding {
        keys: "?",
        desc: "search backward",
        action: A::SearchBackward,
        triggers: &[Trigger::c('?')],
    },
    // `n`/`N` are search motions while a search is active and link motions
    // otherwise, which is what SPEC.md §Keybindings asks of them.
    Binding {
        keys: "n",
        desc: "next link (next search match while searching)",
        action: A::NextMatch,
        triggers: &[Trigger::c('n')],
    },
    Binding {
        keys: "N",
        desc: "previous link (previous match while searching)",
        action: A::PrevMatch,
        triggers: &[Trigger::c('N')],
    },
    Binding {
        keys: "Backspace / -",
        desc: "back in document history",
        action: A::Back,
        triggers: &[Trigger::k(Key::Backspace), Trigger::c('-')],
    },
    Binding {
        keys: "+",
        desc: "forward in document history",
        action: A::Forward,
        triggers: &[Trigger::c('+')],
    },
    Binding {
        keys: "i",
        desc: "corpus index (j/k move, Enter open, / filter, Esc close)",
        action: A::OpenIndex,
        triggers: &[Trigger::c('i')],
    },
    Binding {
        keys: "]",
        desc: "next document in index order",
        action: A::NextDoc,
        triggers: &[Trigger::c(']')],
    },
    Binding {
        keys: "[",
        desc: "previous document in index order",
        action: A::PrevDoc,
        triggers: &[Trigger::c('[')],
    },
    Binding {
        keys: "v",
        desc: "visual line select (j/k/d/u/g/G extend, Esc cancels)",
        action: A::Visual,
        triggers: &[Trigger::c('v')],
    },
    Binding {
        keys: "y",
        desc: "yank the selection, or the focused link's target",
        action: A::Yank,
        triggers: &[Trigger::c('y')],
    },
    Binding {
        keys: "Y",
        desc: "yank the section under the cursor",
        action: A::YankSection,
        triggers: &[Trigger::c('Y')],
    },
    Binding {
        keys: "c",
        desc: "yank the code block under the cursor, verbatim",
        action: A::YankCode,
        triggers: &[Trigger::c('c')],
    },
    Binding {
        keys: "F1 / H",
        desc: "this help",
        action: A::Help,
        triggers: &[Trigger::k(Key::F(1)), Trigger::c('H')],
    },
    Binding {
        keys: "q",
        desc: "quit (steps back first when the history is deep)",
        action: A::Quit,
        triggers: &[Trigger::c('q')],
    },
    // Raw mode clears ISIG, so Ctrl-C never becomes SIGINT: it arrives as a
    // key and has to be bound here or it does nothing at all.
    Binding {
        keys: "Ctrl-C",
        desc: "quit immediately, whatever the history depth",
        action: A::ForceQuit,
        triggers: &[Trigger::k(Key::Ctrl('c'))],
    },
];

/// Resolve a key press (with any pending chord prefix) to an action.
pub fn lookup(prefix: Option<char>, ev: KeyEvent) -> Option<Action> {
    // A modifier no binding asked for means the press is not that binding:
    // `Ctrl-j` is not `j`. But a key that *is* a control key already says so in
    // `Key::Ctrl`, and the two decoders disagree about the modifier bits on it
    // — a raw `0x03` carries none, kitty's `CSI u` sets `ctrl` — so rejecting
    // on the bit alone would make `Ctrl-C` work on one terminal and be
    // swallowed on another.
    if (ev.mods.ctrl && !matches!(ev.key, Key::Ctrl(_))) || ev.mods.alt {
        return None;
    }
    BINDINGS
        .iter()
        .find(|b| {
            b.triggers
                .iter()
                .any(|t| t.prefix == prefix && t.key == ev.key)
        })
        .map(|b| b.action)
}

/// `(keys, description)` rows for the help overlay, in table order.
pub fn help_rows() -> Vec<(&'static str, &'static str)> {
    BINDINGS.iter().map(|b| (b.keys, b.desc)).collect()
}

/// Widest key column, for aligning the help overlay.
pub fn help_key_width() -> usize {
    BINDINGS
        .iter()
        .map(|b| crate::render::str_width(b.keys))
        .max()
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::key::Mods;

    fn ev(c: char) -> KeyEvent {
        KeyEvent::plain(Key::Char(c))
    }

    #[test]
    fn plain_keys_resolve() {
        assert_eq!(lookup(None, ev('j')), Some(A::LineDown));
        assert_eq!(lookup(None, KeyEvent::plain(Key::Down)), Some(A::LineDown));
        assert_eq!(lookup(None, ev('q')), Some(A::Quit));
        assert_eq!(lookup(None, ev('G')), Some(A::Bottom));
    }

    #[test]
    fn chords_need_their_prefix() {
        assert_eq!(lookup(Some('z'), ev('a')), Some(A::ToggleCollapse));
        // `a` is its own binding now, so the point is that it is *not* the
        // chord: pressing it without `z` must never fold a section.
        assert_eq!(lookup(None, ev('a')), Some(A::ToggleHidden));
        assert_ne!(lookup(None, ev('a')), Some(A::ToggleCollapse));
        assert_eq!(lookup(Some('z'), ev('j')), None);
        assert_eq!(lookup(Some('z'), ev('M')), Some(A::CollapseAll));
    }

    #[test]
    fn z_is_the_only_prefix() {
        assert!(is_prefix(ev('z')));
        assert!(!is_prefix(ev('j')));
        assert!(!is_prefix(KeyEvent::with(Key::Char('z'), Mods { ctrl: true, ..Mods::NONE })));
    }

    #[test]
    fn modified_keys_do_not_fire_bindings() {
        let ctrl_j = KeyEvent::with(Key::Char('j'), Mods { ctrl: true, ..Mods::NONE });
        assert_eq!(lookup(None, ctrl_j), None);
    }

    /// A control key from both decoders. A raw `0x03` arrives as
    /// `Key::Ctrl('c')` with no modifier bits; kitty's `CSI u` sends the same
    /// key *with* `ctrl` set, and both must reach the same binding — the hole
    /// that swallowed `Ctrl-C` on one terminal and not the other.
    #[test]
    fn a_control_key_resolves_from_either_decoder() {
        let raw = KeyEvent::plain(Key::Ctrl('c'));
        assert_eq!(lookup(None, raw), Some(A::ForceQuit));
        let kitty = KeyEvent::with(Key::Ctrl('c'), Mods { ctrl: true, ..Mods::NONE });
        assert_eq!(lookup(None, kitty), Some(A::ForceQuit));
        // A chord prefix still gates it: `z` then Ctrl-C is nothing.
        assert_eq!(lookup(Some('z'), raw), None);
    }

    #[test]
    fn help_covers_every_binding_and_never_drifts() {
        assert_eq!(help_rows().len(), BINDINGS.len());
        for b in BINDINGS {
            assert!(!b.keys.is_empty() && !b.desc.is_empty());
            assert!(!b.triggers.is_empty(), "{} has no trigger", b.keys);
            // Every documented binding must be reachable by the dispatcher.
            for t in b.triggers {
                assert_eq!(lookup(t.prefix, KeyEvent::plain(t.key)), Some(b.action));
            }
        }
    }

    #[test]
    fn no_trigger_is_claimed_twice() {
        let mut seen: Vec<Trigger> = Vec::new();
        for b in BINDINGS {
            for t in b.triggers {
                assert!(!seen.contains(t), "duplicate trigger {t:?}");
                seen.push(*t);
            }
        }
    }
}