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

impl App {
    pub(in crate::app) fn open_planning_modal(&mut self) {
        // Remember where the surface was opened from so Esc/q returns there:
        // the board's `m` comes back to the board, the Cycle mode's `m` comes
        // back to the Cycle mode it was showing (tui-cycle-mode).
        self.planning_return = self.mode.clone();
        let modal = self.planning.get_or_insert_with(PlanningModal::new);
        modal.prompt = None;
        modal.selected = 0;
        self.mode = Mode::PlanningManagement;
    }

    /// The rows the planning modal renders, read straight from the registry:
    /// cycles from `cycle_names()` + `cycle()`, milestones from
    /// `list_milestones()`, referencing issue counts from `store.list()`
    /// (same computation as `tix cycle list` / `milestone list`).
    pub(in crate::app) fn planning_rows(&self) -> (Vec<PlanningRow>, Vec<PlanningRow>) {
        let root = self.project_root();
        let planner = Planner::from_config(self.store.config.clone(), root);
        let issues = self.store.list().unwrap_or_default();

        let mut cycles = Vec::new();
        for name in planner.cycle_names() {
            let cycle = planner.cycle(&name);
            let issues = issues
                .iter()
                .filter(|issue| {
                    issue
                        .cycles
                        .as_ref()
                        .is_some_and(|cs| cs.iter().any(|c| c == &name))
                })
                .count();
            cycles.push(PlanningRow {
                name,
                description: cycle
                    .and_then(|c| c.description.clone())
                    .unwrap_or_default(),
                date_a: cycle.and_then(|c| c.start_date.clone()).unwrap_or_default(),
                date_b: Some(cycle.and_then(|c| c.due_date.clone()).unwrap_or_default()),
                issues,
            });
        }

        let mut milestones = Vec::new();
        if let Ok(list) = planner.list_milestones() {
            for ms in list {
                let issues = issues
                    .iter()
                    .filter(|issue| {
                        issue
                            .milestones
                            .as_ref()
                            .is_some_and(|ml| ml.iter().any(|x| x == &ms.name))
                    })
                    .count();
                milestones.push(PlanningRow {
                    name: ms.name,
                    description: ms.description,
                    date_a: ms.target_date,
                    date_b: None,
                    issues,
                });
            }
        }
        (cycles, milestones)
    }

    /// The number of rows in the modal's active segment (for cursor clamping).
    pub(in crate::app) fn planning_rows_len(&self) -> usize {
        let (cycles, milestones) = self.planning_rows();
        match self.planning.as_ref().map(|m| m.segment) {
            Some(PlanningSegment::Cycles) => cycles.len(),
            Some(PlanningSegment::Milestones) => milestones.len(),
            None => 0,
        }
    }

    /// Planning modal keys: browse the list (`j/k`/arrows, Tab segment switch,
    /// `n` create, `e` edit, `x` delete confirm); a `ConfirmDelete` prompt
    /// accepts `y`/Enter or cancels on `n`/Esc.
    pub(in crate::app) fn handle_planning_key(&mut self, key: KeyEvent) -> Result<Action> {
        if self.planning.is_none() {
            self.mode = self.planning_return.clone();
            return Ok(Action::None);
        }
        if let Some(prompt) = self.planning.as_ref().and_then(|m| m.prompt.clone()) {
            return self.handle_planning_confirm_key(key, prompt);
        }
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
                self.planning = None;
                self.mode = self.planning_return.clone();
                Ok(Action::None)
            }
            KeyCode::Up | KeyCode::Char('k') => {
                let len = self.planning_rows_len();
                if let Some(ref mut m) = self.planning {
                    m.move_selected(-1, len);
                }
                Ok(Action::None)
            }
            KeyCode::Down | KeyCode::Char('j') => {
                let len = self.planning_rows_len();
                if let Some(ref mut m) = self.planning {
                    m.move_selected(1, len);
                }
                Ok(Action::None)
            }
            KeyCode::Tab | KeyCode::BackTab => {
                if let Some(ref mut m) = self.planning {
                    m.switch_segment();
                }
                Ok(Action::None)
            }
            // The segments are the surface's horizontal axis; the movement keys
            // switch them, matching the Config section rail (h/j/k/l + arrows in
            // every mode where the axis exists).
            KeyCode::Left | KeyCode::Char('h') | KeyCode::Right | KeyCode::Char('l') => {
                if let Some(m) = &mut self.planning {
                    m.switch_segment();
                }
                Ok(Action::None)
            }
            KeyCode::Char('n') => {
                self.planning_open_create();
                Ok(Action::None)
            }
            KeyCode::Char('e') => self.planning_open_edit(),
            KeyCode::Char('x') | KeyCode::Char('X') => self.planning_confirm_delete(),
            _ => Ok(Action::None),
        }
    }

    /// Keys while a delete confirmation prompt is up inside the modal.
    pub(in crate::app) fn handle_planning_confirm_key(
        &mut self,
        key: KeyEvent,
        prompt: PlanningPrompt,
    ) -> Result<Action> {
        let PlanningPrompt::ConfirmDelete { name, .. } = prompt;
        match key.code {
            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => {
                match self.planning_delete(&name) {
                    Ok(()) => {
                        self.status_msg = Some(format!("Deleted {name}"));
                    }
                    Err(e) => {
                        self.status_msg = Some(format!("Cannot delete {name}: {e}"));
                    }
                }
                if let Some(ref mut m) = self.planning {
                    m.prompt = None;
                }
                Ok(Action::None)
            }
            _ => {
                if let Some(ref mut m) = self.planning {
                    m.prompt = None;
                }
                Ok(Action::None)
            }
        }
    }

    /// `n`: open a blank create form for the active segment.
    pub(in crate::app) fn planning_open_create(&mut self) {
        let kind = match self.planning.as_ref().map(|m| m.segment) {
            Some(PlanningSegment::Milestones) => PlanningKind::Milestone,
            _ => PlanningKind::Cycle,
        };
        self.planning_form = Some(PlanningForm::new(kind));
        self.mode = Mode::PlanningForm;
    }

    /// `e`: open an edit form pre-filled from the selected item.
    pub(in crate::app) fn planning_open_edit(&mut self) -> Result<Action> {
        let Some(modal) = self.planning.as_ref() else {
            return Ok(Action::None);
        };
        let root = self.project_root();
        let planner = Planner::from_config(self.store.config.clone(), root);
        let form = match modal.segment {
            PlanningSegment::Cycles => {
                let name = match planner.cycle_names().into_iter().nth(modal.selected) {
                    Some(name) => name,
                    None => return Ok(Action::None),
                };
                let Some(cycle) = planner.cycle(&name) else {
                    return Ok(Action::None);
                };
                PlanningForm::from_cycle(&name, cycle)
            }
            PlanningSegment::Milestones => {
                let list = planner.list_milestones().unwrap_or_default();
                let Some(ms) = list.into_iter().nth(modal.selected) else {
                    return Ok(Action::None);
                };
                PlanningForm::from_milestone(&ms)
            }
        };
        self.planning_form = Some(form);
        self.mode = Mode::PlanningForm;
        Ok(Action::None)
    }

    /// `x`: compute the delete impact for the selected item and raise the
    /// inline confirm prompt (spec: reference count shown, issues untouched).
    pub(in crate::app) fn planning_confirm_delete(&mut self) -> Result<Action> {
        let Some(modal) = self.planning.as_ref() else {
            return Ok(Action::None);
        };
        if modal.prompt.is_some() {
            return Ok(Action::None);
        }
        let root = self.project_root();
        let planner = Planner::from_config(self.store.config.clone(), root);
        let (name, referencing) = match modal.segment {
            PlanningSegment::Cycles => {
                let Some(name) = planner.cycle_names().into_iter().nth(modal.selected) else {
                    return Ok(Action::None);
                };
                let referencing = planner
                    .delete_impact(&self.store, &name)
                    .map(|i| i.referencing_issues.len())
                    .unwrap_or(0);
                (name, referencing)
            }
            PlanningSegment::Milestones => {
                let list = planner.list_milestones().unwrap_or_default();
                let Some(name) = list.into_iter().nth(modal.selected).map(|m| m.name) else {
                    return Ok(Action::None);
                };
                let referencing = planner
                    .milestone_delete_impact(&self.store, &name)
                    .map(|i| i.referencing_issues.len())
                    .unwrap_or(0);
                (name, referencing)
            }
        };
        if let Some(ref mut m) = self.planning {
            m.prompt = Some(PlanningPrompt::ConfirmDelete { name, referencing });
        }
        Ok(Action::None)
    }

    /// Confirm-delete an item, leaving referencing issues unchanged. Cycle
    /// deletion mutates `tix.toml`, so the store config is reloaded for
    /// pickers built from it.
    pub(in crate::app) fn planning_delete(&mut self, name: &str) -> Result<()> {
        let root = self.project_root();
        let mut planner = Planner::from_config(self.store.config.clone(), root.clone());
        match self.planning.as_ref().map(|m| m.segment) {
            Some(PlanningSegment::Cycles) => {
                planner.delete_cycle(name)?;
                self.reload_config_from(&root)?;
            }
            Some(PlanningSegment::Milestones) => {
                planner.delete_milestone(name)?;
            }
            None => {}
        }
        self.refresh_corpus()?;
        Ok(())
    }

    /// Where a planning form returns when it closes. A form opened from the
    /// Config mode's cycles/milestones sections returns there — the Config mode
    /// owns the surface — while the `m` planning surface keeps the modal behind
    /// the form, as before.
    pub(in crate::app) fn planning_form_return(&self) -> Mode {
        if self.settings.is_some() {
            Mode::Settings
        } else {
            Mode::PlanningManagement
        }
    }

    /// Planning form keys. Cancel closes the form back to the modal; the
    /// remaining keys (field navigation, typing, Enter/apply) live in
    /// `handle_planning_form_edit_key`.
    pub(in crate::app) fn handle_planning_form_key(&mut self, key: KeyEvent) -> Result<Action> {
        if self.planning_form.is_none() {
            self.mode = self.planning_form_return();
            return Ok(Action::None);
        }
        match key.code {
            // The editor is a text form, so printable characters (including `h`)
            // stay text; `Esc` and the horizontal-left key back out to the mode
            // the editor was opened from (tui-config-mode's drill-down).
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') | KeyCode::Left => {
                self.planning_form = None;
                self.mode = self.planning_form_return();
                Ok(Action::None)
            }
            _ => self.handle_planning_form_edit_key(key),
        }
    }

    /// Form field navigation and editing, plus Enter to apply (task groups
    /// 3/4 fill in the mutations).
    pub(in crate::app) fn handle_planning_form_edit_key(
        &mut self,
        key: KeyEvent,
    ) -> Result<Action> {
        if self.planning_form.is_none() {
            return Ok(Action::None);
        }
        match key.code {
            KeyCode::Enter => {
                if self
                    .planning_form
                    .as_ref()
                    .is_some_and(|f| f.focused_is_date() && !f.focused_date_is_complete())
                {
                    self.open_date_picker(Mode::PlanningForm);
                    Ok(Action::None)
                } else {
                    self.planning_form_apply()
                }
            }
            KeyCode::Tab | KeyCode::Down => {
                if let Some(ref mut f) = self.planning_form {
                    f.focus_next();
                }
                Ok(Action::None)
            }
            KeyCode::BackTab | KeyCode::Up => {
                if let Some(ref mut f) = self.planning_form {
                    f.focus_prev();
                }
                Ok(Action::None)
            }
            KeyCode::Backspace => {
                if let Some(ref mut f) = self.planning_form {
                    f.delete_char();
                }
                Ok(Action::None)
            }
            KeyCode::Char(c) => {
                if let Some(ref mut f) = self.planning_form {
                    f.insert_char(c);
                }
                Ok(Action::None)
            }
            KeyCode::F(2) => {
                if self
                    .planning_form
                    .as_ref()
                    .is_some_and(|f| f.focused_is_date())
                {
                    self.open_date_picker(Mode::PlanningForm);
                }
                Ok(Action::None)
            }
            _ => Ok(Action::None),
        }
    }

    /// Apply the open form (create or edit). Filled in by task group 3
    /// (create) and 4 (edit/rename cascades).
    pub(in crate::app) fn planning_form_apply(&mut self) -> Result<Action> {
        let Some(form) = self.planning_form.clone() else {
            self.mode = self.planning_form_return();
            return Ok(Action::None);
        };
        let result = self.planning_form_commit(&form);
        match result {
            Ok(()) => {
                self.planning_form = None;
                self.mode = self.planning_form_return();
                Ok(Action::None)
            }
            Err(e) => {
                self.status_msg = Some(format!("Cannot save {}: {e}", form.kind.noun()));
                Ok(Action::None)
            }
        }
    }

    pub(in crate::app) fn planning_form_commit(&mut self, form: &PlanningForm) -> Result<()> {
        let root = self.project_root();
        let mut planner = Planner::from_config(self.store.config.clone(), root.clone());
        let name = form.name().trim().to_string();
        if name.is_empty() {
            return Err(TixError::InvalidInput("a name is required".to_string()));
        }
        match form.kind {
            PlanningKind::Cycle => {
                let overlaps = if let Some(old) = form.editing.as_deref() {
                    if old != name {
                        planner.rename_cycle_cascade(&self.store, old, &name)?;
                    }
                    planner.set_cycle_fields(
                        &name,
                        Some(form.description()),
                        Some(form.date_a()),
                        Some(form.date_b()),
                    )?;
                    Vec::new()
                } else {
                    planner.add_cycle(Some(name.clone()))?;
                    planner.set_cycle_fields(
                        &name,
                        Some(form.description()),
                        Some(form.date_a()),
                        Some(form.date_b()),
                    )?;
                    // A new cycle whose window intersects an existing one is
                    // created anyway; the overlap is surfaced as a status note,
                    // the TUI's counterpart to the CLI's stderr warning
                    // (tix #1509). A start-less cycle has no window and never
                    // warns.
                    planner.overlapping_cycles(&name)
                };
                self.reload_config_from(&root)?;
                if !overlaps.is_empty() {
                    self.status_msg = Some(format!(
                        "cycle `{name}` overlaps existing cycle(s): {}",
                        overlaps.join(", ")
                    ));
                }
            }
            PlanningKind::Milestone => {
                let target = if form.date_a().trim().is_empty() {
                    None
                } else {
                    Some(form.date_a())
                };
                if let Some(old) = form.editing.as_deref() {
                    if old != name {
                        let old_milestone = planner.milestone(old)?;
                        planner.rename_milestone_cascade(&self.store, &old_milestone, &name)?;
                    }
                    planner.update_milestone(&name, target, Some(form.description()))?;
                } else {
                    planner.create_milestone_file(&name, target, Some(form.description()))?;
                }
            }
        }
        self.refresh_corpus()?;
        Ok(())
    }
}