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 --- CHANGELOG.md | 13 +++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/line.rs | 90 ++++++++++++++++++++++++++------------------ src/ui.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++++----------- twocalls.log | 0 6 files changed, 168 insertions(+), 60 deletions(-) delete mode 100644 twocalls.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 928f05a..38d38e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ break the interface, **PATCH** for bug fixes. file, and the tool schemas the model sees. Anything a script or a resumed session could depend on counts as API. +## 0.5.2 + +### Fixed +- A prompt that exactly filled a terminal row made the next redraw move up one + row too many and erase the line above it, which is usually the model's previous + output. Terminals wrap lazily: filling the last column leaves the cursor in that + column until the next character. The layout now models that, and is unit-tested + at the boundary. +- Reasoning and the answer could be written on the same line, because a reasoning + stream does not always end with a newline. The renderer now closes the reasoning + block on its own line, and does not add a blank line when the reasoning already + ended with one. The renderer is generic over its writer, so this is tested. + ## 0.5.1 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 46e4610..6ab1c18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "agent686" -version = "0.5.1" +version = "0.5.2" dependencies = [ "crossterm", "libc", diff --git a/Cargo.toml b/Cargo.toml index 4c04ee7..14e7bc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "agent686" -version = "0.5.1" +version = "0.5.2" edition = "2021" description = "A small coding agent for a 32-bit Debian box" publish = false 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] diff --git a/src/ui.rs b/src/ui.rs index 69e7aa9..3148df9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -33,53 +33,79 @@ fn paint(code: &str, text: &str) -> String { /// Streams to stdout. Colour is used only when stdout is a terminal, so piping /// the output somewhere gives clean text. -pub struct Renderer { +/// +/// Generic over the writer so the line-break rules can be tested without a +/// terminal: a reasoning stream does not always end with a newline, and the +/// answer must not be appended to the same line as the thinking. +pub struct Renderer { + out: W, + color: bool, dimmed: bool, - wrote_text: bool, + line_dirty: bool, } -impl Renderer { +impl Renderer { pub fn new() -> Self { Self { + out: std::io::stdout(), + color: color(), dimmed: false, - wrote_text: false, + line_dirty: false, + } + } +} + +impl Renderer { + pub fn with_color(out: W, color: bool) -> Self { + Self { + out, + color, + dimmed: false, + line_dirty: false, } } - fn undim(&mut self) { - if self.dimmed { - print!("\x1b[0m"); - self.dimmed = false; + fn close_reasoning(&mut self) { + if !self.dimmed { + return; + } + let _ = write!(self.out, "\x1b[0m"); + if self.line_dirty { + let _ = writeln!(self.out); + self.line_dirty = false; } + self.dimmed = false; } } -impl Sink for Renderer { +impl Sink for Renderer { fn text(&mut self, s: &str) { - self.undim(); - print!("{s}"); - let _ = std::io::stdout().flush(); - self.wrote_text = true; + self.close_reasoning(); + let _ = write!(self.out, "{s}"); + let _ = self.out.flush(); + self.line_dirty = !s.ends_with('\n'); } fn reasoning(&mut self, s: &str) { - if !color() { + if !self.color { return; } if !self.dimmed { - print!("\x1b[2m"); + let _ = write!(self.out, "\x1b[2m"); self.dimmed = true; } - print!("{s}"); - let _ = std::io::stdout().flush(); + let _ = write!(self.out, "{s}"); + let _ = self.out.flush(); + self.line_dirty = !s.ends_with('\n'); } fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { - self.undim(); - if self.wrote_text { - println!(); - self.wrote_text = false; + self.close_reasoning(); + if self.line_dirty { + let _ = writeln!(self.out); + self.line_dirty = false; } + let _ = self.out.flush(); } } @@ -234,3 +260,56 @@ impl Approver { == Answer::Yes } } + +#[cfg(test)] +mod tests { + use super::*; + + fn rendered(color: bool, run: impl FnOnce(&mut Renderer>)) -> String { + let mut renderer = Renderer::with_color(Vec::new(), color); + run(&mut renderer); + String::from_utf8(renderer.out).unwrap() + } + + #[test] + fn the_answer_starts_on_its_own_line_after_reasoning() { + let text = rendered(true, |renderer| { + renderer.reasoning("thinking without a newline"); + renderer.text("the answer"); + renderer.done(&Outcome::default(), Duration::ZERO); + }); + assert_eq!( + text, + "\u{1b}[2mthinking without a newline\u{1b}[0m\nthe answer\n" + ); + } + + #[test] + fn reasoning_that_already_ended_with_a_newline_gets_no_blank_line() { + let text = rendered(true, |renderer| { + renderer.reasoning("thinking\n"); + renderer.text("answer"); + renderer.done(&Outcome::default(), Duration::ZERO); + }); + assert_eq!(text, "\u{1b}[2mthinking\n\u{1b}[0manswer\n"); + } + + #[test] + fn reasoning_alone_still_ends_the_line() { + let text = rendered(true, |renderer| { + renderer.reasoning("only thinking"); + renderer.done(&Outcome::default(), Duration::ZERO); + }); + assert_eq!(text, "\u{1b}[2monly thinking\u{1b}[0m\n"); + } + + #[test] + fn reasoning_is_dropped_when_colour_is_off() { + let text = rendered(false, |renderer| { + renderer.reasoning("invisible"); + renderer.text("answer"); + renderer.done(&Outcome::default(), Duration::ZERO); + }); + assert_eq!(text, "answer\n"); + } +} diff --git a/twocalls.log b/twocalls.log deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3