tread 0.4.2

A terminal reader for markdown, CSV, JSON and source code. Zero dependencies.
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
//! The pager driving a CSV: the pinned header, column scrolling, `w`, the
//! status bar and the three yanks (SPEC.md §CSV, "Reading affordances").
//!
//! These are pager tests, not format tests — everything below goes through key
//! presses and the painted frame, so they check the *seam*: that a format that
//! pins rows, scrolls by columns and counts in rows gets all three without the
//! pager knowing what a CSV is.
#![deny(unsafe_code)]

use super::*;
use crate::source::csv::{CsvSource, HEAD_ROWS};

const ROWS: usize = 12;

fn csv_body() -> String {
    let mut s = String::from("id,name,city\n");
    for i in 1..=40 {
        s.push_str(&format!("{i},person{i},town{i}\n"));
    }
    s
}

fn csv_pager(body: &str, cols: usize, rows: usize) -> Pager {
    let src = CsvSource::from_bytes(body.as_bytes().to_vec(), None);
    Pager::new(Box::new(src), "people.csv".into(), cols, rows, None)
}

/// The painted frame as one string per terminal row.
///
/// A pager frame is positioned with `CSI r;cH` rather than newlines, so the
/// rows are recovered by following those moves — which is also a check that
/// the painter puts each row where it says it does.
fn frame_rows(p: &mut Pager) -> Vec<String> {
    let mut f = Frame::new(true);
    p.paint(&mut f);
    let mut rows = vec![String::new(); p.rows.max(1)];
    let mut at = 0usize;
    let mut cs = f.as_str().chars().peekable();
    while let Some(c) = cs.next() {
        if c != '\u{1b}' {
            let last = rows.len() - 1;
            rows[at.min(last)].push(c);
            continue;
        }
        let mut params = String::new();
        if cs.peek() == Some(&'[') {
            cs.next();
        }
        for c in cs.by_ref() {
            if c.is_ascii_alphabetic() {
                if c == 'H' {
                    let row: usize = params.split(';').next().unwrap_or("1").parse().unwrap_or(1);
                    at = row.saturating_sub(1);
                }
                break;
            }
            params.push(c);
        }
    }
    rows.iter().map(|r| r.trim_end().to_string()).collect()
}

#[test]
fn the_header_stays_on_screen_while_the_body_scrolls() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    let first = frame_rows(&mut p);
    assert!(first[1].contains("name"), "{:?}", first[1]);
    assert!(first[3].contains("person1"));
    press(&mut p, "GG");
    let last = frame_rows(&mut p);
    // Same three rows at the top, whatever the body is showing.
    assert_eq!(&last[..3], &first[..3]);
    assert!(last[3].contains("person"), "{:?}", last[3]);
    assert!(!last[3].contains("person1,"), "the body did scroll");
    // ... and the header is exactly as wide as the body rows it sits over.
    let w = |s: &str| crate::render::str_width(s);
    assert_eq!(w(&last[1]), w(&last[4]));
}

#[test]
fn the_cursor_never_enters_the_pinned_header() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    assert_eq!(p.cursor, HEAD_ROWS);
    press(&mut p, "kkkkk");
    assert_eq!(p.cursor, HEAD_ROWS, "k stopped at the first data row");
    press(&mut p, "g");
    assert_eq!(p.cursor, HEAD_ROWS);
    press(&mut p, "uu");
    assert_eq!(p.cursor, HEAD_ROWS);
    assert!(p.top >= HEAD_ROWS);
}

#[test]
fn scrolling_down_and_back_up_lands_where_it_started() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    let start = frame_rows(&mut p);
    press(&mut p, "  ");
    press(&mut p, "bb");
    assert_eq!(frame_rows(&mut p), start);
}

#[test]
fn h_and_l_move_by_column_and_the_header_moves_with_the_body() {
    // Narrow enough that the grid does not fit.
    let mut p = csv_pager(&csv_body(), 18, ROWS);
    let before = frame_rows(&mut p);
    assert!(before[1].contains("id"));
    press(&mut p, "l");
    let after = frame_rows(&mut p);
    assert!(after[1].contains("name"), "header did not scroll: {:?}", after[1]);
    // The header and the body are cut at the same place, which is the whole
    // point of pinning: the bars line up.
    // Display columns, not byte offsets: the grid is drawn in 3-byte glyphs.
    let bars = |s: &str, glyph: char| -> Vec<usize> {
        s.chars()
            .enumerate()
            .filter(|(_, c)| *c == glyph)
            .map(|(i, _)| i)
            .collect()
    };
    let sep = '\u{2502}';
    assert!(bars(&after[1], sep).len() >= 2);
    assert_eq!(bars(&after[1], sep), bars(&after[4], sep));
    // The top border's interior joins sit under the same bars.
    assert_eq!(
        bars(&after[0], '\u{252c}'),
        bars(&after[1], sep)
            .into_iter()
            .filter(|i| *i > 0 && *i + 1 < after[1].chars().count())
            .collect::<Vec<_>>()
    );
    press(&mut p, "hh");
    assert_eq!(frame_rows(&mut p)[1], before[1], "h came back");
}

#[test]
fn the_status_bar_names_the_row_the_total_and_the_column() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    let s = p.status_line();
    assert!(s.starts_with("people.csv"), "{s}");
    assert!(s.contains("row 1/40"), "{s}");
    assert!(s.ends_with("id"), "{s}");
    press(&mut p, "jjl");
    let s = p.status_line();
    assert!(s.contains("row 3/40"), "{s}");
    assert!(s.ends_with("name"), "{s}");
}

#[test]
fn w_widens_the_column_under_the_cursor() {
    let mut body = String::from("k,note\n");
    body.push_str("1,short\n");
    for i in 2..20 {
        body.push_str(&format!("{i},{}\n", "n".repeat(30)));
    }
    let mut p = csv_pager(&body, 80, ROWS);
    press(&mut p, "l");
    let before = frame_rows(&mut p)[4].clone();
    press(&mut p, "w");
    let after = frame_rows(&mut p)[4].clone();
    assert!(p.message.as_deref().unwrap_or("").contains("note"));
    assert!(
        crate::render::str_width(&after) >= crate::render::str_width(&before),
        "{before:?} -> {after:?}"
    );
    // Markdown has nothing to widen, and says so rather than doing nothing.
    let mut md = pager(DOC, 60, ROWS);
    press(&mut md, "w");
    assert_eq!(md.message.as_deref(), Some("nothing to widen here"));
}

#[test]
fn y_copies_the_cell_capital_y_the_row_and_c_the_column() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    press(&mut p, "jy");
    assert_eq!(p.peek_yank().map(|y| y.text), Some("2\n".to_string()));
    press(&mut p, "Y");
    assert_eq!(
        p.peek_yank().map(|y| y.text),
        Some("2,person2,town2\n".to_string())
    );
    press(&mut p, "lc");
    let col = p.peek_yank().expect("column").text;
    assert!(col.starts_with("name\nperson1\nperson2\n"), "{col:?}");
    // A visual selection yanks whole rows, as CSV.
    press(&mut p, "vjy");
    assert_eq!(
        p.peek_yank().map(|y| y.text),
        Some("2,person2,town2\n3,person3,town3\n".to_string())
    );
}

#[test]
fn search_moves_the_cursor_and_highlights_the_row() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    press(&mut p, "/person30");
    key(&mut p, Key::Enter);
    assert!(p.cursor_text().contains("person30"), "{}", p.cursor_text());
    assert_eq!(p.match_count(), 1);
    press(&mut p, "/nobody");
    key(&mut p, Key::Enter);
    assert!(p.status_line().contains("pattern not found"));
}

#[test]
fn a_csv_has_no_outline_no_links_and_nothing_to_fold() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    press(&mut p, "o");
    assert_eq!(p.mode, Mode::Normal, "there is no outline to open");
    assert_eq!(p.message.as_deref(), Some("no headings"));
    press(&mut p, "za");
    assert_eq!(p.message.as_deref(), Some("no heading here"));
    key(&mut p, Key::Tab);
    assert_eq!(p.message.as_deref(), Some("no further heading"));
    assert_eq!(p.link_count(), 0);
    assert_eq!(p.line_count(), HEAD_ROWS + 41);
}

#[test]
fn a_resize_keeps_the_header_pinned_and_the_cursor_put() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    press(&mut p, "jjjjj");
    let row = p.cursor;
    let cell = p.cursor_text();
    p.resize(24, 8);
    assert_eq!(p.cursor, row, "the cursor stayed on its row");
    assert!(p.cursor_text().starts_with(&cell[..6]), "{}", p.cursor_text());
    let rows = frame_rows(&mut p);
    assert!(rows[1].contains("id"), "header still pinned: {:?}", rows[1]);
    assert!(rows[0].starts_with('\u{250c}'));
    // Narrower: the grid is re-fitted, so nothing is wider than the terminal
    // except by way of horizontal scrolling.
    assert!(p.line_count() > 0);
    p.resize(200, 40);
    assert!(frame_rows(&mut p)[1].contains("city"));
}

#[test]
fn an_empty_csv_paints_and_quits() {
    let mut p = csv_pager("", 40, ROWS);
    assert_eq!(p.line_count(), 0);
    let rows = frame_rows(&mut p);
    assert!(rows[0].is_empty());
    press(&mut p, "jkGgly");
    press(&mut p, "q");
    assert!(p.should_quit());
}

#[test]
fn a_terminal_too_short_for_the_header_still_paints() {
    for rows in 1..=4 {
        let mut p = csv_pager(&csv_body(), 40, rows);
        let painted = frame_rows(&mut p);
        assert!(painted.len() >= rows);
        press(&mut p, "jjGg");
        assert!(p.cursor < p.line_count().max(1));
    }
}

// -- `G` on a file whose end is not known yet --------------------------------

/// A body with more rows than the open path indexes, so its end is genuinely
/// unknown when `G` is pressed.
fn lazy_body(rows: usize) -> String {
    let mut s = String::from("id,name,city\n");
    for i in 1..=rows {
        s.push_str(&format!("{i},person{i},town{i}\n"));
    }
    s
}

#[test]
fn g_scans_to_the_real_end_instead_of_the_end_of_the_index() {
    const N: usize = 20_000;
    let mut p = csv_pager(&lazy_body(N), 60, ROWS);
    frame_rows(&mut p);
    let indexed = p.line_count();
    assert!(indexed < N, "the file was fully indexed on open: {indexed}");
    let before = p.cursor;

    press(&mut p, "G");
    // Not a jump into the middle of the file dressed up as the end.
    assert_eq!(p.cursor, before, "G moved before the end was known");
    let msg = p.status_line();
    assert!(msg.starts_with("scanning to end of file"), "{msg}");
    assert!(msg.contains('%'), "{msg}");

    // The idle tick drives it; a bounded number of them gets there.
    for _ in 0..200 {
        if p.cursor != before {
            break;
        }
        p.idle();
    }
    assert_eq!(p.cursor, HEAD_ROWS + N - 1, "G landed off the last data row");
    // (The cells themselves are truncated: the columns were sized from the
    // first thousand rows, where no id is five digits long. That is the
    // sampling trade SPEC.md §CSV makes, and `w` is the answer to it.)
    assert!(p.cursor_text().contains("person"), "{}", p.cursor_text());
    // The bottom border is below the cursor, not under it.
    assert_eq!(p.line_count(), HEAD_ROWS + N + 1);
    assert!(p.status_line().contains(&format!("row {N}/{N}")), "{}", p.status_line());
}

#[test]
fn a_key_press_stops_the_scan_and_q_never_waits_for_it() {
    let mut p = csv_pager(&lazy_body(20_000), 60, ROWS);
    frame_rows(&mut p);
    let before = p.cursor;
    press(&mut p, "G");
    press(&mut p, "j");
    assert_eq!(p.status_line(), "scan stopped");
    assert_eq!(p.cursor, before + 1, "the key that stopped the scan still acted");
    // Idle ticks keep indexing in the background, but the cursor stays put:
    // the abandoned scan does not come back and move it later.
    for _ in 0..50 {
        p.idle();
    }
    assert_eq!(p.cursor, before + 1);

    // And `q` mid-scan quits at once rather than waiting for the scan.
    let mut p = csv_pager(&lazy_body(20_000), 60, ROWS);
    frame_rows(&mut p);
    press(&mut p, "G");
    assert!(!p.should_quit());
    press(&mut p, "q");
    assert!(p.should_quit());
}

#[test]
fn g_on_a_fully_known_document_still_lands_at_once() {
    // Markdown, and a CSV small enough to be indexed on open: neither has
    // anything to scan, so `G` is the plain jump it always was.
    let mut p = super::pager("# a\n\ntext\n\n# b\n\nmore\n", 40, ROWS);
    press(&mut p, "G");
    assert_eq!(p.cursor, p.line_count() - 1);
    assert!(!p.status_line().contains("scanning"));

    let mut c = csv_pager(&csv_body(), 60, ROWS);
    frame_rows(&mut c);
    press(&mut c, "G");
    assert_eq!(c.cursor, HEAD_ROWS + 40 - 1);
    assert!(!c.status_line().contains("scanning"), "{}", c.status_line());
}


#[test]
fn width_wider_than_the_terminal_still_scrolls_by_column() {
    // `--width 200` in an 18-column terminal: the grid is laid out 200 wide and
    // only 18 of it is on screen, so `l` must keep walking the columns instead
    // of clamping the offset against a viewport that is not the one being
    // painted (SPEC.md §CSV, "h/l scroll by column").
    let src = CsvSource::from_bytes(csv_body().as_bytes().to_vec(), None);
    let mut p = Pager::new(Box::new(src), "people.csv".into(), 18, ROWS, Some(200));
    let before = frame_rows(&mut p);
    assert!(before[1].contains("id"));
    // Two columns right is off the right edge of an 18-column screen, so the
    // grid has to move under it.
    press(&mut p, "ll");
    assert!(p.hoff > 0, "the offset never moved: hoff {}", p.hoff);
    let after = frame_rows(&mut p);
    assert!(after[1].contains("city"), "last column unreachable: {:?}", after[1]);
    let bars = |s: &str| s.chars().filter(|c| *c == '\u{2502}').count();
    assert_eq!(bars(&after[1]), bars(&after[4]), "header and body disagree");
    press(&mut p, "hh");
    assert_eq!(p.hoff, 0);
    assert_eq!(frame_rows(&mut p)[1], before[1], "h came back");
}

// -- the row detail (Enter) ---------------------------------------------------

/// `Enter` on a record row opens it as a form. The pager does not know what a
/// CSV is: it asks the source for a detail and shows whatever comes back.
#[test]
fn enter_opens_the_row_under_the_cursor_as_a_form() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    p.cursor = HEAD_ROWS; // the first data row
    key(&mut p, Key::Enter);
    assert_eq!(p.mode, Mode::Detail);

    let rows = frame_rows(&mut p);
    assert!(rows[0].contains("Row 1"), "{:?}", rows[0]);
    assert!(rows[0].contains("Esc close"), "{:?}", rows[0]);
    // One field per line, label beside value.
    assert!(rows[1].contains("id") && rows[1].contains('1'), "{:?}", rows[1]);
    assert!(rows[2].contains("name") && rows[2].contains("person1"), "{:?}", rows[2]);
    assert!(rows[3].contains("city") && rows[3].contains("town1"), "{:?}", rows[3]);

    key(&mut p, Key::Esc);
    assert_eq!(p.mode, Mode::Normal, "Esc closes it");
}

/// `r` is not one of the row detail's own keys, and taking it had to be free
/// in **every** mode rather than in the ones a lens uses. Inside the form it
/// falls through to the normal dispatcher, exactly as any unbound key there
/// does — the overlay stays up, the selected field does not move, and since a
/// CSV has no record to open the pager says so. Stated here so that binding `r`
/// inside this overlay one day is a failing test rather than a silent shadow.
#[test]
fn r_is_not_one_of_the_row_details_keys() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    p.cursor = HEAD_ROWS;
    key(&mut p, Key::Enter);
    assert_eq!(p.mode, Mode::Detail);
    let rows = p.line_count();
    press(&mut p, "j");
    let sel = p.detail_sel;
    assert_eq!(sel, 1, "`j` is one of them");
    press(&mut p, "r");
    assert_eq!(p.mode, Mode::Detail, "the overlay is still up");
    assert_eq!(p.detail_sel, sel, "and `r` moved nothing in it");
    assert_eq!(p.line_count(), rows, "nor anything behind it");
    assert!(p.status_line().contains("nothing to open"), "{}", p.status_line());
}

/// The fields a header-shaped grid cannot show are exactly what the form is
/// for: they must be in it, and the grid must say so with the marker.
#[test]
fn a_ragged_row_shows_its_hidden_fields_in_the_form() {
    let body = "id,name\n1,alice\n2,bo,hidden one,hidden two\n";
    let mut p = csv_pager(body, 60, ROWS);
    let ragged = HEAD_ROWS + 1;
    assert!(
        frame_rows(&mut p)[ragged].starts_with(crate::theme::MARKER_MORE),
        "the grid marks the row"
    );

    p.cursor = ragged;
    key(&mut p, Key::Enter);
    let rows = frame_rows(&mut p);
    let form = rows.join("\n");
    assert!(form.contains("hidden one"), "{form}");
    assert!(form.contains("hidden two"), "{form}");
    assert!(form.contains("[3]") && form.contains("[4]"), "named by position: {form}");
}

/// Markdown has no row detail, so `Enter` there must still fold.
#[test]
fn enter_still_folds_in_a_format_without_row_detail() {
    let mut p = pager("# One\n\nbody\n\n# Two\n\nmore\n", 40, ROWS);
    p.cursor = 0;
    key(&mut p, Key::Enter);
    assert_eq!(p.mode, Mode::Normal, "no overlay in markdown");
    assert!(p.src.hidden_at(0).is_some(), "the section folded");
}

/// `y` in the form copies the highlighted field, not the row the cursor is on.
#[test]
fn y_in_the_form_copies_the_field_under_the_cursor() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    p.cursor = HEAD_ROWS;
    key(&mut p, Key::Enter);
    press(&mut p, "jj"); // id -> name -> city
    press(&mut p, "y");

    let y = p.peek_yank().expect("a field yank");
    assert_eq!(y.text, "town1\n", "the field, not the whole row");
    assert!(y.what.contains("city"), "{}", y.what);
}

/// The value copied is the real one, not the dotted form the screen shows.
#[test]
fn y_in_the_form_copies_the_raw_value_not_the_painted_one() {
    let mut p = csv_pager("a,b\n1,\"two\nlines\"\n", 60, ROWS);
    p.cursor = HEAD_ROWS;
    key(&mut p, Key::Enter);
    press(&mut p, "j");
    assert!(
        frame_rows(&mut p).join("\n").contains("two\u{b7}lines"),
        "the screen shows it safely"
    );
    press(&mut p, "y");
    assert_eq!(p.peek_yank().expect("yank").text, "two\nlines\n");
}

#[test]
fn y_on_an_empty_field_says_so_rather_than_copying_nothing() {
    let mut p = csv_pager("a,b\n1,\n", 60, ROWS);
    p.cursor = HEAD_ROWS;
    key(&mut p, Key::Enter);
    press(&mut p, "j");
    press(&mut p, "y");
    assert!(p.peek_yank().is_none(), "nothing queued");
    assert_eq!(p.message.as_deref(), Some("b is empty"));
}

/// A border row has no record, and a CSV has no sections either, so `Enter`
/// must say so rather than answering with markdown's "no heading here".
#[test]
fn enter_where_there_is_no_row_says_so() {
    let mut p = csv_pager(&csv_body(), 60, ROWS);
    p.cursor = 0; // the top border
    key(&mut p, Key::Enter);
    assert_eq!(p.mode, Mode::Normal);
    assert_eq!(p.message.as_deref(), Some("nothing to open here"));
}