tix-tracker 0.12.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#[allow(clippy::wildcard_imports)]
use super::*;

impl App {
    /// Open the triage inbox (`t`).
    ///
    /// A refusal — no `[triage]` section in this project, or an I/O failure —
    /// is reported on the status line and **nothing else changes**: a project
    /// that does not use triage leaves the board exactly where it was, which is
    /// T029's "navigation is unchanged when the tab is not active" from the
    /// other side.
    pub(in crate::app) fn open_triage_inbox(&mut self) -> Result<Action> {
        match crate::ui::triage::TriageState::open(&self.store) {
            Ok(state) => {
                self.triage = Some(state);
                self.mode = Mode::TriageInbox;
            }
            Err(e) => self.status_msg = Some(e.to_string()),
        }
        Ok(Action::None)
    }

    /// The inbox's keys (WP06, T029–T032).
    ///
    /// Two layers, exactly as the batch modal has one: an open decision modal
    /// takes the key first, and the list has it otherwise. Every branch here
    /// either moves a cursor that belongs to *this* view or calls a shared
    /// service — no `IssuePatch`, no `store.update`, and no status compared
    /// against a literal. Cancellation drops the modal and changes nothing else;
    /// the queue is re-read only after an apply has actually run.
    #[allow(clippy::too_many_lines)]
    pub(in crate::app) fn handle_triage_key(&mut self, key: KeyEvent) -> Result<Action> {
        use crate::ui::triage::ModalState;

        let Some(mut state) = self.triage.take() else {
            self.mode = Mode::Board;
            return Ok(Action::None);
        };

        let editing = state.modal.as_ref().is_some_and(|m| m.editing);
        let armed = state.modal.as_ref().is_some_and(|m| m.is_armed());
        let previewed = matches!(
            state.modal.as_ref().map(|m| &m.state),
            Some(ModalState::Preview(_))
        );
        let mut closed = false;
        let mut chosen: Option<(TriageDropdownKind, String)> = None;
        let mut create_title: Option<String> = None;
        let mut activate: Option<TriageControl> = None;

        // Priority: picker, then create prompt, then decision modal, then list.
        // A picker and the create prompt can never coexist with a modal, so a
        // letter typed into a decline reason is never stolen (tui-triage-mode).
        if state.dropdown.is_some() {
            if let Some(choice) = state.handle_dropdown_key(key.code) {
                chosen = Some(choice);
            }
        } else if state.creating {
            match key.code {
                KeyCode::Esc => {
                    state.creating = false;
                    state.create_buffer.clear();
                }
                KeyCode::Enter => {
                    let title = state.create_buffer.trim().to_string();
                    state.creating = false;
                    state.create_buffer.clear();
                    if title.is_empty() {
                        state.note = Some("a title is required".to_string());
                    } else {
                        create_title = Some(title);
                    }
                }
                KeyCode::Backspace => {
                    state.create_buffer.pop();
                }
                KeyCode::Char(c) => state.create_buffer.push(c),
                _ => {}
            }
        } else if state.modal.is_some() {
            match key.code {
                // `esc` while a field is being typed reverts the draft rather
                // than closing — the footer says "esc revert", and a revert
                // that closed the modal would take the row it reverted with it.
                KeyCode::Esc if editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.abort_edit();
                    }
                }
                // Cancels from every state: the plan and the draft go, the
                // committed fields stay, and no file moved (T032). The report
                // goes with the user when it is a refusal — a refusal nobody
                // can re-read after pressing `esc` is not a report.
                KeyCode::Esc => {
                    let carried = match state.modal.as_ref().map(|m| &m.state) {
                        Some(ModalState::Done(text)) | Some(ModalState::Refused(text)) => {
                            Some(text.lines().next().unwrap_or(text.as_str()).to_string())
                        }
                        _ => None,
                    };
                    state.close_modal();
                    if carried.is_some() {
                        state.note = carried;
                    }
                }
                KeyCode::Enter if editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.commit_edit();
                    }
                }
                KeyCode::Enter => {
                    let ok_focused = state.modal.as_ref().is_some_and(|modal| modal.ok_focused);
                    if ok_focused {
                        state.modal_ok(&self.store)?;
                    } else if state.modal.as_ref().is_some_and(|modal| {
                        matches!(&modal.state, ModalState::Editing)
                            && modal.field(modal.cursor) == crate::ui::triage::DecisionField::Target
                    }) {
                        let current = state
                            .modal
                            .as_ref()
                            .map(|modal| modal.target.trim().to_string())
                            .unwrap_or_default();
                        let columns = self.active_board_config().columns.clone();
                        state.open_dropdown(
                            TriageDropdownKind::DecisionTarget,
                            crate::ui::triage::status_options(&columns),
                            (!current.is_empty()).then_some(current.as_str()),
                        );
                    } else if let Some(modal) = state.modal.as_mut() {
                        modal.begin_edit();
                    }
                }
                KeyCode::Backspace if editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.pop_char();
                    }
                }
                KeyCode::Char(c) if editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.push_char(c);
                    }
                }
                // The cursor does not move while a field is being typed:
                // `commit_edit` writes the draft to the row the cursor is on,
                // so a cursor that could travel mid-edit would file half a
                // reason under the wrong label.
                KeyCode::Tab | KeyCode::Down | KeyCode::Char('j') if !editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.move_focus(1);
                    }
                }
                KeyCode::BackTab | KeyCode::Up | KeyCode::Char('k') if !editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.move_focus(-1);
                    }
                }
                // The target row, when the decision has one — the same `t` the
                // batch modal gives its pair target.
                KeyCode::Char('t') if !editing => {
                    if let Some(modal) = state.modal.as_mut() {
                        let wanted = if modal.kind == crate::triage::DecisionKind::Accept {
                            crate::ui::triage::DecisionField::Target
                        } else {
                            crate::ui::triage::DecisionField::DuplicateOf
                        };
                        if let Some(index) = modal.fields.iter().position(|f| *f == wanted) {
                            modal.cursor = index;
                            modal.ok_focused = false;
                        }
                    }
                }
                // Cycle the verb. The selection and the already-typed fields
                // survive it, for the reason the batch modal's `a` keeps the
                // selection: a user who marked ten does not re-mark them to
                // change their mind about what to do with them.
                KeyCode::Char('a') => {
                    if let Some(modal) = state.modal.as_mut() {
                        modal.cycle_kind();
                    }
                }
                KeyCode::Char('p') => state.preview(&self.store)?,
                KeyCode::Char('y') => {
                    if armed || previewed {
                        state.confirm(&self.store)?;
                    } else {
                        state.note = Some("press `p` to preview first".to_string());
                    }
                }
                _ => {}
            }
        } else {
            match key.code {
                KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => closed = true,
                KeyCode::Up | KeyCode::Char('k') => state.move_cursor(-1),
                KeyCode::Down | KeyCode::Char('j') => state.move_cursor(1),
                KeyCode::Char(' ') => state.toggle_current(),
                // Oldest first, the TUI's `--age`. Toggling re-reads the queue
                // rather than re-sorting a snapshot, so the ages cannot be
                // computed against two different `today`s.
                KeyCode::Char('A') => {
                    if state.rows.is_empty() {
                        self.action_feedback = Some(super::ActionFeedback::new(
                            "sort triage by age",
                            "the triage inbox has no issues to sort",
                            "press n to create one in the entry column",
                        ));
                    }
                    state.by_age = !state.by_age;
                    state.refresh(&self.store)?;
                }
                KeyCode::Char('a') => {
                    let columns = self.active_board_config().columns.clone();
                    state.open_action(crate::triage::DecisionKind::Accept, &self.store, &columns);
                }
                KeyCode::Char('d') => {
                    let columns = self.active_board_config().columns.clone();
                    state.open_action(crate::triage::DecisionKind::Decline, &self.store, &columns);
                }
                KeyCode::Char('u') => {
                    let columns = self.active_board_config().columns.clone();
                    state.open_action(
                        crate::triage::DecisionKind::Duplicate,
                        &self.store,
                        &columns,
                    );
                }
                KeyCode::Char('f') => {
                    let columns = self.active_board_config().columns.clone();
                    state.open_action(crate::triage::DecisionKind::Defer, &self.store, &columns);
                }
                // The batch modal over everything marked; `a` inside it picks
                // the decision, exactly as the batch modal's `a` picks its
                // action.
                KeyCode::Char('p') => {
                    let columns = self.active_board_config().columns.clone();
                    state.open_batch(crate::triage::DecisionKind::Accept, &self.store, &columns);
                }
                // The status picker: exactly the active board's columns. The
                // direct key moves the focus onto the control it opened, the
                // same as a click or `enter` does.
                KeyCode::Char('s') => {
                    let columns = self.active_board_config().columns.clone();
                    let options = crate::ui::triage::status_options(&columns);
                    let current = state
                        .current()
                        .map(|row| row.issue.status.display().to_string());
                    state.focus = TriageControl::Status;
                    state.open_dropdown(TriageDropdownKind::Status, options, current.as_deref());
                }
                // The assignee picker: storage folders + team members + current.
                KeyCode::Char('@') => {
                    let current = state.current().and_then(|row| row.issue.assignee.clone());
                    let options =
                        crate::ui::triage::assignee_options(&self.store, current.as_deref());
                    state.focus = TriageControl::Assignee;
                    state.open_dropdown(TriageDropdownKind::Assignee, options, current.as_deref());
                }
                // Create an issue straight into the inbox's entry column.
                KeyCode::Char('n') => {
                    state.focus = TriageControl::New;
                    state.creating = true;
                    state.create_buffer.clear();
                }
                // The control bar's own axis (tui-triage-mode). `h`/`l` are
                // **raw** keys, never bindings in this context: the inbox is a
                // text-entry context (`allows_text_fallback`), so a binding
                // would rewrite those letters while a decline reason or a
                // duplicate target is being typed. This branch is reached only
                // with no modal, picker, or prompt open — the same "nothing is
                // being typed" state `!editing` names in the modal branch.
                KeyCode::Left | KeyCode::Char('h') => state.move_focus(-1),
                KeyCode::Right | KeyCode::Char('l') => state.move_focus(1),
                // `enter` activates the focused control. On `queue` it is the
                // no-op it has always been: the queue's keys act on the list
                // directly and need no activation step.
                KeyCode::Enter => activate = Some(state.focus),
                _ => {}
            }
        }

        if let Some(title) = create_title {
            self.triage = Some(state);
            self.create_triage_issue(&title)?;
            return Ok(Action::None);
        }
        if let Some((kind, id)) = chosen {
            self.triage = Some(state);
            match kind {
                TriageDropdownKind::Status => self.move_focused_triage_issue(&id)?,
                TriageDropdownKind::DecisionTarget => {
                    if let Some(state) = self.triage.as_mut()
                        && let Some(modal) = state.modal.as_mut()
                    {
                        modal.choose_target(id);
                    }
                }
                TriageDropdownKind::Assignee => self.assign_focused_triage_issue(&id)?,
            }
            return Ok(Action::None);
        }

        if closed {
            // The note goes to the status line rather than being dropped with
            // the view — the batch modal's handler does the same thing for the
            // same reason.
            let note = state.note.clone();
            self.triage = None;
            self.mode = Mode::Board;
            self.refresh_corpus()?;
            if note.is_some() {
                self.status_msg = note;
            }
            return Ok(Action::None);
        }

        self.triage = Some(state);
        self.mode = Mode::TriageInbox;
        // `enter` on the focused control runs the very same path a click on it
        // does, so the two can never drift.
        if let Some(control) = activate {
            match control {
                TriageControl::Queue => {}
                TriageControl::Status => self.open_triage_status_dropdown(),
                TriageControl::Assignee => self.open_triage_assignee_dropdown(),
                TriageControl::New => {
                    self.apply_triage_action(Action::TriageCreate)?;
                }
            }
            return Ok(Action::None);
        }
        Ok(Action::None)
    }

    /// Open the status picker: exactly the active board's columns, in board
    /// order, with the focused issue's current column preselected. The status
    /// control becomes the focused one, so `enter` reopens what a click opened
    /// (tui-triage-mode).
    pub(in crate::app) fn open_triage_status_dropdown(&mut self) {
        let columns = self.active_board_config().columns.clone();
        let options = crate::ui::triage::status_options(&columns);
        if let Some(state) = self.triage.as_mut() {
            state.focus = TriageControl::Status;
            let current = state
                .current()
                .map(|row| row.issue.status.display().to_string());
            state.open_dropdown(TriageDropdownKind::Status, options, current.as_deref());
        }
    }

    /// Open the assignee picker: `docs/issues/<username>/` folders, configured
    /// team members, and the focused issue's current value (design D6). The
    /// assignee control becomes the focused one.
    pub(in crate::app) fn open_triage_assignee_dropdown(&mut self) {
        let current = self
            .triage
            .as_ref()
            .and_then(|state| state.current())
            .and_then(|row| row.issue.assignee.clone());
        let options = crate::ui::triage::assignee_options(&self.store, current.as_deref());
        if let Some(state) = self.triage.as_mut() {
            state.focus = TriageControl::Assignee;
            state.open_dropdown(TriageDropdownKind::Assignee, options, current.as_deref());
        }
    }

    /// Move the focused triage issue to `target` through the **shared** move
    /// path, so the blocked-move and terminal-transition guards still apply. A
    /// refusal (an unacknowledged blocked move, a terminal exit without a
    /// reason) is reported on the mode's note rather than bypassed.
    pub(in crate::app) fn move_focused_triage_issue(&mut self, target: &str) -> Result<()> {
        let Some(number) = self
            .triage
            .as_ref()
            .and_then(|state| state.current())
            .map(|row| row.issue.number)
        else {
            return Ok(());
        };
        // WP05 T023/T024: the inbox's move is a move like any other, so it asks the
        // same policy. There is no room for a modal in the inbox, so a blocking
        // decision is reported on the inbox's note and the move does not happen —
        // the same rule the acknowledgement below follows: nothing is written
        // without the decision it requires.
        let wip_event = match self.wip_decision(number, target, None, None) {
            Some(decision) if !decision.is_committable() => {
                if let Some(state) = self.triage.as_mut() {
                    state.note = Some(crate::wip::render_decision(&decision));
                }
                return Ok(());
            }
            Some(decision) => decision.history,
            None => None,
        };
        match crate::commands::mv::apply_guarded(
            &self.store.config,
            &self.store,
            &number.to_string(),
            target,
            None,
            None,
            // No acknowledgement is offered inline: a blocked move stays blocked
            // rather than being written without the decision it requires.
            false,
            wip_event.as_ref(),
        ) {
            Ok(moved) => {
                if let Some(state) = self.triage.as_mut() {
                    state.refresh(&self.store)?;
                    state.note = Some(format!("Moved #{} to {target}", moved.issue.number));
                }
                self.refresh_corpus()?;
            }
            Err(e) => {
                if let Some(state) = self.triage.as_mut() {
                    state.note = Some(e.to_string());
                }
            }
        }
        Ok(())
    }

    /// Set the focused triage issue's assignee through `tix edit --assignee`'s
    /// own core, so the field and the file relocation are exactly the CLI's.
    pub(in crate::app) fn assign_focused_triage_issue(&mut self, assignee: &str) -> Result<()> {
        let Some(number) = self
            .triage
            .as_ref()
            .and_then(|state| state.current())
            .map(|row| row.issue.number)
        else {
            return Ok(());
        };
        match crate::commands::edit::apply(
            &self.store.config,
            &self.store,
            &number.to_string(),
            None,
            None,
            None,
            None,
            Some(assignee),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        ) {
            Ok(issue) => {
                if let Some(state) = self.triage.as_mut() {
                    state.refresh(&self.store)?;
                    state.note = Some(format!("Assigned #{} to {assignee}", issue.number));
                }
                self.refresh_corpus()?;
            }
            Err(e) => {
                if let Some(state) = self.triage.as_mut() {
                    state.note = Some(e.to_string());
                }
            }
        }
        Ok(())
    }

    /// Create an issue from inside the Triage mode, straight into the
    /// configured entry column, through the shared create core (so the entry
    /// stamp is recorded and it appears in the queue).
    pub(in crate::app) fn create_triage_issue(&mut self, title: &str) -> Result<()> {
        let entry = self
            .store
            .config
            .triage
            .as_ref()
            .and_then(|triage| triage.entry_status.clone())
            .ok_or_else(|| {
                TixError::InvalidInput(
                    "the triage inbox needs a `[triage]` section in tix.toml, and this project \
                     has none"
                        .to_string(),
                )
            })?;
        let issue = crate::commands::create::create(
            &self.store.config,
            &self.store,
            title,
            None,
            Some(crate::model::Status::new(entry.clone())),
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        )?;
        if let Some(state) = self.triage.as_mut() {
            state.refresh(&self.store)?;
            state.note = Some(format!("Created #{} in {entry}", issue.number));
        }
        self.refresh_corpus()?;
        Ok(())
    }

    /// Run a Triage-mode pointer action recorded during the draw. Public so the
    /// hit-region tests drive the same path the run loop does.
    pub fn apply_triage_action(&mut self, action: Action) -> Result<Action> {
        match action {
            Action::TriageOpenStatus => self.open_triage_status_dropdown(),
            Action::TriageOpenAssignee => self.open_triage_assignee_dropdown(),
            Action::TriageCreate => {
                if let Some(state) = self.triage.as_mut() {
                    state.dropdown = None;
                    state.focus = TriageControl::New;
                    state.creating = true;
                    state.create_buffer.clear();
                }
            }
            Action::TriageFocusQueue => {
                if let Some(state) = self.triage.as_mut() {
                    state.focus = TriageControl::Queue;
                }
            }
            Action::TriageDropdownClose => {
                if let Some(state) = self.triage.as_mut() {
                    state.close_dropdown();
                }
            }
            Action::TriageModalOk => {
                if let Some(state) = self.triage.as_mut() {
                    state.modal_ok(&self.store)?;
                }
            }
            Action::TriagePickOption(row) => {
                let chosen = self.triage.as_mut().and_then(|state| {
                    let (kind, id) = {
                        let dropdown = state.dropdown.as_ref()?;
                        let index = *dropdown.state.visible_indices().get(row)?;
                        (dropdown.kind, dropdown.state.options.get(index)?.id.clone())
                    };
                    state.dropdown = None;
                    Some((kind, id))
                });
                if let Some((kind, id)) = chosen {
                    match kind {
                        TriageDropdownKind::Status => self.move_focused_triage_issue(&id)?,
                        TriageDropdownKind::DecisionTarget => {
                            if let Some(state) = self.triage.as_mut()
                                && let Some(modal) = state.modal.as_mut()
                            {
                                modal.choose_target(id);
                            }
                        }
                        TriageDropdownKind::Assignee => self.assign_focused_triage_issue(&id)?,
                    }
                }
            }
            _ => {}
        }
        Ok(Action::None)
    }

    /// Open the decision modal for one issue directly, bypassing the inbox list
    /// but not its service. The row is located by number, then `open_single`
    /// builds the same plan the `a`/`d`/`u`/`f` keys build.
    pub(in crate::app) fn open_triage_decision(
        &mut self,
        number: u32,
        decision: crate::triage::DecisionKind,
    ) -> Result<Action> {
        let mut state = match crate::ui::triage::TriageState::open(&self.store) {
            Ok(state) => state,
            Err(error) => {
                self.status_msg = Some(error.to_string());
                return Ok(Action::None);
            }
        };
        let Some(index) = state.rows.iter().position(|row| row.issue.number == number) else {
            self.status_msg = Some(format!(
                "#{number} is not in the triage inbox (does it sit in the entry column?)"
            ));
            return Ok(Action::None);
        };
        state.cursor = index;
        let columns = self.active_board_config().columns.clone();
        if !state.open_single(decision, &self.store, &columns) {
            let note = state.note.clone();
            self.triage = Some(state);
            self.mode = Mode::TriageInbox;
            self.status_msg = note;
            return Ok(Action::None);
        }
        self.triage = Some(state);
        self.mode = Mode::TriageInbox;
        Ok(Action::None)
    }
}