lazyfossil 0.7.3

Make it easy to work with Fossil from the terminal: browse files, see history, preview changes, and commit or sync without extra friction.
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
use crate::app::{AppState, CommitTarget, PreviewKind, Tab};
use ratatui::prelude::*;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Tabs, Wrap};

pub fn draw(frame: &mut Frame, state: &mut AppState) {
    let mut cursor = None;
    let areas = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(0),
            Constraint::Length(4),
        ])
        .split(frame.area());

    let tabs = Tabs::new(vec!["Working tree", "File history", "Timeline"])
        .select(match state.tab {
            Tab::WorkingTree => 0,
            Tab::FileHistory => 1,
            Tab::Timeline => 2,
        })
        .block(Block::default().borders(Borders::ALL).title("lazyfossil"));
    frame.render_widget(tabs, areas[0]);

    let body = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(35), Constraint::Percentage(65)])
        .split(areas[1]);

    let mut file_state = match state.tab {
        Tab::WorkingTree => ListState::default().with_offset(state.files_scroll),
        Tab::FileHistory => ListState::default().with_offset(state.history_scroll),
        Tab::Timeline => ListState::default().with_offset(state.timeline_scroll),
    };
    let left = if let Some(repo) = &state.repo {
        match state.tab {
            Tab::Timeline => {
                file_state.select(Some(state.timeline_selected));
                let items: Vec<ListItem> = repo
                    .timeline
                    .iter()
                    .enumerate()
                    .map(|(i, t)| {
                        let prefix = if i == state.timeline_selected {
                            ">"
                        } else {
                            " "
                        };
                        let mut spans = Vec::new();
                        spans.push(Span::raw(format!("{}{}", prefix, t.rid)));
                        if !t.tags.is_empty() {
                            spans.push(Span::raw(" "));
                            spans.push(Span::styled(
                                format!("[{}]", t.tags),
                                Style::default().fg(Color::Cyan),
                            ));
                        }
                        spans.push(Span::raw(format!(" {}", t.message)));
                        ListItem::new(Line::from(spans))
                    })
                    .collect();
                List::new(items)
                    .highlight_style(Style::default().add_modifier(Modifier::REVERSED))
                    .block(Block::default().borders(Borders::ALL).title("Timeline"))
            }
            Tab::FileHistory => {
                file_state.select(Some(state.history_selected));
                let items: Vec<ListItem> = if state.history.is_empty() {
                    vec![ListItem::new("No history entries found")]
                } else {
                    state
                        .history
                        .iter()
                        .enumerate()
                        .map(|(i, t)| {
                            let prefix = if i == state.history_selected {
                                ">"
                            } else {
                                " "
                            };
                            ListItem::new(format!("{}{} {}", prefix, t.rid, t.message))
                        })
                        .collect()
                };
                List::new(items)
                    .highlight_style(Style::default().add_modifier(Modifier::REVERSED))
                    .block(Block::default().borders(Borders::ALL).title("File history"))
            }
            _ => {
                file_state.select(Some(repo.selected_file));
                let items: Vec<ListItem> = repo
                    .files
                    .iter()
                    .enumerate()
                    .map(|(i, f)| {
                        let prefix = if i == repo.selected_file { ">" } else { " " };
                        let selected = if state.selected_files.iter().any(|p| p == &f.path) {
                            "*"
                        } else {
                            " "
                        };
                        let kind = match f.status.as_str() {
                            "extra" => "??",
                            "edited" => "M",
                            "added" => "A",
                            "deleted" => "D",
                            "missing" => "!",
                            "conflict" => "C",
                            _ => "✓",
                        };
                        let mut item =
                            ListItem::new(format!("{}{} {} {}", prefix, selected, kind, f.path));
                        if f.status == "checked-out" {
                            item = item.style(Style::default().fg(Color::Green));
                        } else if f.status == "edited" {
                            item = item.style(Style::default().fg(Color::LightRed));
                        } else if f.status == "missing" {
                            item = item.style(Style::default().fg(Color::Red));
                        }
                        item
                    })
                    .collect();
                List::new(items)
                    .highlight_style(Style::default().add_modifier(Modifier::REVERSED))
                    .block(Block::default().borders(Borders::ALL).title("Files"))
            }
        }
    } else {
        List::new(vec![ListItem::new("No repository detected")])
            .block(Block::default().borders(Borders::ALL).title("Files"))
    };

    let right = if state.repo.is_some() {
        match state.tab {
            Tab::WorkingTree => {
                let diff = state
                    .diff
                    .clone()
                    .unwrap_or_else(|| "Select a file to view diff".to_string());
                Paragraph::new(color_preview(diff, state.preview_kind))
                    .scroll((state.diff_scroll, 0))
                    .block(
                        Block::default()
                            .borders(Borders::ALL)
                            .title(preview_title(state.preview_kind)),
                    )
                    .wrap(Wrap { trim: false })
            }
            Tab::FileHistory => {
                let mut lines = Vec::new();
                if let Some(repo) = &state.repo {
                    if let Some(file) = repo.files.get(repo.selected_file) {
                        lines.push(Line::from(vec![
                            Span::styled("file ", Style::default().fg(Color::DarkGray)),
                            Span::styled(file.path.clone(), Style::default().fg(Color::Yellow)),
                        ]));
                        lines.push(Line::from(vec![
                            Span::styled("entries ", Style::default().fg(Color::DarkGray)),
                            Span::styled(
                                state.history.len().to_string(),
                                Style::default().fg(Color::White),
                            ),
                        ]));
                        lines.push(Line::from(""));
                    }
                }
                if let Some(entry) = state.history.get(state.history_selected) {
                    lines.push(Line::from(vec![
                        Span::styled("commit ", Style::default().fg(Color::DarkGray)),
                        Span::styled(entry.rid.clone(), Style::default().fg(Color::Yellow)),
                    ]));
                    lines.push(Line::from(vec![
                        Span::styled("author ", Style::default().fg(Color::DarkGray)),
                        Span::raw(entry.user.clone()),
                        Span::raw("  "),
                        Span::styled(entry.date.clone(), Style::default().fg(Color::DarkGray)),
                    ]));
                    lines.push(Line::from(vec![
                        Span::styled("message ", Style::default().fg(Color::DarkGray)),
                        Span::raw(entry.message.clone()),
                    ]));
                    if !entry.tags.is_empty() {
                        lines.push(Line::from(vec![
                            Span::styled("tags ", Style::default().fg(Color::DarkGray)),
                            Span::styled(entry.tags.clone(), Style::default().fg(Color::Cyan)),
                        ]));
                    }
                    lines.push(Line::from(""));
                }
                let diff = state
                    .history_diff
                    .clone()
                    .unwrap_or_else(|| "No history diff available".to_string());
                lines.extend(color_preview(diff, PreviewKind::Diff).lines);
                Paragraph::new(Text::from(lines))
                    .scroll((state.diff_scroll, 0))
                    .block(
                        Block::default()
                            .borders(Borders::ALL)
                            .title("File history details"),
                    )
                    .wrap(Wrap { trim: false })
            }
            Tab::Timeline => {
                let mut lines = Vec::new();
                if let Some(repo) = &state.repo {
                    lines.push(Line::from(vec![
                        Span::styled("entries ", Style::default().fg(Color::DarkGray)),
                        Span::styled(
                            repo.timeline.len().to_string(),
                            Style::default().fg(Color::White),
                        ),
                    ]));
                    lines.push(Line::from(""));
                    if let Some(entry) = repo.timeline.get(state.timeline_selected) {
                        lines.push(Line::from(vec![
                            Span::styled("commit ", Style::default().fg(Color::DarkGray)),
                            Span::styled(entry.rid.clone(), Style::default().fg(Color::Yellow)),
                        ]));
                        lines.push(Line::from(vec![
                            Span::styled("author ", Style::default().fg(Color::DarkGray)),
                            Span::raw(entry.user.clone()),
                            Span::raw("  "),
                            Span::styled(entry.date.clone(), Style::default().fg(Color::DarkGray)),
                        ]));
                        lines.push(Line::from(vec![
                            Span::styled("message ", Style::default().fg(Color::DarkGray)),
                            Span::raw(entry.message.clone()),
                        ]));
                        lines.push(Line::from(""));
                    }
                }
                let diff = state
                    .timeline_diff
                    .clone()
                    .unwrap_or_else(|| "No timeline diff available".to_string());
                lines.extend(color_preview(diff, PreviewKind::Diff).lines);
                Paragraph::new(Text::from(lines))
                    .scroll((state.diff_scroll, 0))
                    .block(
                        Block::default()
                            .borders(Borders::ALL)
                            .title("Timeline details"),
                    )
                    .wrap(Wrap { trim: false })
            }
        }
    } else {
        Paragraph::new(
            state
                .error
                .clone()
                .unwrap_or_else(|| "Open a Fossil checkout to begin".to_string()),
        )
        .block(Block::default().borders(Borders::ALL).title("Status"))
    };
    frame.render_stateful_widget(left, body[0], &mut file_state);
    match state.tab {
        Tab::WorkingTree => state.files_scroll = file_state.offset(),
        Tab::FileHistory => state.history_scroll = file_state.offset(),
        Tab::Timeline => state.timeline_scroll = file_state.offset(),
    }
    frame.render_widget(right, body[1]);

    let footer = if let Some(msg) = &state.commit_prompt {
        let target = match state.commit_target {
            CommitTarget::Selected => "selected",
            CommitTarget::Current => "current",
        };
        let text = commit_prompt_text(target, msg);
        cursor = Some((
            areas[2].x + 1 + 7 + target.len() as u16 + 2 + msg.chars().count() as u16,
            areas[2].y + 1,
        ));
        Paragraph::new(text).block(Block::default().borders(Borders::ALL).title("Commit"))
    } else if let Some(path) = &state.ignore_prompt {
        let text = confirmation_prompt("ignore ", path, "?");
        cursor = Some((
            areas[2].x + 1 + 7 + path.chars().count() as u16,
            areas[2].y + 1,
        ));
        Paragraph::new(text).block(Block::default().borders(Borders::ALL).title("Ignore"))
    } else if let Some(path) = &state.discard_prompt {
        let text = confirmation_prompt("discard changes in ", path, "?");
        cursor = Some((
            areas[2].x + 1 + 19 + path.chars().count() as u16,
            areas[2].y + 1,
        ));
        Paragraph::new(text).block(Block::default().borders(Borders::ALL).title("Discard"))
    } else if state.repo.is_none() {
        let lines = vec![Line::from(vec![
            key_span("q", Color::Yellow),
            Span::raw(" quit"),
        ])];
        Paragraph::new(Text::from(lines)).block(Block::default().borders(Borders::TOP))
    } else {
        let sel_count = state.selected_files.len();
        let mut lines = vec![Line::from(vec![
            key_span("q", Color::Yellow),
            Span::raw(" quit  "),
            key_span("r", Color::Yellow),
            Span::raw(" refresh  "),
            key_span("Space", Color::Yellow),
            Span::raw(" select  "),
            key_span("i", Color::Yellow),
            Span::raw(" ignore  "),
            key_span("c", Color::Yellow),
            Span::raw(" commit  "),
            key_span("a", Color::Yellow),
            Span::raw(" all/none  "),
            key_span("p", Color::Yellow),
            Span::raw(" pull  "),
            key_span("e", Color::Yellow),
            Span::raw(" edit  "),
            key_span("o", Color::Yellow),
            Span::raw(" open  "),
            key_span("d", Color::Yellow),
            Span::raw(" discard  "),
            key_span("H", Color::Yellow),
            Span::raw(" hex  "),
            key_span("Tab", Color::Yellow),
            Span::raw(" views  "),
            Span::styled("mouse", Style::default().fg(Color::DarkGray)),
            Span::raw(" select/scroll left pane"),
        ])];
        if state.tab != Tab::Timeline {
            if let Some(repo) = &state.repo {
                if let Some(f) = repo.files.get(repo.selected_file) {
                    lines.push(Line::from(vec![
                        Span::styled("selected", Style::default().fg(Color::DarkGray)),
                        Span::raw(": "),
                        Span::styled(f.path.clone(), Style::default().fg(Color::Yellow)),
                        Span::raw(" ["),
                        Span::styled(f.status.clone(), Style::default().fg(Color::Cyan)),
                        Span::raw("]"),
                    ]));
                }
            }
        }
        if state.tab == Tab::WorkingTree {
            lines.push(Line::from(vec![
                Span::styled("selected files", Style::default().fg(Color::DarkGray)),
                Span::raw(": "),
                Span::styled(sel_count.to_string(), Style::default().fg(Color::White)),
            ]));
        }
        Paragraph::new(Text::from(lines))
            .block(Block::default().borders(Borders::TOP).title("Shortcuts"))
    };

    frame.render_widget(footer, areas[2]);
    if let Some(error) = &state.error {
        if state.repo.is_some() {
            let popup_area = centered_rect(60, 20, frame.area());
            let mut lines = error
                .lines()
                .map(|line| Line::from(styled_message_line(line)))
                .collect::<Vec<_>>();
            lines.push(Line::from(Span::styled(
                "Press Esc to dismiss",
                Style::default().fg(Color::DarkGray),
            )));
            let popup = Paragraph::new(Text::from(lines))
                .block(Block::default().borders(Borders::ALL).title("Warning"))
                .wrap(Wrap { trim: true });
            frame.render_widget(Clear, popup_area);
            frame.render_widget(popup, popup_area);
        }
    }
    if state.repo.is_none() {
        let popup_area = centered_rect(72, 42, frame.area());
        let popup = Paragraph::new(Text::from(info_box_lines()))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("Not a Fossil checkout"),
            )
            .wrap(Wrap { trim: true });
        frame.render_widget(Clear, popup_area);
        frame.render_widget(popup, popup_area);
    }
    if let Some((x, y)) = cursor {
        frame.set_cursor_position((x, y));
    }
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);
    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}

fn commit_prompt_text(target: &str, msg: &str) -> Text<'static> {
    Text::from(vec![
        Line::from(vec![
            Span::raw("commit "),
            key_span(target, Color::Yellow),
            Span::raw(": "),
            Span::styled(
                msg.to_string(),
                Style::default().bg(Color::DarkGray).fg(Color::White),
            ),
        ]),
        confirmation_hint_line(),
    ])
}

fn confirmation_prompt(prefix: &str, value: &str, suffix: &str) -> Text<'static> {
    Text::from(vec![
        Line::from(vec![
            Span::raw(prefix.to_string()),
            Span::styled(
                value.to_string(),
                Style::default().bg(Color::DarkGray).fg(Color::White),
            ),
            Span::raw(suffix.to_string()),
        ]),
        confirmation_hint_line(),
    ])
}

fn confirmation_hint_line() -> Line<'static> {
    Line::from(vec![
        key_span("Esc", Color::Red),
        Span::raw(" cancel · "),
        key_span("Enter", Color::Green),
        Span::raw(" confirm"),
    ])
}

fn key_span(label: &str, color: Color) -> Span<'static> {
    Span::styled(
        label.to_string(),
        Style::default().fg(color).add_modifier(Modifier::BOLD),
    )
}

fn styled_message_line(line: &str) -> Vec<Span<'static>> {
    // let mut s = line.to_string();
    // vec![Span::raw(s)]
    let mut spans = Vec::new();
    let mut rest = line;
    while let Some(start) = rest.find("[[") {
        let (before, after_start) = rest.split_at(start);
        if !before.is_empty() {
            spans.push(Span::raw(before.to_string()));
        }
        let inner = &after_start[2..];
        if let Some(end) = inner.find("]]") {
            let value = &inner[..end];
            spans.push(Span::styled(
                value.to_string(),
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ));
            rest = &inner[end + 2..];
        } else {
            spans.push(Span::raw(after_start.to_string()));
            rest = "";
            break;
        }
    }
    if !rest.is_empty() {
        spans.push(Span::raw(rest.to_string()));
    }
    if spans.is_empty() {
        vec![Span::raw(line.to_string())]
    } else {
        spans
    }
}

fn info_box_lines() -> Vec<Line<'static>> {
    vec![
        Line::from(vec![Span::styled(
            "lazyfossil could not find a Fossil checkout in this directory.",
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "What you can do:",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from("  • move into a Fossil checkout and restart"),
        Line::from("  • run `fossil open <repo>` or `fossil checkout <uuid>`"),
        Line::from("  • press q to quit"),
        Line::from(""),
        Line::from(vec![Span::styled(
            "Repository actions are disabled until a checkout is detected.",
            Style::default().fg(Color::DarkGray),
        )]),
    ]
}

fn preview_title(kind: PreviewKind) -> &'static str {
    match kind {
        PreviewKind::Diff => "Diff",
        PreviewKind::Plain => "Plain preview",
        PreviewKind::Markdown => "Markdown preview",
        PreviewKind::Hex => "Hex preview",
        PreviewKind::Notice => "Preview",
    }
}

fn color_preview(diff: String, kind: PreviewKind) -> Text<'static> {
    Text::from(
        diff.lines()
            .map(|line| {
                if line.starts_with("Press [o] to open externally or [H] for hex view") {
                    return Line::from(vec![
                        Span::raw("Press "),
                        key_span("o", Color::Yellow),
                        Span::raw(" to open externally or "),
                        key_span("H", Color::Yellow),
                        Span::raw(" for hex view"),
                    ]);
                }
                let style = match kind {
                    PreviewKind::Diff => {
                        if line.starts_with("Preview unavailable for ") {
                            Style::default()
                                .fg(Color::Yellow)
                                .add_modifier(Modifier::BOLD)
                        } else if line.starts_with("+++") || line.starts_with("---") {
                            Style::default().fg(Color::Blue)
                        } else if line.starts_with("@@") {
                            Style::default()
                                .fg(Color::Cyan)
                                .add_modifier(Modifier::BOLD)
                        } else if line.starts_with('+') {
                            Style::default().fg(Color::Green)
                        } else if line.starts_with('-') {
                            Style::default().fg(Color::Red)
                        } else {
                            Style::default().fg(Color::Reset)
                        }
                    }
                    PreviewKind::Markdown => {
                        if line.starts_with('#') {
                            Style::default()
                                .fg(Color::Cyan)
                                .add_modifier(Modifier::BOLD)
                        } else if line.starts_with("- ") || line.starts_with("* ") {
                            Style::default().fg(Color::Yellow)
                        } else if line.starts_with('>') {
                            Style::default().fg(Color::DarkGray)
                        } else {
                            Style::default().fg(Color::Reset)
                        }
                    }
                    PreviewKind::Plain | PreviewKind::Hex => Style::default().fg(Color::Reset),
                    PreviewKind::Notice => {
                        if line.starts_with("Preview unavailable for ")
                            || line.starts_with("Missing file ")
                            || line.starts_with("Conflict detected for ")
                        {
                            Style::default()
                                .fg(Color::Yellow)
                                .add_modifier(Modifier::BOLD)
                        } else if line.starts_with("Possible rename detected:") {
                            Style::default()
                                .fg(Color::Cyan)
                                .add_modifier(Modifier::BOLD)
                        } else {
                            Style::default().fg(Color::Reset)
                        }
                    }
                };
                Line::from(Span::styled(line.to_string(), style))
            })
            .collect::<Vec<_>>(),
    )
}

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

    #[test]
    fn confirmation_prompt_contains_hint_line() {
        let text = confirmation_prompt("ignore ", "tracked.txt", "?");
        let lines = text.lines.into_iter().collect::<Vec<_>>();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].spans[1].content, "tracked.txt");
        assert_eq!(lines[1].spans[0].content, "Esc");
        assert_eq!(lines[1].spans[2].content, "Enter");
    }

    #[test]
    fn styled_message_line_highlights_markers() {
        let spans = styled_message_line("File [[path/to/file]] renamed");
        assert!(spans
            .iter()
            .any(|span| span.content.as_ref() == "path/to/file"));
        assert!(spans.len() >= 3);
    }

    #[test]
    fn key_span_uses_expected_label() {
        let span = key_span("Esc", Color::Red);
        assert_eq!(span.content, "Esc");
    }

    #[test]
    fn info_box_mentions_checkout_actions() {
        let lines = info_box_lines();
        assert!(lines
            .iter()
            .any(|line| line.to_string().contains("not find a Fossil checkout")));
        assert!(lines
            .iter()
            .any(|line| line.to_string().contains("press q to quit")));
    }

    #[test]
    fn commit_prompt_text_uses_selected_target_and_message() {
        let text = commit_prompt_text("selected", "Fix bug");
        let lines = text.lines.into_iter().collect::<Vec<_>>();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].spans[1].content, "selected");
        assert_eq!(lines[0].spans[3].content, "Fix bug");
    }

    #[test]
    fn footer_omits_selected_files_in_non_working_tree_tabs() {
        let mut state = AppState {
            tab: Tab::FileHistory,
            repo: None,
            error: None,
            diff: None,
            diff_scroll: 0,
            selected_files: vec!["a.txt".to_string()],
            commit_prompt: None,
            commit_target: CommitTarget::Selected,
            ignore_prompt: None,
            discard_prompt: None,
            history: vec![],
            history_diff: None,
            timeline_diff: None,
            redraw: false,
            show_hex: false,
            history_selected: 0,
            timeline_selected: 0,
            files_scroll: 0,
            history_scroll: 0,
            timeline_scroll: 0,
            preview_kind: PreviewKind::Diff,
        };
        let footer = if state.tab == Tab::WorkingTree {
            let sel_count = state.selected_files.len();
            let mut lines = vec![Line::from(vec![Span::raw("q")])];
            lines.push(Line::from(vec![
                Span::styled("selected files", Style::default().fg(Color::DarkGray)),
                Span::raw(": "),
                Span::styled(sel_count.to_string(), Style::default().fg(Color::White)),
            ]));
            Text::from(lines)
        } else {
            Text::from(vec![Line::from(vec![Span::raw("q")])])
        };
        assert!(!footer.to_string().contains("selected files"));
        state.tab = Tab::Timeline;
        let footer = if state.tab == Tab::WorkingTree {
            Text::from(vec![Line::from(vec![Span::raw("selected files")])])
        } else {
            Text::from(vec![Line::from(vec![Span::raw("q")])])
        };
        assert!(!footer.to_string().contains("selected files"));
    }

    #[test]
    fn preview_titles_match_context() {
        assert_eq!(preview_title(PreviewKind::Diff), "Diff");
        assert_eq!(preview_title(PreviewKind::Plain), "Plain preview");
        assert_eq!(preview_title(PreviewKind::Markdown), "Markdown preview");
        assert_eq!(preview_title(PreviewKind::Hex), "Hex preview");
        assert_eq!(preview_title(PreviewKind::Notice), "Preview");
    }

    #[test]
    fn commit_prompt_helper_formats_message() {
        let text = commit_prompt_text("selected", "Ship it");
        assert!(text.to_string().contains("commit selected: Ship it"));
    }

    #[test]
    fn footer_mentions_mouse_hints() {
        let state = AppState {
            tab: Tab::WorkingTree,
            repo: None,
            error: None,
            diff: None,
            diff_scroll: 0,
            selected_files: vec![],
            commit_prompt: None,
            commit_target: CommitTarget::Selected,
            ignore_prompt: None,
            discard_prompt: None,
            history: vec![],
            history_diff: None,
            timeline_diff: None,
            redraw: false,
            show_hex: false,
            history_selected: 0,
            timeline_selected: 0,
            files_scroll: 0,
            history_scroll: 0,
            timeline_scroll: 0,
            preview_kind: PreviewKind::Diff,
        };
        let footer_text = {
            let sel_count = state.selected_files.len();
            let mut lines = vec![Line::from(vec![Span::raw("q")])];
            lines.push(Line::from(vec![
                Span::styled("selected files", Style::default().fg(Color::DarkGray)),
                Span::raw(": "),
                Span::styled(sel_count.to_string(), Style::default().fg(Color::White)),
            ]));
            lines.push(Line::from(vec![
                Span::styled("mouse", Style::default().fg(Color::DarkGray)),
                Span::raw(" select/scroll"),
            ]));
            Text::from(lines)
        };
        assert!(footer_text.to_string().contains("mouse select/scroll"));
    }

}