sluuz 0.9.1

git, but it sleuths - a git superset with cross-repo search, secret scanning, and multi-repo management
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Row renderers, pane furniture and the modal box: what every view draws
//! with, none of it knowing which view is drawing.

use crate::git::load::{Commit, FileEntry, RefKind, RefLabel, fit_refs};
use crate::tui::clamp_scroll;
use crate::tui::input::{X_MOVE, char_to_byte, is_back, is_down, is_up};
use ratatui::Frame;
use ratatui::crossterm::event::KeyCode;
use ratatui::layout::{Margin, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
    Block, Borders, Clear, ListItem, Padding, Paragraph, Scrollbar, ScrollbarOrientation,
    ScrollbarState, Wrap,
};
use std::time::Duration;

/// Render a commit row `width` columns wide. `unpushed` prepends a yellow `↑`
/// marker (this commit is on no remote yet); pushed commits get an aligning
/// blank so columns line up. Refs take only what leaves the subject
/// `MIN_SUBJECT` columns.
pub fn commit_item(c: &Commit, unpushed: bool, width: usize) -> ListItem<'static> {
    let mark = if unpushed {
        Span::styled(
            "↑ ",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
    } else {
        Span::raw("  ")
    };
    let mut spans = vec![
        mark,
        Span::styled(
            format!("{:<7} ", c.short),
            Style::default().fg(Color::Yellow),
        ),
        Span::styled(format!("{}  ", c.date), Style::default().fg(Color::Green)),
        Span::styled(
            format!("<{}> ", c.committer),
            Style::default().fg(Color::Blue),
        ),
    ];
    let left: usize = spans.iter().map(|s| s.content.chars().count()).sum();
    let room = width.saturating_sub(left + MIN_SUBJECT);
    spans.extend(ref_spans(&c.refs, room));
    spans.push(Span::raw(c.subject.clone()));
    ListItem::new(Line::from(spans))
}

/// Columns a commit's subject keeps however many refs point at it.
pub const MIN_SUBJECT: usize = 20;

/// `(HEAD -> main, origin/main, +2) ` in `git log --decorate`'s colours, as
/// many labels as `room` holds, or nothing when no ref points here.
fn ref_spans(refs: &[RefLabel], room: usize) -> Vec<Span<'static>> {
    if refs.is_empty() {
        return Vec::new();
    }
    let (kept, hidden) = fit_refs(refs, room);
    let mut spans = vec![Span::raw("(")];
    for (i, (kind, text)) in kept.into_iter().enumerate() {
        if i > 0 {
            spans.push(Span::raw(", "));
        }
        let colour = match kind {
            RefKind::Head | RefKind::Detached => Color::Cyan,
            RefKind::Branch => Color::Green,
            RefKind::Remote => Color::Red,
            RefKind::Tag => Color::Yellow,
        };
        spans.push(Span::styled(
            text,
            Style::default().fg(colour).add_modifier(Modifier::BOLD),
        ));
    }
    if hidden > 0 {
        spans.push(Span::raw(", "));
        spans.push(Span::styled(
            format!("+{hidden}"),
            Style::default().fg(Color::DarkGray),
        ));
    }
    spans.push(Span::raw(") "));
    spans
}

pub fn file_item(f: &FileEntry) -> ListItem<'static> {
    let (color, ch) = status_glyph(f.status);
    ListItem::new(Line::from(vec![
        Span::styled(format!("{ch}  "), Style::default().fg(color)),
        Span::raw(f.path.clone()),
    ]))
}

fn status_glyph(status: char) -> (Color, char) {
    match status {
        'A' => (Color::Green, 'A'),
        'M' => (Color::Yellow, 'M'),
        'D' => (Color::Red, 'D'),
        'R' => (Color::Cyan, 'R'),
        c => (Color::Gray, c),
    }
}

/// A bordered block whose border is bright when the pane is focused.
pub fn pane_block(title: impl Into<Line<'static>>, active: bool) -> Block<'static> {
    let color = if active { Color::Cyan } else { Color::DarkGray };
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(color))
        .title(title)
}

/// How long a note sits in a view's footer before the key hints come back.
pub const NOTE: Duration = Duration::from_secs(3);

/// What a `:` line asked for.
pub enum Command {
    Help,
    Quit,
    Unknown(String),
}

/// What a key did to an open `:` line.
pub enum Typed {
    Open,
    Cancel,
    Run(Command),
}

/// The `:` line in a view's footer: what has been typed after the colon. It
/// opens showing `help` as a placeholder, and Enter on an empty line runs
/// exactly that, so the one command worth knowing needs nothing typed.
#[derive(Default)]
pub struct CommandLine {
    text: String,
}

impl CommandLine {
    pub fn on_key(&mut self, code: KeyCode) -> Typed {
        match code {
            KeyCode::Esc => Typed::Cancel,
            KeyCode::Enter => Typed::Run(match self.text.trim() {
                "" | "help" | "h" | "keys" => Command::Help,
                "q" | "quit" => Command::Quit,
                other => Command::Unknown(other.to_string()),
            }),
            KeyCode::Backspace if self.text.is_empty() => Typed::Cancel,
            KeyCode::Backspace => {
                self.text.pop();
                Typed::Open
            }
            KeyCode::Char(c) => {
                self.text.push(c);
                Typed::Open
            }
            _ => Typed::Open,
        }
    }
}

/// A pane's live filter: what has been typed into it, and where the caret sits
/// in that text. Empty means the pane shows everything its scope keeps.
#[derive(Default)]
pub struct Query {
    pub text: String,
    pub caret: usize,
}

impl Query {
    /// Does a row survive this filter? Terms are whitespace-separated and every
    /// one of them has to appear somewhere in the row, so `pablo fix` narrows
    /// to what both words are in rather than to either.
    pub fn keeps(&self, row: &str) -> bool {
        if self.text.trim().is_empty() {
            return true;
        }
        let row = row.to_lowercase();
        self.text
            .split_whitespace()
            .all(|term| row.contains(&term.to_lowercase()))
    }

    /// Start typing again at the end of whatever a previous Enter kept.
    pub fn open(&mut self) {
        self.caret = self.text.chars().count();
    }

    /// A key typed into the open filter. Returns whether the text changed, so
    /// the caller re-filters only then.
    pub fn on_key(&mut self, code: KeyCode) -> bool {
        let end = self.text.chars().count();
        match code {
            KeyCode::Char(c) => {
                self.text.insert(char_to_byte(&self.text, self.caret), c);
                self.caret += 1;
                return true;
            }
            KeyCode::Backspace if self.caret > 0 => {
                self.text.remove(char_to_byte(&self.text, self.caret - 1));
                self.caret -= 1;
                return true;
            }
            KeyCode::Delete if self.caret < end => {
                self.text.remove(char_to_byte(&self.text, self.caret));
                return true;
            }
            KeyCode::Left => self.caret = self.caret.saturating_sub(1),
            KeyCode::Right if self.caret < end => self.caret += 1,
            KeyCode::Home => self.caret = 0,
            KeyCode::End => self.caret = end,
            _ => {}
        }
        false
    }

    pub fn clear(&mut self) {
        self.text.clear();
        self.caret = 0;
    }

    /// The filter as its pane's title shows it, led by `key`, the key that
    /// opened it, so a query can never look like it went to the other pane. A
    /// `filterable` pane with nothing typed offers that key instead, dimmed.
    pub fn title_span(&self, key: char, editing: bool, filterable: bool) -> Option<Span<'static>> {
        let text = &self.text;
        if editing {
            let at = char_to_byte(text, self.caret);
            Some(Span::raw(format!(
                "   {key}{}▏{}",
                &text[..at],
                &text[at..]
            )))
        } else if !text.is_empty() {
            Some(Span::raw(format!("   {key}{text}")))
        } else if filterable {
            Some(Span::styled(
                format!("   {key} filter"),
                Style::default()
                    .fg(Color::DarkGray)
                    .add_modifier(Modifier::DIM),
            ))
        } else {
            None
        }
    }
}

/// The row under a view: the keys it answers to, or for `NOTE` what the last
/// action came to, green when it worked and yellow when it did not, or the `:`
/// line while one is open. Keys live here rather than on pane borders, which
/// only have room for what a pane is. `:help`, when `help` says the view takes
/// it, is kept at the right edge however narrow the window, since it is the way
/// to every key that falls off.
pub fn key_footer(
    actions: &[String],
    note: Option<&(bool, String)>,
    command: Option<&CommandLine>,
    help: bool,
    width: u16,
) -> Paragraph<'static> {
    let dim = Style::default().fg(Color::DarkGray);
    let line = match (command, note) {
        (Some(cmd), _) => {
            let mut spans = vec![Span::raw(format!(" :{}▏", cmd.text))];
            if cmd.text.is_empty() {
                spans.push(Span::styled("help", dim.add_modifier(Modifier::DIM)));
            }
            Line::from(spans)
        }
        (None, Some((true, text))) => Line::from(Span::styled(
            format!(" ✓ {text}"),
            Style::default().fg(Color::Green),
        )),
        (None, Some((false, text))) => Line::from(Span::styled(
            format!(" ✗ {text}"),
            Style::default().fg(Color::Yellow),
        )),
        (None, None) if !help => Line::from(Span::styled(fit(actions, width), dim)),
        (None, None) => {
            const HELP: &str = ":help ";
            let room = (width as usize).saturating_sub(HELP.len() + 2);
            let left = fit(actions, room as u16);
            let pad = (width as usize).saturating_sub(left.chars().count() + HELP.len());
            Line::from(vec![
                Span::styled(left, dim),
                Span::raw(" ".repeat(pad)),
                Span::styled(HELP, dim),
            ])
        }
    };
    Paragraph::new(line)
}

/// As many whole keys as fit in `width`, in order: a key cut off mid-word says
/// less than one left off.
fn fit(keys: &[String], width: u16) -> String {
    let mut line = String::new();
    for key in keys {
        let next = if line.is_empty() {
            format!(" {key}")
        } else {
            format!("{line} · {key}")
        };
        if next.chars().count() > width as usize {
            break;
        }
        line = next;
    }
    line
}

/// The most tabs a slider shows at once. More than this and a border runs out
/// of room, so a search with a dozen terms shows the ones around where you are.
const MAX_TABS: usize = 5;

/// A scope slider as tabs: `│` between them, and the picked one filled with
/// the border's cyan, the way a picked button is. On a pane border, whose own
/// text is already cyan, a cyan-and-bold stop the way the tab bars in the other
/// crates mark it did not stand out from the rest. Past `MAX_TABS` it shows a
/// window that slides with the picked one, and `‹`/`›` say more are hidden.
pub fn scope_tabs(labels: &[&str], picked: usize) -> Vec<Span<'static>> {
    let first = picked
        .saturating_sub(MAX_TABS / 2)
        .min(labels.len().saturating_sub(MAX_TABS));
    let last = (first + MAX_TABS).min(labels.len());
    let dim = Style::default().fg(Color::DarkGray);
    let mut spans = Vec::new();
    if first > 0 {
        spans.push(Span::styled("‹", dim));
    }
    for (i, label) in labels.iter().enumerate().take(last).skip(first) {
        if i > first {
            spans.push(Span::styled("│", dim));
        }
        let style = if i == picked {
            Style::default()
                .fg(Color::Black)
                .bg(Color::Cyan)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Reset)
        };
        spans.push(Span::styled(format!(" {label} "), style));
    }
    if last < labels.len() {
        spans.push(Span::styled("›", dim));
    }
    spans
}

/// Draw a vertical scrollbar down the right edge of `area`, with the thumb at
/// `top` of `total` rows. No bar is drawn when everything already fits.
fn render_vscrollbar(frame: &mut Frame, area: Rect, total: usize, top: usize) {
    let viewport = area.height.saturating_sub(2) as usize;
    if total <= viewport {
        return; // everything fits; no scrollbar needed
    }
    let mut state = ScrollbarState::new(total - viewport).position(top);
    let bar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
        .begin_symbol(None)
        .end_symbol(None);
    frame.render_stateful_widget(bar, area.inner(Margin::new(0, 1)), &mut state);
}

/// Scrollbar for a diff pane: thumb tracks the scroll line.
pub fn diff_scrollbar(frame: &mut Frame, area: Rect, total_lines: usize, scroll: u16) {
    render_vscrollbar(frame, area, total_lines, scroll as usize);
}

/// Scrollbar for a list pane: thumb tracks the visible window (the list's
/// `offset`). Call it right after rendering the list so the offset is current.
pub fn list_scrollbar(frame: &mut Frame, area: Rect, total: usize, offset: usize) {
    render_vscrollbar(frame, area, total, offset);
}

/// Horizontal scrollbar along a diff pane's bottom border. `max_line` is the
/// widest content line, `cell_w` the visible columns per side, `hscroll` the pan
/// offset. Drawn only when the content is wider than one cell (else there's
/// nothing to pan). Kept off the corners with a 1-col horizontal inset.
pub fn diff_hscrollbar(frame: &mut Frame, area: Rect, max_line: usize, cell_w: u16, hscroll: u16) {
    let cell = cell_w as usize;
    if max_line <= cell {
        return;
    }
    let mut state = ScrollbarState::new(max_line - cell).position(hscroll as usize);
    // `■` renders vertically centered and medium-weight - between the too-thin,
    // low-sitting `▬` and the full-cell block `█`.
    let bar = Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
        .begin_symbol(None)
        .end_symbol(None)
        .thumb_symbol("■");
    frame.render_stateful_widget(bar, area.inner(Margin::new(1, 0)), &mut state);
}

// ── modal ───────────────────────────────────────────────────────────────────

/// A box over the whole screen with something the user has to read: a title, a
/// body, and no way past it but dismissing it.
///
/// It exists because the alternative is a line in a pane title, which is where
/// a failed `git difftool` used to be reported and where nobody looked: the
/// screen came back unchanged and the run looked like a no-op. A view holds an
/// `Option<Modal>` and hands it the keys first, the way the drill gates on its
/// confirm popup.
pub struct Modal {
    title: String,
    body: String,
    scroll: u16,
    /// Yellow for an alert, cyan for a reader: the one thing that differs.
    colour: Color,
    keys: &'static str,
}

impl Modal {
    pub fn new(title: impl Into<String>, body: impl Into<String>) -> Modal {
        Modal {
            title: title.into(),
            body: body.into(),
            scroll: 0,
            colour: Color::Yellow,
            keys: "j/k ↑↓ scroll · esc dismiss",
        }
    }

    /// A reader: the same box in cyan, for something read by choice rather
    /// than something that went wrong. `rows` is a key and what it does, one
    /// per line, the keys in one column.
    pub fn reader(title: impl Into<String>, rows: &[(String, String)]) -> Modal {
        let width = rows
            .iter()
            .map(|(k, _)| k.chars().count())
            .max()
            .unwrap_or(0)
            + 3;
        let body = rows
            .iter()
            .map(|(key, does)| format!("{key:<width$}{does}"))
            .collect::<Vec<_>>()
            .join("\n");
        Modal {
            title: title.into(),
            body,
            scroll: 0,
            colour: Color::Cyan,
            keys: "j/k ↑↓ scroll · esc close",
        }
    }

    /// Free text under a reader's rows, a blank line apart: a message, which
    /// wraps on its own rather than hanging under the rows' second column.
    pub fn with_text(mut self, text: &str) -> Modal {
        if !text.is_empty() {
            self.body = format!("{}\n\n{text}", self.body);
        }
        self
    }

    /// Handle one key while the modal is up. Returns true when it was
    /// dismissed, so the caller drops it. Movement keys scroll a body too long
    /// for the box; anything else is swallowed, so a stray keypress can't
    /// close a message before it is read.
    pub fn on_key(&mut self, code: KeyCode) -> bool {
        if is_down(code) {
            self.scroll = self.scroll.saturating_add(1);
        } else if is_up(code) {
            self.scroll = self.scroll.saturating_sub(1);
        } else if is_back(code) || matches!(code, KeyCode::Enter | KeyCode::Char('q' | ' ')) {
            return true;
        }
        false
    }

    /// Draw it centered over whatever the view already rendered.
    pub fn draw(&mut self, frame: &mut Frame) {
        let full = frame.area();
        let width = box_width(full.width);
        // Measured from the wrapped text, never its line count: counting
        // unwrapped lines is what clips the bottom off a long message.
        let body_h = wrapped_height(&self.body, box_inner_width(width)) as u16;
        // The body, a blank, the keys.
        let area = popup_area(full, width, box_height(body_h + 2, full.height));

        // The viewport is what is left after the borders and the padding row.
        let viewport = area.height.saturating_sub(BOX_CHROME_H);
        self.scroll = clamp_scroll(self.scroll, body_h as usize, viewport);

        frame.render_widget(Clear, area); // wipe whatever's underneath
        let mut lines: Vec<Line> = self
            .body
            .lines()
            .map(|l| Line::raw(l.to_string()))
            .collect();
        lines.push(Line::raw(""));
        lines.push(box_hint(self.keys));

        let body = Paragraph::new(lines)
            .block(box_block(self.colour, &self.title))
            .wrap(Wrap { trim: false })
            .scroll((self.scroll, 0));
        frame.render_widget(body, area);
    }
}

// ── the house box ───────────────────────────────────────────────────────────
// Every overlay is built from these, so only its colour and its buttons carry
// meaning: gate red, alert yellow, offer/picker/form/reader cyan.

/// Narrowest a box may be, so a two-word message still reads as a box.
pub const BOX_MIN_W: u16 = 24;
/// Widest, so one long line does not stretch a box across a 200-column screen.
pub const BOX_MAX_W: u16 = 88;
/// Rows the chrome costs: two borders plus the single top padding row.
pub const BOX_CHROME_H: u16 = 3;
/// Columns the chrome costs: two borders plus two columns of padding a side.
pub const BOX_CHROME_W: u16 = 6;

/// How wide a box is on a screen this wide.
pub fn box_width(screen_w: u16) -> u16 {
    screen_w.saturating_sub(4).clamp(BOX_MIN_W, BOX_MAX_W)
}

/// The columns a body actually gets, which is what it must be wrapped to.
pub fn box_inner_width(width: u16) -> usize {
    width.saturating_sub(BOX_CHROME_W).max(1) as usize
}

/// How tall a box holding `body_rows` *wrapped* rows is, capped at the screen.
/// Pass the wrapped count, never the line count: measuring the unwrapped text
/// is what clips a modal's last row off and makes it look unanswerable.
///
/// The cap is the whole screen and not some fraction of it. A box is drawn over
/// a `Clear`, so it owns the screen while it is up anyway, and a fraction only
/// decides in advance that a long one gets cut off.
pub fn box_height(body_rows: u16, screen_h: u16) -> u16 {
    let floor = BOX_CHROME_H + 1;
    body_rows
        .saturating_add(BOX_CHROME_H)
        .clamp(floor, screen_h.max(floor))
}

/// The bordered block every box wears: a spaced title on the top border, and
/// otherwise an unbroken frame in the colour that says what kind it is.
///
/// Nothing else is written on the border. Keys go in the body, through
/// `box_hint`: a frame with a sentence along the bottom of it stops reading as
/// a frame, and the title then has to compete with it.
pub fn box_block(colour: Color, title: &str) -> Block<'static> {
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(colour))
        // One blank row above the content and none below it: the key line is
        // the last thing in the box, and a blank under it is a wasted row that
        // makes the frame look loose.
        .padding(Padding::new(2, 2, 1, 0))
        .title(format!(" {} ", title.trim()))
}

/// The line of keys a box ends with, as the last row of its body. Every kind
/// puts it in the same place, so it is where the eye already is.
///
/// Quieter than anything else in the box, deliberately: an unfocused field
/// label is the default foreground dimmed, so this goes a step below that with
/// `DarkGray` dimmed again. Separation is the blank row above it and its fixed
/// place at the bottom, not brightness. Colouring it only made a guideline look
/// like something worth reading.
pub fn box_hint(keys: &str) -> Line<'static> {
    Line::from(Span::styled(
        keys.to_string(),
        Style::default()
            .fg(Color::DarkGray)
            .add_modifier(Modifier::DIM),
    ))
}

/// The Yes/No row a gate and an offer share. The labels carry their keys, so
/// the hint line does not have to teach them twice, and the picked one is
/// filled with the border colour rather than merely reversed: a reversed
/// button reads as "selected", a filled one reads as "this is what Enter does".
pub fn box_buttons(colour: Color, yes: bool) -> Line<'static> {
    let button = |label: &str, picked: bool| {
        let style = if picked {
            Style::default()
                .fg(Color::Black)
                .bg(colour)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().add_modifier(Modifier::DIM)
        };
        Span::styled(format!(" {label} "), style)
    };
    Line::from(vec![
        button("Yes (y)", yes),
        Span::raw("  "),
        button("No (n)", !yes),
    ])
}

/// A Yes/No box: a gate (red, opening on No, since a reflex Enter must never be
/// the key that fires an irreversible thing) or an offer (cyan, opening on
/// Yes, with nothing at stake). Which it is comes from `colour` and the `yes`
/// the caller opens it with. `note` is what else to know before answering.
pub fn confirm_popup(
    frame: &mut Frame,
    colour: Color,
    title: &str,
    name: &str,
    note: Option<&str>,
    yes: bool,
) {
    let full = frame.area();
    let width = box_width(full.width);
    let mut lines = vec![Line::from(Span::styled(
        name.to_string(),
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD),
    ))];
    for note in note.into_iter().flat_map(str::lines) {
        lines.push(Line::from(Span::styled(
            note.to_string(),
            Style::default().add_modifier(Modifier::DIM),
        )));
    }
    lines.push(Line::from(""));
    lines.push(box_buttons(colour, yes));
    lines.push(Line::from(""));
    lines.push(box_hint(&format!("{X_MOVE} move · enter select · y/n")));
    // Measured from the wrapped text: a long name or note wraps, and a box of
    // fixed height would put the buttons past its own bottom border.
    let inner = box_inner_width(width);
    let rows: usize = lines.iter().map(|l| l.width().div_ceil(inner).max(1)).sum();
    let area = popup_area(full, width, box_height(rows as u16, full.height));

    frame.render_widget(Clear, area);
    let body = Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .block(box_block(colour, title));
    frame.render_widget(body, area);
}

/// The typed gate: red, no Yes/No, and nothing happens on Enter until `typed`
/// is exactly `name`, which the box shows so it is copied rather than guessed.
pub fn typed_popup(frame: &mut Frame, title: &str, note: Option<&str>, name: &str, typed: &str) {
    let full = frame.area();
    let width = box_width(full.width);
    let mut lines: Vec<Line<'static>> = note
        .into_iter()
        .flat_map(str::lines)
        .map(|l| Line::from(l.to_string()))
        .collect();
    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("type "),
        Span::styled(
            name.to_string(),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(" to confirm:"),
    ]));
    // Armed or not is shown on the thing Enter does, not only on the text: plain
    // text and a dim `enter delete` until the name matches, then bold green
    // text and `enter delete` filled red, the way a picked gate button is.
    let armed = typed == name;
    let field = if armed {
        Style::default()
            .fg(Color::Green)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
    };
    lines.push(Line::from(Span::styled(format!("{typed}▏"), field)));
    lines.push(Line::from(""));
    let enter = if armed {
        Span::styled(
            " enter delete ",
            Style::default()
                .fg(Color::Black)
                .bg(Color::Red)
                .add_modifier(Modifier::BOLD),
        )
    } else {
        Span::styled(
            " enter delete ",
            Style::default()
                .fg(Color::DarkGray)
                .add_modifier(Modifier::DIM),
        )
    };
    let esc = Span::styled(
        "  esc cancel",
        Style::default()
            .fg(Color::DarkGray)
            .add_modifier(Modifier::DIM),
    );
    lines.push(Line::from(vec![enter, esc]));
    let inner = box_inner_width(width);
    let rows: usize = lines.iter().map(|l| l.width().div_ceil(inner).max(1)).sum();
    let area = popup_area(full, width, box_height(rows as u16, full.height));

    frame.render_widget(Clear, area);
    let body = Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .block(box_block(Color::Red, title));
    frame.render_widget(body, area);
}

/// Rows `text` takes once wrapped to `width` columns.
pub fn wrapped_height(text: &str, width: usize) -> usize {
    text.lines()
        .map(|l| l.chars().count().div_ceil(width).max(1))
        .sum()
}

/// A box of at most `w`×`h`, centered in `area`.
pub fn popup_area(area: Rect, w: u16, h: u16) -> Rect {
    let (w, h) = (w.min(area.width), h.min(area.height));
    Rect {
        x: area.x + (area.width - w) / 2,
        y: area.y + (area.height - h) / 2,
        width: w,
        height: h,
    }
}

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

    fn query(text: &str) -> Query {
        Query {
            text: text.to_string(),
            caret: 0,
        }
    }

    #[test]
    fn a_filter_keeps_only_rows_every_term_is_in() {
        // `/` and `?` split on whitespace and require all of them, which is what
        // lets `pablo fix` mean both words rather than either.
        assert!(
            query("").keeps("anything at all"),
            "an empty filter keeps everything"
        );

        let q = query("pablo fix");
        assert!(q.keeps("a1b2c3 2026-09-03 pablo fix: the thing"));
        assert!(!q.keeps("a1b2c3 2026-09-03 pablo feat: the thing"));
        assert!(!q.keeps("a1b2c3 2026-09-03 marta fix: the thing"));
    }

    #[test]
    fn a_filter_ignores_case_on_both_sides() {
        assert!(query("FIX Pablo").keeps("pablo fix: lowercase row"));
        assert!(query("fix").keeps("PABLO FIX: UPPERCASE ROW"));
    }

    #[test]
    fn a_filter_of_only_spaces_is_no_filter() {
        // Typing a space and deleting the word must not leave a query that
        // matches nothing at all.
        assert!(query("   ").keeps("anything"));
    }
}