plannotator-tui 0.15.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! `]` and `[` put the cursor on the first line of a hunk, where its bar is drawn.

use plannotator_tui_review::Side;

use super::*;

const BAR: char = '\u{258e}';

/// `long.txt`, forty lines, with lines 12 and 33 changed: two hunks, lines 9-15 and 30-36.
fn two_hunks(tag: &str) -> Fixture {
    let fixture = Fixture::new(tag);
    let lines = (1..=40).fold(String::new(), |text, n| text + "line " + &n.to_string() + "\n");
    fixture.write("long.txt", &lines);
    fixture.git(&["add", "long.txt"]);
    fixture.git(&["-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-qm", "long"]);
    let changed = lines.replace("line 12\n", "line twelve\n").replace("line 33\n", "line 33a\n");
    fixture.write("long.txt", &changed);
    fixture
}

fn on(screen: &Screen) -> Option<(String, Side, u32)> {
    let lines = screen.diff.cursor().expect("cursor")?;
    Some((lines.path, lines.side, lines.first))
}

/// Whether the drawn row showing `text` starts with the cursor's bar.
fn bar_on(screen: &Screen, rows: &[String], text: &str) -> bool {
    diff_side(screen, row_with(rows, text)).starts_with(BAR)
}

#[test]
fn hunk_jumps_land_on_the_hunks_first_line_and_draw_the_cursor_bar_there() {
    let fixture = two_hunks("hunk-jump");
    let mut screen = fixture.open();
    let (cols, rows) = (100, 40);
    frame(&mut screen, cols, rows);
    screen.diff.reveal("long.txt", Side::New, 13).expect("line 13");

    keys(&mut screen, "]");
    let shown = frame(&mut screen, cols, rows);
    assert!(bar_on(&screen, &shown, "line 30"), "] to the next hunk's first line: {shown:#?}");
    assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));
    assert!(shown.iter().any(|r| r.contains("@@ -30,7 +30,7 @@")), "its heading is shown: {shown:#?}");

    // From a hunk's first line `[` goes to the hunk before; from inside a hunk, to its start.
    keys(&mut screen, "[");
    let shown = frame(&mut screen, cols, rows);
    assert!(bar_on(&screen, &shown, "line 9"), "[ to the hunk before: {shown:#?}");
    assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 9)));
    screen.diff.reveal("long.txt", Side::New, 35).expect("line 35");
    keys(&mut screen, "[");
    let shown = frame(&mut screen, cols, rows);
    assert!(bar_on(&screen, &shown, "line 30"), "[ to its own hunk's first line: {shown:#?}");
    assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));

    // Past the last hunk nothing moves.
    keys(&mut screen, "]");
    assert_eq!(on(&screen), Some(("long.txt".into(), Side::New, 30)));
}

#[test]
fn a_hunk_jump_with_no_hunk_that_way_moves_nothing_past_a_file_with_none() {
    let fixture = Fixture::new("hunk-jump-none");
    std::fs::write(fixture.repo().join("aa.bin"), [0u8, 1, 2, 255]).expect("binary");
    let mut screen = fixture.open();
    assert_eq!(screen.diff.paths(), ["aa.bin", "app.py"], "a file with no hunks first");
    keys(&mut screen, "n");
    let heading = screen.diff.cursor_row().expect("cursor");
    assert_eq!((heading.path.as_str(), heading.kind), ("app.py", diffs_rs::engine::DiffRowKind::Header));
    for n in 0..3 {
        keys(&mut screen, "[");
        assert_eq!(screen.diff.cursor_row().as_ref(), Some(&heading), "[ {n}: no hunk before app.py's");
    }
}