From 371027a85f44470880b1436d59fa97774e9c8642 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:43:11 +0100 Subject: 0.6.1: redraw from the caret row, not the block height A two-line prompt with the caret moved up made the next redraw overshoot by one row and clear the line above, erasing the model's output. The editor now tracks the caret's row. Adds scripts/ux-check.sh, which drives tmux and asserts the rendered screen: byte-level tests cannot see misplaced cursors or erased rows. --- src/line.rs | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 13 deletions(-) (limited to 'src/line.rs') diff --git a/src/line.rs b/src/line.rs index 1ae210a..613dffe 100644 --- a/src/line.rs +++ b/src/line.rs @@ -43,27 +43,27 @@ impl Editor { let guard = RawGuard::enable()?; let mut buffer: Vec = Vec::new(); let mut cursor = 0usize; - let mut rows = 1usize; + let mut state = DrawState::default(); self.history_index = self.history.len(); self.draft.clear(); - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; loop { let event = event::read()?; match event { Event::Paste(text) => { insert(&mut buffer, &mut cursor, &text); - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; } Event::Resize(_, _) => { // The terminal reflowed the block we were tracking, so the // old row count is meaningless. Start a fresh prompt line // rather than risk erasing output above it. println!(); - rows = 1; - draw(prompt, &buffer, cursor, &mut rows)?; + state = DrawState::default(); + draw(prompt, &buffer, cursor, &mut state)?; } Event::Key(key) if key.kind == KeyEventKind::Press => { match handle_key(key, &mut buffer, &mut cursor, self) { @@ -71,7 +71,7 @@ impl Editor { Action::Submit => { // Redraw with the caret at the end so the submitted // line looks complete before we move on. - draw(prompt, &buffer, buffer.len(), &mut rows)?; + draw(prompt, &buffer, buffer.len(), &mut state)?; drop(guard); let line: String = buffer.iter().collect(); self.remember(&line); @@ -89,7 +89,7 @@ impl Editor { return Ok(Some(String::new())); } } - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; } _ => {} } @@ -303,15 +303,40 @@ fn cells(ch: char, col: usize) -> usize { unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) } -fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { - let width = terminal_width(); +/// Where the caret physically is, relative to the first row of the input block. +/// The previous draw can leave the caret on any row, so a redraw must move up by +/// that row, not by the number of rows the block occupies. +#[derive(Default, Clone, Copy)] +struct DrawState { + rows: usize, + cursor_row: usize, +} + +fn draw( + prompt: &str, + buffer: &[char], + cursor: usize, + state: &mut DrawState, +) -> io::Result<()> { let mut out = io::stdout(); + draw_to(&mut out, prompt, buffer, cursor, state)?; + out.flush() +} + +fn draw_to( + out: &mut W, + prompt: &str, + buffer: &[char], + cursor: usize, + state: &mut DrawState, +) -> io::Result<()> { + let width = terminal_width(); let (end_row, _) = layout(prompt, buffer, buffer.len(), width); let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); - if *rows > 1 { - execute!(out, MoveUp(clamp(*rows - 1)))?; + if state.cursor_row > 0 { + execute!(out, MoveUp(clamp(state.cursor_row)))?; } execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; @@ -330,9 +355,9 @@ fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::R execute!(out, MoveUp(clamp(end_row - cursor_row)))?; } execute!(out, MoveToColumn(clamp(cursor_col)))?; - out.flush()?; - *rows = end_row + 1; + state.rows = end_row + 1; + state.cursor_row = cursor_row; Ok(()) } @@ -433,6 +458,48 @@ mod tests { assert!(terminal_width() >= 1); } + fn drawn(prompt: &str, buffer: &str, cursor: usize, state: &mut DrawState) -> String { + let chars: Vec = buffer.chars().collect(); + let mut out = Vec::new(); + draw_to(&mut out, prompt, &chars, cursor, state).unwrap(); + String::from_utf8(out).unwrap() + } + + #[test] + fn redraw_moves_up_by_the_caret_row_not_the_block_height() { + let mut state = DrawState::default(); + + // First draw with the caret at the end of a two-line buffer. + let first = drawn("> ", "one\ntwo", 7, &mut state); + assert_eq!(state.cursor_row, 1, "caret ends on the second row"); + assert!(!first.contains("\u{1b}[1A"), "first draw must not move up"); + + // Move the caret to the first row. The previous caret was on row 1, so + // the redraw may move up exactly one row — not `rows - 1` from row 0, + // which would land on the line above the prompt and erase it. + let second = drawn("> ", "one\ntwo", 0, &mut state); + assert!( + second.starts_with("\u{1b}[1A"), + "expected one MoveUp, got {:?}", + second + ); + assert_eq!(state.cursor_row, 0); + + // Drawing again with the caret already on row 0 must not move up + // *before* clearing; the only MoveUp is the one after writing, which + // puts the caret back on the first row. + let third = drawn("> ", "one\ntwo", 0, &mut state); + assert!( + !third.starts_with("\u{1b}[1A"), + "must not move up before clearing: {third:?}" + ); + assert_eq!( + third.matches("\u{1b}[1A").count(), + 1, + "exactly one repositioning move after writing: {third:?}" + ); + } + #[test] fn layout_counts_wide_and_combining_characters() { let wide: Vec = "日本語".chars().collect(); -- cgit v1.2.3