tix-tracker 0.4.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
Documentation
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
482
483
484
485
486
487
488
489
490
491
use std::fmt::Write;

use crate::keybindings::{EffectiveBindings, InputContext, KeyBindings, TuiAction};

pub const MODE_BOARD: &str = "board";
pub const MODE_DETAIL: &str = "detail";
pub const MODE_HELP: &str = "help";
pub const MODE_COMMENT: &str = "comment";
pub const MODE_ASSET: &str = "asset";
pub const MODE_FILTER: &str = "filter";

const MAX_HINT_CHARS: usize = 80;

pub struct Cmd {
    pub key: &'static str,
    pub desc: &'static str,
    pub modes: &'static [&'static str],
    pub mode: &'static str,
}

pub const COMMANDS: &[Cmd] = &[
    Cmd {
        key: "j/k",
        desc: "move up/down",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "n",
        desc: "new issue",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "?",
        desc: "help",
        modes: &[MODE_BOARD, MODE_DETAIL],
        mode: "board/detail",
    },
    Cmd {
        key: "h/l",
        desc: "column",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "enter",
        desc: "open",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "d",
        desc: "delete",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "< / >",
        desc: "move",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "r",
        desc: "refresh",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "/",
        desc: "filter",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "f",
        desc: "criteria",
        modes: &[MODE_BOARD],
        mode: MODE_BOARD,
    },
    Cmd {
        key: "c",
        desc: "comment",
        modes: &[MODE_DETAIL],
        mode: MODE_DETAIL,
    },
    Cmd {
        key: "a",
        desc: "asset",
        modes: &[MODE_DETAIL],
        mode: MODE_DETAIL,
    },
];

fn actions(context: InputContext) -> &'static [TuiAction] {
    match context {
        InputContext::Board => &[
            TuiAction::NewIssue,
            TuiAction::Help,
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::Planning,
            TuiAction::Quit,
        ],
        InputContext::Detail => &[
            TuiAction::Comment,
            TuiAction::Asset,
            // #1359: the three mutating relationship operations and the cursor that
            // names which edge `E` and `d` act on. A binding that exists but is not
            // in the footer is a binding nobody finds.
            TuiAction::NewRelationship,
            TuiAction::NextRelationship,
            TuiAction::PreviousRelationship,
            TuiAction::EditRelationship,
            TuiAction::RemoveRelationship,
            TuiAction::Edit,
            TuiAction::ScrollUp,
            TuiAction::ScrollDown,
            TuiAction::Help,
        ],
        _ => &[],
    }
}

pub fn hint_bar(active_modes: &[&str]) -> String {
    let bindings =
        KeyBindings::resolve(&KeyBindings::default()).expect("built-in bindings are valid");
    for mode in active_modes {
        if let Some(context) = match *mode {
            MODE_BOARD => Some(InputContext::Board),
            MODE_DETAIL => Some(InputContext::Detail),
            _ => None,
        } {
            return hint_bar_for(&bindings, context);
        }
    }
    String::new()
}

pub fn hint_bar_for(bindings: &EffectiveBindings, context: InputContext) -> String {
    if context == InputContext::Board
        && bindings.label(context, TuiAction::MoveUp) == "Up / k"
        && bindings.label(context, TuiAction::MoveDown) == "Down / j"
    {
        return truncate(
            &format!(
                "j/k nav · {} new issue · {} help",
                bindings.label(context, TuiAction::NewIssue),
                bindings.label(context, TuiAction::Help),
            ),
            MAX_HINT_CHARS,
        );
    }
    let mut text = String::new();
    for action in actions(context) {
        let _ = write!(
            text,
            "{} {} · ",
            bindings.label(context, *action),
            action.description()
        );
    }
    truncate(text.trim_end_matches(" · "), MAX_HINT_CHARS)
}

/// The contexts the keybindings overlay documents, in reading order.
///
/// One list, shared with the completeness guard in this module's tests: a context
/// added to the overlay but not to the guard (or the reverse) is how a whole surface
/// becomes invisible in the help without anyone noticing.
const HELP_CONTEXTS: [InputContext; 25] = [
    InputContext::Detail,
    InputContext::Board,
    InputContext::Rollover,
    InputContext::Form,
    InputContext::FormPicker,
    InputContext::ConfirmDelete,
    InputContext::MoveIntoDone,
    // WP04: the blocked-move prompt's keys belong in the help overlay too, or
    // a user who never guessed `y` has no way to learn it.
    InputContext::ConfirmBlockedMove,
    InputContext::Help,
    InputContext::HelpSearch,
    InputContext::ContextMenu,
    InputContext::Filter,
    InputContext::FilterModal,
    InputContext::Settings,
    InputContext::SettingsPrompt,
    InputContext::PlanningManagement,
    InputContext::PlanningConfirm,
    InputContext::PlanningForm,
    InputContext::DatePicker,
    InputContext::Comment,
    InputContext::Asset,
    InputContext::LinkPrompt,
    InputContext::EditWarning,
    // WP07: the batch surface's keys, for the same reason WP04 put the
    // blocked-move prompt's there — a key nobody can discover is a key nobody
    // uses, and the batch surface is entirely keyboard-driven.
    InputContext::BulkModal,
    // WP06: the triage inbox's keys, same reason. The `t` that opens it is
    // listed by the `board` rows below.
    InputContext::Triage,
];

pub fn help_overlay(bindings: &EffectiveBindings) -> Vec<(String, String, String)> {
    let mut rows = vec![
        (
            "mouse click".to_string(),
            "click".to_string(),
            "board".to_string(),
        ),
        (
            "mouse double".to_string(),
            "double-click".to_string(),
            "board".to_string(),
        ),
        (
            "mouse right".to_string(),
            "right-click".to_string(),
            "board".to_string(),
        ),
        (
            "mouse drag".to_string(),
            "drag".to_string(),
            "board".to_string(),
        ),
    ];
    for context in HELP_CONTEXTS {
        for action in all_actions(context) {
            rows.push((
                bindings.label(context, *action),
                action.description().to_string(),
                context.as_str().to_string(),
            ));
        }
    }
    rows
}

fn all_actions(context: InputContext) -> &'static [TuiAction] {
    match context {
        InputContext::Board => &[
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::MoveLeft,
            TuiAction::MoveRight,
            TuiAction::NextBoard,
            TuiAction::PreviousBoard,
            TuiAction::Open,
            TuiAction::Edit,
            TuiAction::Agent,
            TuiAction::ToggleColumn,
            TuiAction::FilterModal,
            TuiAction::BulkModal,
            TuiAction::Triage,
            TuiAction::Mark,
            TuiAction::Unmark,
            TuiAction::MovePrevious,
            TuiAction::MoveNext,
            // The page keys (#1442): bound on the board, and until this guard existed
            // they were the only way to reach a card the column had scrolled past —
            // documented nowhere.
            TuiAction::ScrollUp,
            TuiAction::ScrollDown,
            TuiAction::NewIssue,
            // The two board keys added after this list was written: the overlay's list
            // is hand-written, so a binding it does not name is a key that works and
            // cannot be discovered. `every_bound_action_appears_in_the_overlay` is the
            // guard, and it is what found these.
            TuiAction::CloneIssue,
            TuiAction::ToggleArchived,
            TuiAction::Delete,
            TuiAction::Refresh,
            TuiAction::Settings,
            TuiAction::Planning,
            TuiAction::NewBoard,
            TuiAction::Help,
            TuiAction::Quit,
            TuiAction::TextFilter,
            TuiAction::ReorderUp,
            TuiAction::ReorderDown,
        ],
        InputContext::Detail => &[
            // The pane's own list, not the footer's: `close` is how you get back, and a
            // key that returns you is not one to leave out of the reference.
            TuiAction::Close,
            TuiAction::Edit,
            TuiAction::ScrollUp,
            TuiAction::ScrollDown,
            TuiAction::Help,
            TuiAction::Comment,
            TuiAction::Asset,
            TuiAction::NewRelationship,
            TuiAction::NextRelationship,
            TuiAction::PreviousRelationship,
            TuiAction::EditRelationship,
            TuiAction::RemoveRelationship,
        ],
        InputContext::Rollover => &[TuiAction::Submit, TuiAction::Previous, TuiAction::Cancel],
        InputContext::Form | InputContext::PlanningForm => &[
            TuiAction::Submit,
            TuiAction::Cancel,
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::OpenPicker,
        ],
        InputContext::FormPicker => &[
            TuiAction::Cancel,
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::Submit,
            TuiAction::Create,
        ],
        InputContext::ConfirmDelete
        | InputContext::PlanningConfirm
        | InputContext::ConfirmBlockedMove => &[TuiAction::Confirm, TuiAction::Cancel],
        InputContext::MoveIntoDone
        | InputContext::Filter
        | InputContext::Comment
        | InputContext::Asset
        | InputContext::LinkPrompt => &[TuiAction::Submit, TuiAction::Cancel],
        InputContext::Help => &[
            TuiAction::Cancel,
            TuiAction::Next,
            TuiAction::Previous,
            // #1496: the search key, so the overlay names the way to search it.
            TuiAction::TextFilter,
            // #1441/#1442: the overlay's page keys. These are the overlay's *rows*, not
            // a footer — the footer for this context is the hint bar `render_help` draws
            // underneath, which is the board's or the pane's.
            TuiAction::ScrollUp,
            TuiAction::ScrollDown,
            TuiAction::Submit,
        ],
        InputContext::ContextMenu => &[
            TuiAction::Cancel,
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::Submit,
        ],
        // Free text: `Esc` leaves the line (and clears it), `Enter` keeps the query.
        InputContext::HelpSearch => &[TuiAction::Cancel, TuiAction::Submit],
        InputContext::BulkModal => &[
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::Submit,
            TuiAction::Cancel,
        ],
        // WP06: only what the context actually binds. The inbox's decision keys
        // (`a`, `d`, `u`, `f`, `p`, `y`) are deliberately unbound so they stay
        // typeable in a reason field, so they cannot appear here as chords —
        // they are named by the modal's own footer instead, which is how the
        // batch surface names its unbound keys too.
        InputContext::Triage => &[
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::Toggle,
            TuiAction::Submit,
            TuiAction::Cancel,
        ],
        InputContext::FilterModal => &[
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::Reset,
            TuiAction::Submit,
            TuiAction::Cancel,
            TuiAction::OpenPicker,
        ],
        InputContext::Settings => &[
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::Next,
            TuiAction::Previous,
            TuiAction::Toggle,
            TuiAction::AddColumn,
            TuiAction::NewBoard,
            TuiAction::Delete,
            TuiAction::Close,
        ],
        InputContext::SettingsPrompt => &[TuiAction::Submit, TuiAction::Cancel],
        InputContext::PlanningManagement => &[
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::Next,
            TuiAction::NewPlanningItem,
            TuiAction::EditPlanningItem,
            TuiAction::DeletePlanningItem,
            TuiAction::Close,
        ],
        InputContext::DatePicker => &[
            TuiAction::MoveUp,
            TuiAction::MoveDown,
            TuiAction::MoveLeft,
            TuiAction::MoveRight,
            TuiAction::PreviousMonth,
            TuiAction::NextMonth,
            TuiAction::PreviousYear,
            TuiAction::NextYear,
            TuiAction::Submit,
            TuiAction::Reset,
            TuiAction::Cancel,
        ],
        InputContext::EditWarning => &[TuiAction::Reedit, TuiAction::Ignore],
    }
}

fn truncate(value: &str, max_chars: usize) -> String {
    if max_chars == 0 || value.chars().count() <= max_chars {
        return value.to_string();
    }
    let take = max_chars.saturating_sub(1);
    let head: String = value.chars().take(take).collect();
    format!("{}…", head.trim_end())
}

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

    /// Every action a documented context binds must appear in the overlay.
    ///
    /// `all_actions` is written by hand per context, so a binding can be added to the
    /// keymap and never appear in the help — the worst of both worlds, because the key
    /// works and nobody can discover it. It had already happened twice before this
    /// guard existed (`clone_issue` and `toggle_archived`, both added while this list
    /// was not).
    #[test]
    fn every_bound_action_appears_in_the_overlay() {
        let bindings =
            KeyBindings::resolve(&KeyBindings::default()).expect("built-in bindings are valid");
        let mut missing: Vec<String> = Vec::new();
        for context in HELP_CONTEXTS {
            let listed = all_actions(context);
            for action in bindings.bound_actions(context) {
                if !listed.contains(&action) {
                    missing.push(format!("{} — {}", context.as_str(), action.as_str()));
                }
            }
        }
        assert!(
            missing.is_empty(),
            "the overlay does not list these bound actions, so nothing in the help \
             would tell a user they exist:\n  {}",
            missing.join("\n  ")
        );
    }

    /// The guard is only as good as its context list: a context the overlay documents
    /// but the guard does not walk is a surface whose keys can go missing silently.
    #[test]
    fn the_overlay_and_its_guard_cover_the_same_contexts() {
        let bindings =
            KeyBindings::resolve(&KeyBindings::default()).expect("built-in bindings are valid");
        let drawn = help_overlay(&bindings);
        for context in HELP_CONTEXTS {
            let scope = context.as_str();
            assert!(
                drawn.iter().any(|(_, _, listed)| listed == scope),
                "`{scope}` is in HELP_CONTEXTS but contributes no rows to the overlay"
            );
        }
    }

    #[test]
    fn dynamic_hint_uses_effective_aliases() {
        let mut overrides = KeyBindings::default();
        overrides.board.new_issue = Some(vec!["ctrl+n".into(), "f4".into()]);
        overrides.board.help = Some(vec!["f1".into()]);
        let bindings = KeyBindings::resolve(&overrides).unwrap();
        let hint = hint_bar_for(&bindings, InputContext::Board);
        assert!(hint.contains("Ctrl+n / F4 new issue"), "got {hint:?}");
        assert!(hint.contains("F1 help"), "got {hint:?}");
    }

    #[test]
    fn help_covers_every_configured_action() {
        let bindings = KeyBindings::resolve(&KeyBindings::default()).unwrap();
        let rows = help_overlay(&bindings);
        for context in InputContext::ALL {
            for action in all_actions(*context) {
                assert!(
                    rows.iter()
                        .any(|(_, description, mode)| description == action.description()
                            && mode == context.as_str())
                );
            }
        }
    }
}