From a05c142be27f8be9d570e60d58476143cf139d58 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:09:59 +0100 Subject: 0.5.2: lazy-wrap redraw, and reasoning ends its own line - layout models pending-wrap, so a prompt that fills a row exactly no longer clears the line above on the next redraw - the renderer closes a reasoning block with a newline only when it needs one - renderer is generic over Write, with tests for the line-break rules --- src/line.rs | 90 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 37 deletions(-) (limited to 'src/line.rs') diff --git a/src/line.rs b/src/line.rs index 4989734..accf6cd 100644 --- a/src/line.rs +++ b/src/line.rs @@ -48,15 +48,14 @@ impl Editor { self.history_index = self.history.len(); self.draft.clear(); - let width = terminal_width(); - draw(prompt, &buffer, cursor, width, &mut rows)?; + draw(prompt, &buffer, cursor, &mut rows)?; loop { let event = event::read()?; match event { Event::Paste(text) => { insert(&mut buffer, &mut cursor, &text); - draw(prompt, &buffer, cursor, width, &mut rows)?; + draw(prompt, &buffer, cursor, &mut rows)?; } Event::Key(key) if key.kind == KeyEventKind::Press => { match handle_key(key, &mut buffer, &mut cursor, self) { @@ -64,7 +63,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(), width, &mut rows)?; + draw(prompt, &buffer, buffer.len(), &mut rows)?; drop(guard); let line: String = buffer.iter().collect(); self.remember(&line); @@ -82,7 +81,7 @@ impl Editor { return Ok(Some(String::new())); } } - draw(prompt, &buffer, cursor, width, &mut rows)?; + draw(prompt, &buffer, cursor, &mut rows)?; } _ => {} } @@ -241,43 +240,51 @@ fn terminal_width() -> usize { } /// Position of the cursor after consuming `prompt` plus `buffer[..upto]`. -/// Approximates every character as one column; good enough for a prompt line. +/// +/// Columns are 0-based, matching the cursor-move escape. Wrapping is modelled +/// the way terminals actually behave: filling the last column leaves the cursor +/// *in* that column, and only the next character wraps. Getting this wrong makes +/// the redraw move up one row too many and erase the line above the prompt. fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, usize) { + let width = width.max(1); let mut row = 0usize; let mut col = 0usize; - for ch in prompt.chars().chain(buffer[..upto.min(buffer.len())].iter().copied()) { - advance(ch, &mut row, &mut col, width); - } - (row, col) -} + let mut pending_wrap = false; -fn advance(ch: char, row: &mut usize, col: &mut usize, width: usize) { - if ch == '\n' { - *row += 1; - *col = 0; - return; - } - *col += 1; - if *col >= width { - *col = 0; - *row += 1; + for ch in prompt + .chars() + .chain(buffer[..upto.min(buffer.len())].iter().copied()) + { + if ch == '\n' { + row += 1; + col = 0; + pending_wrap = false; + continue; + } + if pending_wrap { + row += 1; + col = 0; + pending_wrap = false; + } + col += 1; + if col >= width { + col = width - 1; + pending_wrap = true; + } } + + (row, col) } -fn draw( - prompt: &str, - buffer: &[char], - cursor: usize, - width: usize, - rows: &mut usize, -) -> io::Result<()> { +fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { + let width = terminal_width(); let mut out = io::stdout(); - let total = layout(prompt, buffer, buffer.len(), width).0 + 1; + 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((*rows - 1) as u16))?; + execute!(out, MoveUp(clamp(*rows - 1)))?; } execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; @@ -292,17 +299,20 @@ fn draw( } out.write_all(text.as_bytes())?; - let end_row = total - 1; if end_row > cursor_row { - execute!(out, MoveUp((end_row - cursor_row) as u16))?; + execute!(out, MoveUp(clamp(end_row - cursor_row)))?; } - execute!(out, MoveToColumn(cursor_col as u16))?; + execute!(out, MoveToColumn(clamp(cursor_col)))?; out.flush()?; - *rows = total; + *rows = end_row + 1; Ok(()) } +fn clamp(value: usize) -> u16 { + value.min(u16::MAX as usize) as u16 +} + struct RawGuard; impl RawGuard { @@ -376,11 +386,17 @@ mod tests { } #[test] - fn layout_wraps_at_the_terminal_width() { + fn layout_wraps_lazily_at_the_terminal_width() { let buffer: Vec = "abcdef".chars().collect(); - // Prompt takes 2 columns, so the 6 chars fill exactly 8. - assert_eq!(layout("> ", &buffer, 6, 8), (1, 0)); + // Prompt takes 2 columns, so six more fill the 8-column row exactly. + // The cursor stays in the last column until another character arrives; + // counting a new row here is what used to erase the line above. + assert_eq!(layout("> ", &buffer, 6, 8), (0, 7)); assert_eq!(layout("> ", &buffer, 3, 8), (0, 5)); + + let mut longer = buffer.clone(); + longer.push('g'); + assert_eq!(layout("> ", &longer, 7, 8), (1, 1)); } #[test] -- cgit v1.2.3