datasight 0.10.0

Terminal TUI for CSV, TSV, Parquet, JSON and NDJSON files — vim-style navigation, filtering, sorting, group-by, plotting and cloud storage (Azure, S3)
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
use crate::browser::FileBrowser;
use crate::theme::Theme;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
use ratatui::Frame;

const PAGE_SCROLL_LINES: usize = 20;

#[derive(Debug, PartialEq)]
pub enum TextMode {
    Normal,
    Search,
}

#[derive(Default, Debug)]
pub struct TextSearchState {
    pub query: String,
    pub matches: Vec<(usize, usize)>,
    pub cursor: usize,
}

pub struct TextApp {
    pub title: String,
    pub theme: &'static Theme,
    pub should_quit: bool,

    lines: Vec<String>,
    truncated: Option<u64>,
    is_pretty_json: bool,

    row_offset: usize,
    col_offset: usize,
    wrap: bool,
    show_line_numbers: bool,

    search: TextSearchState,
    mode: TextMode,

    /// Tracks pending `g` keypress for the `gg` two-key sequence.
    last_g: bool,
}

#[derive(Debug)]
pub struct TextLoad {
    pub lines: Vec<String>,
    pub truncated: Option<u64>,
    pub is_pretty_json: bool,
}

#[derive(Debug)]
pub enum TextLoadError {
    Binary,
    Io(String),
}

impl std::fmt::Display for TextLoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TextLoadError::Binary => write!(f, "not a text file"),
            TextLoadError::Io(s) => write!(f, "{}", s),
        }
    }
}

impl std::error::Error for TextLoadError {}

pub fn load_text(
    path: &str,
    backend: &dyn FileBrowser,
    max_bytes: usize,
) -> Result<TextLoad, TextLoadError> {
    let bytes = read_bytes(path, backend).map_err(TextLoadError::Io)?;
    let ext = path.rsplit('.').next().unwrap_or("").to_lowercase();
    parse_text(&bytes, &ext, max_bytes)
}

fn read_bytes(path: &str, backend: &dyn FileBrowser) -> Result<Vec<u8>, String> {
    if path.starts_with("az://") || path.starts_with("s3://") {
        backend.download_bytes(path).map_err(|e| e.to_string())
    } else {
        std::fs::read(path).map_err(|e| e.to_string())
    }
}

/// Pure byte → TextLoad transformation. Split out so it can be unit-tested
/// without touching the filesystem or a backend.
pub fn parse_text(bytes: &[u8], ext: &str, max_bytes: usize) -> Result<TextLoad, TextLoadError> {
    let total = bytes.len() as u64;
    let truncated = if (total as usize) > max_bytes {
        Some(total)
    } else {
        None
    };
    let slice = if truncated.is_some() {
        &bytes[..max_bytes]
    } else {
        bytes
    };
    let s = std::str::from_utf8(slice).map_err(|_| TextLoadError::Binary)?;

    // Pretty-print JSON only when the file fit fully (truncating mid-JSON
    // would yield a parse error and leave the user with raw, half-loaded
    // bytes). For .ndjson/.jsonl we never pretty-print: each line is its
    // own document and serde would refuse the whole-file payload anyway.
    let (content, is_pretty_json) = if ext == "json" && truncated.is_none() {
        match serde_json::from_str::<serde_json::Value>(s) {
            Ok(v) => (
                serde_json::to_string_pretty(&v).unwrap_or_else(|_| s.to_string()),
                true,
            ),
            Err(_) => (s.to_string(), false),
        }
    } else {
        (s.to_string(), false)
    };

    let lines: Vec<String> = content.split('\n').map(|s| s.to_string()).collect();
    Ok(TextLoad {
        lines,
        truncated,
        is_pretty_json,
    })
}

impl TextApp {
    pub fn new(load: TextLoad, title: String, theme: &'static Theme) -> Self {
        Self {
            title,
            theme,
            should_quit: false,
            lines: load.lines,
            truncated: load.truncated,
            is_pretty_json: load.is_pretty_json,
            row_offset: 0,
            col_offset: 0,
            wrap: true,
            show_line_numbers: true,
            search: TextSearchState::default(),
            mode: TextMode::Normal,
            last_g: false,
        }
    }

    pub fn is_typing(&self) -> bool {
        self.mode == TextMode::Search
    }

    fn line_count(&self) -> usize {
        self.lines.len()
    }

    fn scroll_down(&mut self, n: usize) {
        let max = self.line_count().saturating_sub(1);
        self.row_offset = (self.row_offset + n).min(max);
    }

    fn scroll_up(&mut self, n: usize) {
        self.row_offset = self.row_offset.saturating_sub(n);
    }

    fn go_top(&mut self) {
        self.row_offset = 0;
    }

    fn go_bottom(&mut self) {
        self.row_offset = self.line_count().saturating_sub(1);
    }

    fn h_scroll_right(&mut self) {
        self.col_offset = self.col_offset.saturating_add(1);
    }

    fn h_scroll_left(&mut self) {
        self.col_offset = self.col_offset.saturating_sub(1);
    }

    fn toggle_wrap(&mut self) {
        self.wrap = !self.wrap;
        if self.wrap {
            self.col_offset = 0;
        }
    }

    fn toggle_line_numbers(&mut self) {
        self.show_line_numbers = !self.show_line_numbers;
    }

    fn enter_search(&mut self) {
        self.mode = TextMode::Search;
        self.search.query.clear();
        self.search.matches.clear();
        self.search.cursor = 0;
    }

    fn exit_search(&mut self) {
        self.mode = TextMode::Normal;
        self.search.query.clear();
        self.search.matches.clear();
        self.search.cursor = 0;
    }

    fn confirm_search(&mut self) {
        self.mode = TextMode::Normal;
        self.recompute_matches();
        if let Some(&(row, _)) = self.search.matches.first() {
            self.row_offset = row;
            self.search.cursor = 0;
        }
    }

    fn recompute_matches(&mut self) {
        self.search.matches.clear();
        self.search.cursor = 0;
        if self.search.query.is_empty() {
            return;
        }
        let q = self.search.query.to_lowercase();
        for (i, line) in self.lines.iter().enumerate() {
            let lower = line.to_lowercase();
            let mut start = 0;
            while let Some(pos) = lower[start..].find(&q) {
                let abs = start + pos;
                self.search.matches.push((i, abs));
                // Step past the match start so overlapping occurrences still
                // surface (e.g. searching "aa" in "aaaa" yields three hits).
                start = abs + 1;
                if start >= lower.len() {
                    break;
                }
            }
        }
    }

    fn next_match(&mut self) {
        if self.search.matches.is_empty() {
            return;
        }
        self.search.cursor = (self.search.cursor + 1) % self.search.matches.len();
        if let Some(&(row, _)) = self.search.matches.get(self.search.cursor) {
            self.row_offset = row;
        }
    }

    fn prev_match(&mut self) {
        if self.search.matches.is_empty() {
            return;
        }
        self.search.cursor = if self.search.cursor == 0 {
            self.search.matches.len() - 1
        } else {
            self.search.cursor - 1
        };
        if let Some(&(row, _)) = self.search.matches.get(self.search.cursor) {
            self.row_offset = row;
        }
    }

    #[cfg(test)]
    fn row_offset(&self) -> usize {
        self.row_offset
    }

    #[cfg(test)]
    fn col_offset(&self) -> usize {
        self.col_offset
    }

    #[cfg(test)]
    fn wrap(&self) -> bool {
        self.wrap
    }

    #[cfg(test)]
    fn show_line_numbers(&self) -> bool {
        self.show_line_numbers
    }

    #[cfg(test)]
    fn mode(&self) -> &TextMode {
        &self.mode
    }

    #[cfg(test)]
    fn matches_len(&self) -> usize {
        self.search.matches.len()
    }
}

pub fn dispatch_text_viewer_key(app: &mut TextApp, key: &KeyEvent) {
    match app.mode {
        TextMode::Normal => handle_normal_key(app, key),
        TextMode::Search => handle_search_key(app, key),
    }
}

fn handle_normal_key(app: &mut TextApp, key: &KeyEvent) {
    let was_g = app.last_g;
    app.last_g = false;
    match key.code {
        KeyCode::Char('j') | KeyCode::Down => app.scroll_down(1),
        KeyCode::Char('k') | KeyCode::Up => app.scroll_up(1),
        KeyCode::PageDown => app.scroll_down(PAGE_SCROLL_LINES),
        KeyCode::PageUp => app.scroll_up(PAGE_SCROLL_LINES),
        KeyCode::Char('g') => {
            if was_g {
                app.go_top();
            } else {
                app.last_g = true;
            }
        }
        KeyCode::Char('G') => app.go_bottom(),
        KeyCode::Char('h') | KeyCode::Left if !app.wrap => app.h_scroll_left(),
        KeyCode::Char('l') | KeyCode::Right if !app.wrap => app.h_scroll_right(),
        KeyCode::Char('w') => app.toggle_wrap(),
        KeyCode::Char('L') => app.toggle_line_numbers(),
        KeyCode::Char('/') => app.enter_search(),
        KeyCode::Char('n') => app.next_match(),
        KeyCode::Char('N') => app.prev_match(),
        KeyCode::Char('q') => app.should_quit = true,
        _ => {}
    }
}

fn handle_search_key(app: &mut TextApp, key: &KeyEvent) {
    match key.code {
        KeyCode::Char(c) => {
            app.search.query.push(c);
            app.recompute_matches();
        }
        KeyCode::Backspace => {
            app.search.query.pop();
            app.recompute_matches();
        }
        KeyCode::Enter => app.confirm_search(),
        KeyCode::Esc => app.exit_search(),
        _ => {}
    }
}

pub fn render_text_viewer(frame: &mut Frame, app: &mut TextApp, area: Rect, theme: &Theme) {
    let title_text = build_title(app);
    let block = Block::default()
        .title(Line::from(title_text).alignment(Alignment::Left))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.border_idle))
        .style(Style::default().bg(theme.bg));

    let inner = block.inner(area);
    frame.render_widget(block, area);

    // Reserve a single status row when search is active or matches are pinned.
    let body_area = if app.mode == TextMode::Search {
        let [body, status] =
            Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(inner);
        let count = app.search.matches.len();
        let suffix = if count > 0 {
            format!(" — {} match{}", count, if count == 1 { "" } else { "es" })
        } else if app.search.query.is_empty() {
            " — type to search, Enter to jump, Esc to cancel".to_string()
        } else {
            " — no matches".to_string()
        };
        let q = format!(" /{}_", app.search.query);
        let style = Style::default()
            .fg(theme.bg)
            .bg(theme.warn)
            .add_modifier(Modifier::BOLD);
        frame.render_widget(
            Paragraph::new(format!("{}{}", q, suffix)).style(style),
            status,
        );
        body
    } else if !app.search.matches.is_empty() {
        let [body, status] =
            Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(inner);
        let n = app.search.matches.len();
        let cursor = app.search.cursor + 1;
        let info = format!(
            " {}/{} for /{}/  —  n next, N prev ",
            cursor, n, app.search.query
        );
        let style = Style::default().fg(theme.fg_dim).bg(theme.bg_alt);
        frame.render_widget(Paragraph::new(info).style(style), status);
        body
    } else {
        inner
    };

    // Width of the line-number gutter (digits + 1 trailing space).
    let line_num_width = if app.show_line_numbers {
        let n = app.lines.len().max(1);
        ((n as f64).log10().floor() as usize) + 1
    } else {
        0
    };

    let visible_h = body_area.height as usize;
    let start = app.row_offset;
    let end = (start + visible_h).min(app.lines.len());

    // Build all visible lines as a single ratatui Lines vec — line numbers
    // rendered as a styled prefix span so wrap behaviour stays consistent.
    let mut text_lines: Vec<Line> = Vec::with_capacity(end.saturating_sub(start));
    for i in start..end {
        let mut spans: Vec<Span> = Vec::new();
        if app.show_line_numbers {
            spans.push(Span::styled(
                format!("{:>width$} ", i + 1, width = line_num_width),
                Style::default().fg(theme.fg_dim),
            ));
        }
        spans.extend(highlight_spans(&app.lines[i], &app.search.query, theme));
        text_lines.push(Line::from(spans));
    }

    let mut paragraph = Paragraph::new(text_lines).style(Style::default().fg(theme.fg));
    if app.wrap {
        paragraph = paragraph.wrap(Wrap { trim: false });
    } else {
        paragraph = paragraph.scroll((0, app.col_offset as u16));
    }
    frame.render_widget(paragraph, body_area);
}

fn build_title(app: &TextApp) -> String {
    let mut parts = vec![app.title.clone(), format!("{} lines", app.lines.len())];
    if app.is_pretty_json {
        parts.push("pretty JSON".to_string());
    }
    if let Some(total) = app.truncated {
        let mb = total as f64 / (1024.0 * 1024.0);
        parts.push(format!("truncated, {:.1} MB total", mb));
    }
    parts.push("UTF-8".to_string());
    format!(" {} ", parts.join(" · "))
}

fn highlight_spans(text: &str, query: &str, theme: &Theme) -> Vec<Span<'static>> {
    if query.is_empty() {
        return vec![Span::raw(text.to_string())];
    }
    let q = query.to_lowercase();
    let lower = text.to_lowercase();
    let hit_style = Style::default()
        .fg(theme.bg)
        .bg(theme.warn)
        .add_modifier(Modifier::BOLD);

    let mut spans: Vec<Span<'static>> = Vec::new();
    let mut last = 0;
    let mut start = 0;
    while let Some(pos) = lower[start..].find(&q) {
        let abs = start + pos;
        if abs > last {
            spans.push(Span::raw(text[last..abs].to_string()));
        }
        let end = abs + q.len();
        spans.push(Span::styled(text[abs..end].to_string(), hit_style));
        last = end;
        start = end;
        if start >= lower.len() {
            break;
        }
    }
    if last < text.len() {
        spans.push(Span::raw(text[last..].to_string()));
    }
    spans
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};

    fn theme() -> &'static Theme {
        crate::theme::default_theme()
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::NONE,
            kind: KeyEventKind::Press,
            state: KeyEventState::NONE,
        }
    }

    fn make_app(content: &str) -> TextApp {
        let bytes = content.as_bytes();
        let load = parse_text(bytes, "txt", 1024 * 1024).expect("parse");
        TextApp::new(load, "test.txt".to_string(), theme())
    }

    // ── parse_text ────────────────────────────────────────────────────────

    #[test]
    fn parse_text_plain_utf8_succeeds() {
        let load = parse_text(b"hello\nworld\n", "txt", 1024).expect("ok");
        assert_eq!(load.lines, vec!["hello", "world", ""]);
        assert!(load.truncated.is_none());
        assert!(!load.is_pretty_json);
    }

    #[test]
    fn parse_text_non_utf8_returns_binary() {
        // Lone 0xff is invalid UTF-8.
        let bytes = [0xff_u8, 0xfe, 0xfd];
        let err = parse_text(&bytes, "txt", 1024).unwrap_err();
        assert!(matches!(err, TextLoadError::Binary));
    }

    #[test]
    fn parse_text_truncates_when_over_cap() {
        let big = "a".repeat(2048);
        let load = parse_text(big.as_bytes(), "txt", 1024).expect("ok");
        assert_eq!(load.truncated, Some(2048));
        assert_eq!(load.lines.iter().map(|s| s.len()).sum::<usize>(), 1024);
    }

    #[test]
    fn parse_text_does_not_truncate_when_under_cap() {
        let load = parse_text(b"hi", "txt", 1024).expect("ok");
        assert!(load.truncated.is_none());
    }

    #[test]
    fn parse_text_pretty_prints_valid_json() {
        let load = parse_text(br#"{"a":1,"b":2}"#, "json", 1024).expect("ok");
        assert!(load.is_pretty_json);
        // Pretty-printed output spans multiple lines.
        assert!(load.lines.len() > 1);
    }

    #[test]
    fn parse_text_leaves_invalid_json_as_raw_text() {
        let load = parse_text(b"{not valid json", "json", 1024).expect("ok");
        assert!(!load.is_pretty_json);
    }

    #[test]
    fn parse_text_does_not_pretty_print_truncated_json() {
        // Even valid JSON is left as raw text when truncated, since slicing
        // mid-document would break the parser.
        let big = format!("[{}]", "1,".repeat(5000));
        let load = parse_text(big.as_bytes(), "json", 100).expect("ok");
        assert!(load.truncated.is_some());
        assert!(!load.is_pretty_json);
    }

    #[test]
    fn parse_text_does_not_pretty_print_ndjson() {
        let load = parse_text(b"{\"a\":1}\n{\"a\":2}\n", "ndjson", 1024).expect("ok");
        assert!(!load.is_pretty_json);
    }

    // ── TextApp navigation ────────────────────────────────────────────────

    #[test]
    fn new_starts_at_top_with_defaults() {
        let app = make_app("a\nb\nc\n");
        assert_eq!(app.row_offset(), 0);
        assert_eq!(app.col_offset(), 0);
        assert!(app.wrap());
        assert!(app.show_line_numbers());
        assert_eq!(app.mode(), &TextMode::Normal);
    }

    #[test]
    fn j_scrolls_down() {
        let mut app = make_app("a\nb\nc\nd\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('j')));
        assert_eq!(app.row_offset(), 1);
    }

    #[test]
    fn k_scrolls_up() {
        let mut app = make_app("a\nb\nc\nd\n");
        app.scroll_down(2);
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('k')));
        assert_eq!(app.row_offset(), 1);
    }

    #[test]
    fn j_clamps_at_last_line() {
        let mut app = make_app("a\nb\n");
        for _ in 0..10 {
            dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('j')));
        }
        // 3 lines (a, b, "") — last index is 2.
        assert_eq!(app.row_offset(), 2);
    }

    #[test]
    fn gg_jumps_to_top() {
        let mut app = make_app("a\nb\nc\nd\n");
        app.scroll_down(3);
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('g')));
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('g')));
        assert_eq!(app.row_offset(), 0);
    }

    #[test]
    fn capital_g_jumps_to_bottom() {
        let mut app = make_app("a\nb\nc\nd\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('G')));
        // 5 lines (a, b, c, d, "") — last index is 4.
        assert_eq!(app.row_offset(), 4);
    }

    #[test]
    fn page_down_scrolls_by_page_amount() {
        let many = "x\n".repeat(50);
        let mut app = make_app(&many);
        dispatch_text_viewer_key(&mut app, &key(KeyCode::PageDown));
        assert_eq!(app.row_offset(), PAGE_SCROLL_LINES);
    }

    #[test]
    fn h_and_l_only_scroll_when_wrap_off() {
        let mut app = make_app("very long line content here\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('l')));
        // wrap is on by default, so 'l' is ignored.
        assert_eq!(app.col_offset(), 0);
        // Toggle wrap off, then 'l' should advance col_offset.
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('w')));
        assert!(!app.wrap());
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('l')));
        assert_eq!(app.col_offset(), 1);
    }

    #[test]
    fn toggle_wrap_resets_col_offset() {
        let mut app = make_app("line\n");
        app.toggle_wrap(); // off
        app.col_offset = 5;
        app.toggle_wrap(); // on
        assert_eq!(app.col_offset(), 0);
    }

    #[test]
    fn capital_l_toggles_line_numbers() {
        let mut app = make_app("a\n");
        assert!(app.show_line_numbers());
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('L')));
        assert!(!app.show_line_numbers());
    }

    #[test]
    fn q_quits() {
        let mut app = make_app("a\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('q')));
        assert!(app.should_quit);
    }

    // ── Search ───────────────────────────────────────────────────────────

    #[test]
    fn slash_enters_search_mode() {
        let mut app = make_app("a\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('/')));
        assert_eq!(app.mode(), &TextMode::Search);
        assert!(app.is_typing());
    }

    #[test]
    fn typing_in_search_populates_matches() {
        let mut app = make_app("hello\nworld\nhello again\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('/')));
        for c in "hello".chars() {
            dispatch_text_viewer_key(&mut app, &key(KeyCode::Char(c)));
        }
        assert_eq!(app.matches_len(), 2);
    }

    #[test]
    fn search_is_case_insensitive() {
        let mut app = make_app("Hello\nHELLO\nhello\n");
        app.search.query = "hello".to_string();
        app.recompute_matches();
        assert_eq!(app.matches_len(), 3);
    }

    #[test]
    fn search_finds_overlapping_matches() {
        let mut app = make_app("aaaa\n");
        app.search.query = "aa".to_string();
        app.recompute_matches();
        assert_eq!(app.matches_len(), 3);
    }

    #[test]
    fn enter_confirms_search_and_returns_to_normal() {
        let mut app = make_app("a\nfoo\nb\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('/')));
        for c in "foo".chars() {
            dispatch_text_viewer_key(&mut app, &key(KeyCode::Char(c)));
        }
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Enter));
        assert_eq!(app.mode(), &TextMode::Normal);
        assert_eq!(app.row_offset(), 1);
    }

    #[test]
    fn esc_cancels_search_and_clears_matches() {
        let mut app = make_app("a\nfoo\n");
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('/')));
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('f')));
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Esc));
        assert_eq!(app.mode(), &TextMode::Normal);
        assert_eq!(app.matches_len(), 0);
    }

    #[test]
    fn n_cycles_to_next_match() {
        let mut app = make_app("foo\nbar\nfoo\nbaz\nfoo\n");
        app.search.query = "foo".to_string();
        app.recompute_matches();
        app.confirm_search(); // jumps to first match (line 0)
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('n')));
        assert_eq!(app.row_offset(), 2);
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('n')));
        assert_eq!(app.row_offset(), 4);
        // wraps back
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('n')));
        assert_eq!(app.row_offset(), 0);
    }

    #[test]
    fn capital_n_cycles_backward() {
        let mut app = make_app("foo\nbar\nfoo\n");
        app.search.query = "foo".to_string();
        app.recompute_matches();
        app.confirm_search();
        dispatch_text_viewer_key(&mut app, &key(KeyCode::Char('N')));
        assert_eq!(app.row_offset(), 2); // wraps to last
    }

    #[test]
    fn is_typing_is_true_only_in_search_mode() {
        let mut app = make_app("a\n");
        assert!(!app.is_typing());
        app.enter_search();
        assert!(app.is_typing());
        app.exit_search();
        assert!(!app.is_typing());
    }

    // ── highlight_spans ─────────────────────────────────────────────────

    #[test]
    fn highlight_spans_returns_single_span_when_no_query() {
        let spans = highlight_spans("hello", "", theme());
        assert_eq!(spans.len(), 1);
    }

    #[test]
    fn highlight_spans_splits_on_match() {
        let spans = highlight_spans("hello world hello", "hello", theme());
        // [hello][ world ][hello] — three spans, two matches in the middle of ranges.
        assert!(spans.len() >= 3);
    }
}