frame 0.1.5

A markdown task tracker with a terminal UI for humans and a CLI for agents
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
611
612
613
614
615
616
617
618
619
620
621
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;

use crate::tui::app::{App, EditTarget, Mode, MoveState, TriageSource, View};
use crate::util::unicode;

/// Render the status row (bottom of screen)
pub fn render_status_row(frame: &mut Frame, app: &App, area: Rect) {
    let bg = app.theme.background;
    let width = area.width as usize;

    let line = match &app.mode {
        Mode::Navigate if app.status_message.is_some() => render_centered_message(
            app.status_message.as_deref().unwrap(),
            width,
            bg,
            app.status_is_error,
            app.theme.text_bright,
        ),
        Mode::Navigate if app.filter_pending => {
            let mut spans = vec![
                Span::styled(
                    " f",
                    Style::default()
                        .fg(app.theme.highlight)
                        .bg(bg)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled("\u{258C}", Style::default().fg(app.theme.highlight).bg(bg)),
            ];
            let hint = "a=active o=todo b=blocked p=parked r=ready t=tag f=clear";
            build_mode_hint(&mut spans, hint, width, bg, app.theme.text_bright);
            Line::from(spans)
        }
        Mode::Navigate => {
            if let Some(pattern) = app.last_search.as_ref()
                && !matches!(app.view, View::Search)
            {
                let mut spans = vec![Span::styled(
                    format!("/{}", pattern),
                    Style::default().fg(app.theme.text_bright).bg(bg),
                )];
                let hint = if matches!(app.view, View::Detail { .. }) {
                    "n/N next/prev  Bkspc clear"
                } else {
                    "n/N next/prev  Esc clear"
                };
                build_right_side(app, &mut spans, hint, width, bg, true);
                Line::from(spans)
            } else if app.show_startup_hints {
                let hint = "? help    > commands    QQ quit";
                let hint_width = unicode::display_width(hint);
                let left_pad = width.saturating_sub(hint_width) / 2;
                let right_pad = width.saturating_sub(hint_width + left_pad);
                Line::from(vec![
                    Span::styled(" ".repeat(left_pad), Style::default().bg(bg)),
                    Span::styled(hint, Style::default().fg(Color::LightMagenta).bg(bg)),
                    Span::styled(" ".repeat(right_pad), Style::default().bg(bg)),
                ])
            } else {
                Line::from(Span::styled(" ".repeat(width), Style::default().bg(bg)))
            }
        }
        Mode::Search => {
            if app.project_search_active {
                let mut spans = vec![
                    Span::styled(
                        " Search all: ",
                        Style::default()
                            .fg(app.theme.highlight)
                            .bg(bg)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(
                        app.project_search_input.clone(),
                        Style::default().fg(app.theme.text_bright).bg(bg),
                    ),
                    Span::styled("\u{258C}", Style::default().fg(app.theme.highlight).bg(bg)),
                ];
                let hint = "Enter search  Esc cancel";
                build_right_side(app, &mut spans, hint, width, bg, false);
                Line::from(spans)
            } else {
                let mut spans = vec![
                    Span::styled(
                        format!("/{}", app.search_input),
                        Style::default().fg(app.theme.text_bright).bg(bg),
                    ),
                    Span::styled("\u{258C}", Style::default().fg(app.theme.highlight).bg(bg)),
                ];
                let hint = "Enter search  Esc cancel";
                build_right_side(app, &mut spans, hint, width, bg, false);
                Line::from(spans)
            }
        }
        Mode::Edit => {
            let is_filter_tag = matches!(
                app.edit_target,
                Some(crate::tui::app::EditTarget::FilterTag)
            );
            let is_jump_to = matches!(app.edit_target, Some(crate::tui::app::EditTarget::JumpTo));
            let label = if is_filter_tag {
                "filter tag:"
            } else if is_jump_to {
                "jump:"
            } else {
                "-- EDIT --"
            };
            let mode_label = Span::styled(
                label,
                Style::default()
                    .fg(app.theme.highlight)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            );
            let hint = if is_filter_tag {
                "Enter select  Esc cancel"
            } else if is_jump_to {
                "Enter jump  Esc cancel"
            } else {
                "Enter confirm  Esc cancel"
            };
            let mut spans = vec![Span::styled(" ", Style::default().bg(bg)), mode_label];
            if is_filter_tag || is_jump_to {
                spans.push(Span::styled(" ", Style::default().bg(bg)));
                spans.push(Span::styled(
                    app.edit_buffer.clone(),
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
                spans.push(Span::styled(
                    "\u{258C}",
                    Style::default().fg(app.theme.highlight).bg(bg),
                ));
            }
            let content_width: usize = spans
                .iter()
                .map(|s| unicode::display_width(&s.content))
                .sum();
            let hint_width = unicode::display_width(hint);
            if content_width + hint_width < width {
                let padding = width - content_width - hint_width;
                spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
                spans.push(Span::styled(
                    hint,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
            }
            Line::from(spans)
        }
        Mode::Move => {
            let label_text = if let Some(MoveState::BulkTask {
                ref removed_tasks, ..
            }) = app.move_state
            {
                format!("-- MOVE ({}) --", removed_tasks.len())
            } else {
                "-- MOVE --".to_string()
            };
            let mode_label = Span::styled(
                label_text,
                Style::default()
                    .fg(app.theme.highlight)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            );
            let hint =
                "\u{25B2}\u{25BC} move  \u{25C0}\u{25B6} depth  m/Enter \u{2713}  Esc \u{2717}";
            let mut spans = vec![Span::styled(" ", Style::default().bg(bg)), mode_label];
            build_mode_hint(&mut spans, hint, width, bg, app.theme.text_bright);
            Line::from(spans)
        }
        Mode::Triage => {
            let is_select_track = matches!(
                &app.triage_state,
                Some(ts) if matches!(ts.step, crate::tui::app::TriageStep::SelectTrack)
            );
            let step_text = if is_select_track {
                "Select track:"
            } else {
                "Select position:"
            };
            let is_cross_track = matches!(
                &app.triage_state,
                Some(ts) if matches!(ts.source, TriageSource::CrossTrackMove { .. } | TriageSource::BulkCrossTrackMove { .. })
            );
            let label_text = if is_cross_track {
                "-- MOVE TO TRACK --"
            } else {
                "-- TRIAGE --"
            };
            let mode_label = Span::styled(
                label_text,
                Style::default()
                    .fg(app.theme.highlight)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            );
            let mut spans = vec![
                Span::styled(" ", Style::default().bg(bg)),
                mode_label,
                Span::styled("  ", Style::default().bg(bg)),
            ];
            // In track selection step, show the edit buffer with cursor
            if is_select_track {
                spans.push(Span::styled(
                    step_text,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
                spans.push(Span::styled(" ", Style::default().bg(bg)));
                spans.push(Span::styled(
                    app.edit_buffer.clone(),
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
                spans.push(Span::styled(
                    "\u{258C}",
                    Style::default().fg(app.theme.highlight).bg(bg),
                ));
            } else {
                spans.push(Span::styled(
                    step_text,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
            }
            // Pad to full width
            let content_width: usize = spans
                .iter()
                .map(|s| unicode::display_width(&s.content))
                .sum();
            if content_width < width {
                spans.push(Span::styled(
                    " ".repeat(width - content_width),
                    Style::default().bg(bg),
                ));
            }
            Line::from(spans)
        }
        Mode::Select if app.status_message.is_some() => render_centered_message(
            app.status_message.as_deref().unwrap(),
            width,
            bg,
            app.status_is_error,
            app.theme.text_bright,
        ),
        Mode::Select => {
            let count = app.selection.len();
            let is_range = app.range_anchor.is_some();
            let label_text = if is_range {
                "-- RANGE --"
            } else {
                "-- SELECT --"
            };
            let mode_label = Span::styled(
                label_text,
                Style::default()
                    .fg(app.theme.highlight)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            );
            let count_text = format!("{} selected", count);
            let is_bulk_edit = matches!(
                &app.edit_target,
                Some(EditTarget::BulkTags) | Some(EditTarget::BulkDeps)
            );
            let hint = if is_bulk_edit {
                "Enter confirm  Esc cancel"
            } else if is_range {
                "V end range  Esc cancel"
            } else {
                "x/b/o/~ t d m Esc"
            };
            let mut spans = vec![Span::styled(" ", Style::default().bg(bg)), mode_label];
            let content_width: usize = spans
                .iter()
                .map(|s| unicode::display_width(&s.content))
                .sum();
            let count_width = unicode::display_width(&count_text);
            let hint_width = unicode::display_width(hint);
            let right_width = count_width + 4 + hint_width;
            if content_width + right_width < width {
                let padding = width - content_width - right_width;
                spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
                spans.push(Span::styled(
                    count_text,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
                spans.push(Span::styled(" ".repeat(4), Style::default().bg(bg)));
                spans.push(Span::styled(
                    hint,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
            } else if content_width + hint_width < width {
                let padding = width - content_width - hint_width;
                spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
                spans.push(Span::styled(
                    hint,
                    Style::default().fg(app.theme.text_bright).bg(bg),
                ));
            }
            Line::from(spans)
        }
        Mode::Command => {
            let mode_label = Span::styled(
                "-- COMMAND --",
                Style::default()
                    .fg(app.theme.highlight)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            );
            let hint = "\u{25B2}\u{25BC} navigate  Enter \u{2713}  Esc \u{2717}";
            let mut spans = vec![Span::styled(" ", Style::default().bg(bg)), mode_label];
            build_mode_hint(&mut spans, hint, width, bg, app.theme.text_bright);
            Line::from(spans)
        }
        Mode::Confirm => {
            let message = app
                .confirm_state
                .as_ref()
                .map(|s| s.message.as_str())
                .unwrap_or("Confirm?");
            let mut spans = vec![
                Span::styled(" ", Style::default().bg(bg)),
                Span::styled(
                    message.to_string(),
                    Style::default()
                        .fg(Color::LightMagenta)
                        .bg(bg)
                        .add_modifier(Modifier::BOLD),
                ),
            ];
            let content_width: usize = spans
                .iter()
                .map(|s| unicode::display_width(&s.content))
                .sum();
            if content_width < width {
                spans.push(Span::styled(
                    " ".repeat(width - content_width),
                    Style::default().bg(bg),
                ));
            }
            Line::from(spans)
        }
    };

    // In key debug mode, show the raw KeyEvent instead of the normal status row
    let line = if app.key_debug {
        let kitty_tag = if app.kitty_enabled {
            "kitty:on"
        } else {
            "kitty:off"
        };
        if let Some(ref event_str) = app.last_key_event {
            let mut spans = vec![
                Span::styled(
                    " KEY ",
                    Style::default()
                        .fg(Color::Black)
                        .bg(Color::LightYellow)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(" ", Style::default().bg(bg)),
                Span::styled(
                    event_str.clone(),
                    Style::default().fg(Color::LightYellow).bg(bg),
                ),
            ];
            let right = format!("{}  ^D off", kitty_tag);
            let content_width: usize = spans
                .iter()
                .map(|s| unicode::display_width(&s.content))
                .sum();
            let right_width = unicode::display_width(&right);
            if content_width + right_width + 2 < width {
                let padding = width - content_width - right_width;
                spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
                spans.push(Span::styled(
                    right,
                    Style::default().fg(app.theme.dim).bg(bg),
                ));
            }
            Line::from(spans)
        } else {
            let text = format!(" KEY DEBUG ON  {}  press any key...  ^D off", kitty_tag);
            let mut spans = vec![
                Span::styled(
                    " KEY DEBUG ON ",
                    Style::default()
                        .fg(Color::Black)
                        .bg(Color::LightYellow)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    format!("  {}  press any key...  ^D off", kitty_tag),
                    Style::default().fg(app.theme.dim).bg(bg),
                ),
            ];
            let content_width = unicode::display_width(&text);
            if content_width < width {
                spans.push(Span::styled(
                    " ".repeat(width - content_width),
                    Style::default().bg(bg),
                ));
            }
            Line::from(spans)
        }
    } else {
        line
    };

    let paragraph = Paragraph::new(line).style(Style::default().bg(bg));
    frame.render_widget(paragraph, area);
}

/// Build the right side of the status bar: optional message + spacer + key hints.
/// In Navigate mode, wrap messages take priority over match count.
/// In Search mode, only match count is shown (no wrap messages).
fn build_right_side<'a>(
    app: &App,
    spans: &mut Vec<Span<'a>>,
    hint: &'a str,
    width: usize,
    bg: Color,
    is_navigate: bool,
) {
    let content_width: usize = spans
        .iter()
        .map(|s| unicode::display_width(&s.content))
        .sum();
    let hint_width = unicode::display_width(hint);

    // Determine message text and style
    let message: Option<(String, Style)> = if is_navigate {
        if let Some(ref wrap_msg) = app.search_wrap_message {
            Some((
                wrap_msg.clone(),
                Style::default().fg(Color::LightMagenta).bg(bg),
            ))
        } else {
            match_count_message(app, bg)
        }
    } else {
        match_count_message(app, bg)
    };

    let spacer = 8;

    if let Some((ref msg_text, msg_style)) = message {
        let padded_msg = format!(" {} ", msg_text);
        let msg_width = unicode::display_width(&padded_msg);
        let right_width = msg_width + spacer + hint_width;
        if content_width + right_width < width {
            let padding = width - content_width - right_width;
            spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
            spans.push(Span::styled(padded_msg, msg_style));
            spans.push(Span::styled(" ".repeat(spacer), Style::default().bg(bg)));
            spans.push(Span::styled(
                hint,
                Style::default().fg(app.theme.text_bright).bg(bg),
            ));
            return;
        }
    }

    // Fallback: no message, just hints
    if content_width + hint_width < width {
        let padding = width - content_width - hint_width;
        spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
        spans.push(Span::styled(
            hint,
            Style::default().fg(app.theme.text_bright).bg(bg),
        ));
    }
}

/// Build the match count message with appropriate styling.
fn match_count_message(app: &App, bg: Color) -> Option<(String, Style)> {
    let count = app.search_match_count?;
    let text = if count == 1 {
        "1 match".to_string()
    } else {
        format!("{} matches", count)
    };
    let style = if count == 0 && app.search_zero_confirmed {
        Style::default()
            .fg(app.theme.text_bright)
            .bg(Color::Rgb(0x8D, 0x0B, 0x0B))
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(app.theme.text_bright).bg(bg)
    };
    Some((text, style))
}

/// Append right-aligned hint text to a span list.
fn build_mode_hint<'a>(
    spans: &mut Vec<Span<'a>>,
    hint: &'a str,
    width: usize,
    bg: Color,
    text_bright: Color,
) {
    let content_width: usize = spans
        .iter()
        .map(|s| unicode::display_width(&s.content))
        .sum();
    let hint_width = unicode::display_width(hint);
    if content_width + hint_width < width {
        let padding = width - content_width - hint_width;
        spans.push(Span::styled(" ".repeat(padding), Style::default().bg(bg)));
        spans.push(Span::styled(hint, Style::default().fg(text_bright).bg(bg)));
    }
}

/// Render a centered status message spanning the full width.
fn render_centered_message<'a>(
    msg: &str,
    width: usize,
    bg: Color,
    is_error: bool,
    text_bright: Color,
) -> Line<'a> {
    let msg_text = if is_error {
        format!(" {} ", msg)
    } else {
        msg.to_string()
    };
    let msg_len = unicode::display_width(&msg_text);
    let left_pad = width.saturating_sub(msg_len) / 2;
    let right_pad = width.saturating_sub(msg_len + left_pad);
    let msg_style = if is_error {
        Style::default()
            .fg(text_bright)
            .bg(Color::Rgb(0x8D, 0x0B, 0x0B))
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
            .fg(Color::LightMagenta)
            .bg(bg)
            .add_modifier(Modifier::BOLD)
    };
    Line::from(vec![
        Span::styled(" ".repeat(left_pad), Style::default().bg(bg)),
        Span::styled(msg_text, msg_style),
        Span::styled(" ".repeat(right_pad), Style::default().bg(bg)),
    ])
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::render::test_helpers::*;
    use insta::assert_snapshot;

    #[test]
    fn navigate_default() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.show_startup_hints = false;
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }

    #[test]
    fn navigate_with_status_message() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.status_message = Some("Task moved to Done".into());
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }

    #[test]
    fn search_mode() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.mode = Mode::Search;
        app.search_input = "effect".into();
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }

    #[test]
    fn edit_mode() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.mode = Mode::Edit;
        app.edit_target = Some(EditTarget::NewTask {
            task_id: "T-5".into(),
            track_id: "test".into(),
            parent_id: None,
        });
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }

    #[test]
    fn move_mode() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.mode = Mode::Move;
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }

    #[test]
    fn command_mode() {
        let mut app = app_with_track(SIMPLE_TRACK_MD);
        app.mode = Mode::Command;
        let output = render_to_string(TERM_W, 1, |frame, area| {
            render_status_row(frame, &app, area);
        });
        assert_snapshot!(output);
    }
}