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
#[allow(clippy::wildcard_imports)]
use super::*;

impl App {
    /// Open the detail pane on the issue a shared issue-table row names
    /// ([`Action::OpenIssue`]).
    ///
    /// The row's filename is store-relative, exactly what
    /// [`IssueSelector::Filename`] takes; a file that has vanished since the
    /// draw surfaces as an `Err` the caller reports through the status line
    /// rather than opening an empty detail.
    pub fn open_issue_by_filename(&mut self, filename: &str) -> Result<()> {
        let issue = self
            .store
            .get(IssueSelector::Filename(filename.to_string()))?;
        self.open_detail_root(issue)
    }

    /// Open `issue`'s detail pane as a **fresh root**: the back stack is cleared,
    /// because the navigation began at the board rather than from another pane
    /// (tix #1513).
    pub(in crate::app) fn open_detail_root(&mut self, issue: Issue) -> Result<()> {
        // The pane is about to describe one issue's health, so the document it reads
        // from is re-read here: a value another process recorded a moment ago is what
        // the reader expects to see, not what was on disk when the board was built.
        self.refresh_health();
        self.nav_stack.clear();
        self.related_picker = None;
        let number = issue.number;
        self.detail = Some(self.detail_state_for(issue)?);
        self.mode = Mode::Detail;
        // Design D7: opening an issue is the moment a pending mention is surfaced.
        // The pane is already the detail view, so dismissing the notice returns to
        // it and the mention it pointed at is behind it.
        self.notice_mentions_for(number);
        Ok(())
    }

    /// Open the issue numbered `number` in the detail pane, if it still exists.
    pub(in crate::app) fn open_issue_by_number(&mut self, number: u32) -> Result<()> {
        let issue = self.store.get(IssueSelector::Number(number))?;
        self.open_detail_root(issue)
    }

    /// Build a detail pane for a **store** issue, service-resolved.
    ///
    /// One builder for every path that opens a store issue (the board's Enter, a
    /// double-click, and following a local related entry), so the `related` list can
    /// never be populated on one path and forgotten on another. The pane is
    /// writable: a store issue has a file to route writes through.
    pub(in crate::app) fn detail_state_for(&self, issue: Issue) -> Result<DetailState> {
        // Service-resolved, so the pane shows the relationships that touch this
        // issue rather than the ones it happens to own.
        let relationships = self.store.relationships_for(issue.number)?;
        // One graph for the chain and the blocked note, so the two rows cannot
        // describe different snapshots of the same issue.
        let (all, done_column) = self.store.graph_source()?;
        let graph = commands::graph_over(&all, &done_column);
        let number = issue.number;
        // WP05: the resolved category, for the detail row (T023).
        let category = self.category_of_issue(&issue);
        // #1513: classified once, here, so the renderer stays a pure function of
        // the pane's state.
        let related = crate::related::classify_all(&issue.related_issues);
        let wip = self.wip_line(&issue);
        // The scope's health, from the same document the CLI renders. `None` when the
        // document has no entry for the issue, which the pane renders as no block at
        // all rather than as an empty one.
        let scope = crate::health::HealthScope::of_issue(issue.number);
        let health = self
            .health
            .as_ref()
            .and_then(|document| document.scope(&scope).cloned());
        let health_diagnostics = self
            .health
            .as_ref()
            .map(|document| {
                document
                    .diagnostics_for(&scope)
                    .into_iter()
                    .map(|diagnostic| diagnostic.to_diagnostic())
                    .collect()
            })
            .unwrap_or_default();
        // WP04 T019: the issue's referenced epics, from the one service read the CLI
        // epic block uses, so the pane and `tix show` cannot disagree about an epic
        // (C-008). The snapshot already read above is reused rather than re-walked.
        // Empty for an issue in no epic, which the pane draws no heading for.
        let epics =
            commands::view::epic_entries_over(&self.store.config, &all, &done_column, &issue)?;
        // WP06 T031: the capacity scopes this issue's own references name, from the
        // shared calculation read at the last refresh — never a per-open disk read.
        let capacity = self.issue_capacity(&issue);
        let mut state = DetailState {
            health,
            health_diagnostics,
            issue,
            wip,
            category,
            relationships,
            chain: graph.dependency_chain(number),
            blocked: commands::blocked_note(&graph.blocked_state(number)),
            // From the **same** `all` snapshot the graph was built over, so the
            // hierarchy rows and the relationship rows cannot describe different
            // states of the same issue.
            hierarchy: commands::parent::facts_over(&all, &done_column, number),
            scroll_offset: std::cell::Cell::new(0),
            body_start: std::cell::Cell::new(0),
            mention_body: None,
            focus_pending: std::cell::Cell::new(false),
            viewport: std::cell::Cell::new(0),
            max_scroll: std::cell::Cell::new(0),
            rel_cursor: 0,
            related,
            related_cursor: 0,
            read_only: false,
            epics,
            capacity,
        };
        state.clamp_related_cursor();
        Ok(state)
    }

    /// Open an issue parsed from a file **outside** the store in the pane,
    /// read-only (tix #1513).
    ///
    /// There is no store entry to resolve relationships or a dependency graph
    /// against, so the pane carries the issue and its `related` list and nothing
    /// derived from the graph. `read_only` makes `e`/`c`/`a` and the relationship
    /// operations refuse rather than fail with a lookup error.
    pub(in crate::app) fn open_external_detail(&mut self, issue: Issue) {
        let number = issue.number;
        let related = crate::related::classify_all(&issue.related_issues);
        // No store snapshot to compute hierarchy over beyond the issue itself.
        let hierarchy = commands::parent::facts_over(std::slice::from_ref(&issue), "done", number);
        let wip = self.wip_line(&issue);
        let mut state = DetailState {
            health: None,
            health_diagnostics: Vec::new(),
            issue,
            wip,
            category: None,
            relationships: Vec::new(),
            chain: crate::relationships::DependencyChain::default(),
            blocked: None,
            hierarchy,
            scroll_offset: std::cell::Cell::new(0),
            body_start: std::cell::Cell::new(0),
            mention_body: None,
            focus_pending: std::cell::Cell::new(false),
            viewport: std::cell::Cell::new(0),
            max_scroll: std::cell::Cell::new(0),
            rel_cursor: 0,
            related,
            related_cursor: 0,
            read_only: true,
            // An external file has no store, so no epic service read is available; the
            // pane shows no epic block rather than a stale one.
            epics: Vec::new(),
            // And no capacity read either: the pane is about a file, not a store
            // issue, so it shows no capacity block rather than a stale one.
            capacity: Vec::new(),
        };
        state.clamp_related_cursor();
        self.detail = Some(state);
        self.mode = Mode::Detail;
    }

    #[allow(clippy::too_many_lines)]
    pub(in crate::app) fn handle_detail_key(&mut self, key: KeyEvent) -> Result<Action> {
        // The related picker overlays the pane (#1513), exactly as `form_picker`
        // overlays the form: while it is open it owns the keyboard.
        if self.related_picker.is_some() {
            return self.handle_related_picker_key(key);
        }
        // An external, read-only issue has no store to route a write through, so
        // the mutating keys refuse up front rather than failing with a lookup
        // error from the store (#1513).
        if self.detail.as_ref().is_some_and(|detail| detail.read_only)
            && matches!(key.code, KeyCode::Char('e' | 'c' | 'a' | 'n' | 'E' | 'd'))
        {
            self.status_msg = Some("read-only: the issue is outside the store".to_string());
            return Ok(Action::None);
        }
        // Mission `project-issue-health-status-01M3C8HX` (WP06): the health form, on
        // the issue the pane is showing. Handled here, in the raw-key style the rest of
        // this handler follows; the `[detail]` entry exists so the key is
        // listed in the help overlay like every other.
        if key.code == KeyCode::Char('H') {
            return self.open_health_form();
        }
        match key.code {
            KeyCode::Char('q') | KeyCode::Char('Q') | KeyCode::Esc => {
                self.mode = Mode::Board;
                self.detail = None;
                // The navigation that ended here is over with it; a back stack left
                // behind would let a later pane `Backspace` into a screen the reader
                // had already closed (#1513).
                self.nav_stack.clear();
                self.related_picker = None;
                Ok(Action::None)
            }
            KeyCode::Char('e') => {
                if let Some(ref d) = self.detail {
                    let filename = d.issue.filename.clone();
                    Ok(Action::OpenEditor(filename))
                } else {
                    Ok(Action::None)
                }
            }
            KeyCode::Char('v') => {
                if let Some(d) = &self.detail {
                    Ok(Action::OpenViewer(d.issue.filename.clone()))
                } else {
                    Ok(Action::None)
                }
            }
            KeyCode::PageUp => {
                if let Some(d) = self.detail.as_mut() {
                    let page = d.page();
                    d.scroll_by(-i32::from(page));
                }
                Ok(Action::None)
            }
            KeyCode::PageDown => {
                if let Some(d) = self.detail.as_mut() {
                    let page = d.page();
                    d.scroll_by(i32::from(page));
                }
                Ok(Action::None)
            }
            KeyCode::Char('?') => {
                self.open_help(Mode::Detail);
                Ok(Action::None)
            }
            KeyCode::Char('c') => {
                self.mode = Mode::Comment {
                    buffer: String::new(),
                };
                Ok(Action::None)
            }
            KeyCode::Char('a') => {
                self.mode = Mode::Asset {
                    buffer: String::new(),
                };
                Ok(Action::None)
            }
            // #1359: the three mutating relationship operations, plus the cursor
            // naming which edge `E` and `d` act on.
            KeyCode::Char('n') => {
                self.mode = Mode::LinkCreate {
                    buffer: String::new(),
                    step: LinkStep::Target,
                    target: None,
                };
                Ok(Action::None)
            }
            // #1384: set, change or clear the open issue's parent. `P` was free in
            // the detail pane; the write goes through the shared hierarchy service,
            // so the TUI gains the `tix edit --parent` parity the create-only form
            // could not express.
            KeyCode::Char('P') => {
                self.mode = Mode::ParentPrompt {
                    buffer: String::new(),
                };
                Ok(Action::None)
            }
            KeyCode::Char('E') => {
                // Only meaningful with an edge under the cursor; with none selected
                // the key says so rather than doing nothing, because a silently
                // ignored key is indistinguishable from a missing binding.
                match self.selected_relationship_id() {
                    Some(id) => {
                        let index = self.detail.as_ref().map(|d| d.rel_cursor).unwrap_or(0);
                        self.mode = Mode::LinkEdit {
                            index,
                            buffer: String::new(),
                            step: LinkStep::Target,
                            target: None,
                            kind: None,
                        };
                        self.status_msg = Some(format!("Re-kind {id}"));
                    }
                    None => {
                        self.status_msg = Some(
                            "no relationship selected — press Tab or Shift+Tab first".to_string(),
                        );
                    }
                }
                Ok(Action::None)
            }
            KeyCode::Char('d') => {
                match self.selected_relationship().cloned() {
                    Some(rel) => {
                        let line = crate::commands::link::relationship_line(&rel);
                        let index = self.detail.as_ref().map(|d| d.rel_cursor).unwrap_or(0);
                        self.status_msg = Some(format!("Remove {line}? (y to confirm)"));
                        self.mode = Mode::ConfirmRemoveRelationship { index };
                    }
                    None => {
                        self.status_msg = Some(
                            "no relationship selected — press Tab or Shift+Tab first".to_string(),
                        );
                    }
                }
                Ok(Action::None)
            }
            // `j`/`k` (and the arrows) scroll the **body**, one row per press; `g`/`G`
            // jump to the top and bottom. These arrive as their canonical events
            // (`Down`/`Up`/`g`/`G`) whatever the user rebound them to. The
            // relationship cursor moved to `Tab`/`BackTab`, so scrolling no longer
            // collides with selecting an edge.
            KeyCode::Down => {
                if let Some(d) = self.detail.as_mut() {
                    d.scroll_by(1);
                }
                Ok(Action::None)
            }
            KeyCode::Up => {
                if let Some(d) = self.detail.as_mut() {
                    d.scroll_by(-1);
                }
                Ok(Action::None)
            }
            KeyCode::Char('g') => {
                if let Some(d) = self.detail.as_mut() {
                    d.scroll_by(-i32::from(d.scroll_offset.get()));
                }
                Ok(Action::None)
            }
            KeyCode::Char('G') => {
                if let Some(d) = self.detail.as_mut() {
                    d.scroll_by(i32::from(d.max_scroll.get()));
                }
                Ok(Action::None)
            }
            KeyCode::Tab => {
                self.move_rel_cursor(1);
                Ok(Action::None)
            }
            KeyCode::BackTab => {
                self.move_rel_cursor(-1);
                Ok(Action::None)
            }
            // #1513: the related-issues surface. `r` lists them, `o` follows the
            // selected one, `Backspace` returns to the previous pane. `j`/`k` are
            // deliberately **not** reused: they move the relationship cursor, and
            // the picker is the selection surface for related entries.
            KeyCode::Char('r') => {
                self.open_related_picker();
                Ok(Action::None)
            }
            KeyCode::Char('o') => self.follow_related(),
            KeyCode::Backspace => {
                self.nav_back();
                Ok(Action::None)
            }
            _ => Ok(Action::None),
        }
    }

    /// Open the related-issues picker over the detail pane (tix #1513).
    ///
    /// A no-op with nothing to pick, so `r` on an issue with no `related_issues`
    /// does not open an empty box.
    pub(in crate::app) fn open_related_picker(&mut self) {
        let Some(detail) = self.detail.as_ref() else {
            return;
        };
        if detail.related.is_empty() {
            self.status_msg = Some("no related issues".to_string());
            return;
        }
        let options: Vec<PickerItem> = detail
            .related
            .iter()
            .enumerate()
            .map(|(index, entry)| PickerItem {
                id: index.to_string(),
                label: entry.display(),
            })
            .collect();
        let mut state = DropdownState::single(options);
        // Start on the entry `o` would follow, so the picker opens where the
        // reader already is.
        state.cursor = detail
            .related_cursor
            .min(state.visible_indices().len().saturating_sub(1));
        self.related_picker = Some(state);
    }

    /// Keyboard routing for the open related picker (tix #1513): the picker owns
    /// the keys while it is open, exactly as `handle_form_picker_key` does for a
    /// form dropdown. Canonical events arrive (`Up`/`Down`/`Enter`/`Esc`) because
    /// `InputContext::RelatedPicker` binds `next`/`previous`/`submit`/`cancel`.
    pub(in crate::app) fn handle_related_picker_key(&mut self, key: KeyEvent) -> Result<Action> {
        let event = match self.related_picker.as_mut() {
            Some(picker) => picker.handle_key(key.code),
            None => return Ok(Action::None),
        };
        match event {
            PickerEvent::None => {}
            PickerEvent::Cancelled => {
                self.related_picker = None;
            }
            PickerEvent::Selected(ids) => {
                self.related_picker = None;
                if let Some(id) = ids.first().and_then(|id| id.parse::<usize>().ok()) {
                    if let Some(detail) = self.detail.as_mut() {
                        detail.related_cursor = id;
                        detail.clamp_related_cursor();
                    }
                    // Selecting an entry means "go there", so the picker is a
                    // navigation surface rather than a second selection step.
                    return self.follow_related();
                }
            }
            // The picker has no inline create; a `Created` event cannot arise from
            // the keys bound in its context, and ignoring it is honest.
            PickerEvent::Created(_) => {}
        }
        Ok(Action::None)
    }

    /// Follow the detail pane's selected related entry (tix #1513).
    ///
    /// Three shapes, matching [`crate::related::RelatedRef`]:
    /// - `Local` resolves through the store and opens the issue in the pane;
    /// - `Path` opens the parsed file read-only (it may be outside the store), or
    ///   hands the path to the resolved viewer when it does not parse as an issue;
    /// - `Url` launches the system browser, and falls back to showing the URL so it
    ///   can be copied.
    ///
    /// Every failure is a status message, never an `Err`: a broken link in one issue
    /// must not end the session.
    pub(in crate::app) fn follow_related(&mut self) -> Result<Action> {
        let Some(detail) = self.detail.as_ref() else {
            return Ok(Action::None);
        };
        let Some(entry) = detail.related.get(detail.related_cursor).cloned() else {
            // Nothing to follow, and saying so is better than a key that silently
            // does nothing (#1513).
            self.status_msg = Some("no related issues".to_string());
            return Ok(Action::None);
        };
        let base = crate::related::base_dir(&self.store.issues_dir, &detail.issue.filename);
        match entry {
            crate::related::RelatedRef::Local(raw) => {
                match crate::related::resolve_local(&self.store, &raw) {
                    // Built **before** the snapshot is pushed, so a store failure
                    // leaves the stack and the pane untouched and reports instead of
                    // ending the session — the same rule the path and URL branches
                    // follow.
                    Ok(issue) => match self.detail_state_for(issue) {
                        Ok(state) => {
                            self.push_detail_snapshot();
                            self.detail = Some(state);
                            self.mode = Mode::Detail;
                        }
                        Err(e) => self.status_msg = Some(format!("related {raw}: {e}")),
                    },
                    Err(e) => self.status_msg = Some(format!("related {raw}: {e}")),
                }
            }
            crate::related::RelatedRef::Path { .. } => return self.follow_path(&base, &entry),
            crate::related::RelatedRef::Url { .. } => {
                let url = entry.display();
                match open_in_browser(&url) {
                    Ok(()) => self.status_msg = Some(format!("opened {url}")),
                    // Not an error the app can act on: the URL is shown so it can
                    // be copied into a browser by hand.
                    Err(e) => self.status_msg = Some(format!("{url} (could not open: {e})")),
                }
            }
        }
        Ok(Action::None)
    }

    /// Follow a `Path` entry (tix #1513).
    ///
    /// A path that lands **inside** the store is opened as an ordinary issue
    /// through the service (it is not "external" just because it was written as a
    /// path); anything else is opened read-only when it parses as an issue, or
    /// handed to the read-only viewer when it does not — the viewer needs the
    /// terminal, so it travels back as an [`Action`].
    pub(in crate::app) fn follow_path(
        &mut self,
        base: &Path,
        entry: &crate::related::RelatedRef,
    ) -> Result<Action> {
        let Some(path) = crate::related::resolve_path(base, entry) else {
            return Ok(Action::None);
        };
        if let Ok(relative) = path.strip_prefix(&self.store.issues_dir) {
            let name = relative.to_string_lossy().to_string();
            if let Ok(issue) = self.store.get(IssueSelector::Filename(name)) {
                match self.detail_state_for(issue) {
                    Ok(state) => {
                        self.push_detail_snapshot();
                        self.detail = Some(state);
                        self.mode = Mode::Detail;
                    }
                    // A store failure is reported, not propagated: a link must not be
                    // able to end the session (#1513).
                    Err(e) => self.status_msg = Some(format!("related: {e}")),
                }
                return Ok(Action::None);
            }
        }
        match crate::related::read_issue_file(&path) {
            Ok(issue) => {
                self.push_detail_snapshot();
                self.open_external_detail(issue);
                Ok(Action::None)
            }
            // Not an issue file: hand it to the read-only viewer, which can page
            // anything. A viewer that cannot be resolved is a status message, not a
            // failed key.
            Err(_) => match crate::editor::resolve_viewer(self.store.config.viewer.as_deref()) {
                Ok(_viewer) => Ok(Action::OpenViewerAt(path)),
                Err(e) => {
                    self.status_msg = Some(format!("viewer: {e}"));
                    Ok(Action::None)
                }
            },
        }
    }

    /// Push the current pane onto the back stack, bounded (tix #1513).
    pub(in crate::app) fn push_detail_snapshot(&mut self) {
        let Some(current) = self.detail.as_ref() else {
            return;
        };
        if self.nav_stack.len() >= MAX_DETAIL_NAV {
            self.nav_stack.remove(0);
        }
        self.nav_stack.push(current.clone());
    }

    /// Pop the back stack and restore the previous pane (tix #1513).
    pub(in crate::app) fn nav_back(&mut self) {
        match self.nav_stack.pop() {
            Some(previous) => {
                self.detail = Some(previous);
                self.mode = Mode::Detail;
                self.related_picker = None;
            }
            // Said rather than swallowed: a back key that does nothing and says
            // nothing is indistinguishable from a missing binding.
            None => self.status_msg = Some("no previous issue".to_string()),
        }
    }
}