diff options
Diffstat (limited to '')
| -rw-r--r-- | CHANGELOG.md | 13 | ||||
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/line.rs | 90 | ||||
| -rw-r--r-- | src/ui.rs | 121 | ||||
| -rw-r--r-- | twocalls.log | 0 |
6 files changed, 168 insertions, 60 deletions
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. | |||
| 7 | file, and the tool schemas the model sees. Anything a script or a resumed session | 7 | file, and the tool schemas the model sees. Anything a script or a resumed session |
| 8 | could depend on counts as API. | 8 | could depend on counts as API. |
| 9 | 9 | ||
| 10 | ## 0.5.2 | ||
| 11 | |||
| 12 | ### Fixed | ||
| 13 | - A prompt that exactly filled a terminal row made the next redraw move up one | ||
| 14 | row too many and erase the line above it, which is usually the model's previous | ||
| 15 | output. Terminals wrap lazily: filling the last column leaves the cursor in that | ||
| 16 | column until the next character. The layout now models that, and is unit-tested | ||
| 17 | at the boundary. | ||
| 18 | - Reasoning and the answer could be written on the same line, because a reasoning | ||
| 19 | stream does not always end with a newline. The renderer now closes the reasoning | ||
| 20 | block on its own line, and does not add a blank line when the reasoning already | ||
| 21 | ended with one. The renderer is generic over its writer, so this is tested. | ||
| 22 | |||
| 10 | ## 0.5.1 | 23 | ## 0.5.1 |
| 11 | 24 | ||
| 12 | ### Fixed | 25 | ### Fixed |
| @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" | |||
| 10 | 10 | ||
| 11 | [[package]] | 11 | [[package]] |
| 12 | name = "agent686" | 12 | name = "agent686" |
| 13 | version = "0.5.1" | 13 | version = "0.5.2" |
| 14 | dependencies = [ | 14 | dependencies = [ |
| 15 | "crossterm", | 15 | "crossterm", |
| 16 | "libc", | 16 | "libc", |
| @@ -1,6 +1,6 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "agent686" | 2 | name = "agent686" |
| 3 | version = "0.5.1" | 3 | version = "0.5.2" |
| 4 | edition = "2021" | 4 | edition = "2021" |
| 5 | description = "A small coding agent for a 32-bit Debian box" | 5 | description = "A small coding agent for a 32-bit Debian box" |
| 6 | publish = false | 6 | 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 { | |||
| 48 | self.history_index = self.history.len(); | 48 | self.history_index = self.history.len(); |
| 49 | self.draft.clear(); | 49 | self.draft.clear(); |
| 50 | 50 | ||
| 51 | let width = terminal_width(); | 51 | draw(prompt, &buffer, cursor, &mut rows)?; |
| 52 | draw(prompt, &buffer, cursor, width, &mut rows)?; | ||
| 53 | 52 | ||
| 54 | loop { | 53 | loop { |
| 55 | let event = event::read()?; | 54 | let event = event::read()?; |
| 56 | match event { | 55 | match event { |
| 57 | Event::Paste(text) => { | 56 | Event::Paste(text) => { |
| 58 | insert(&mut buffer, &mut cursor, &text); | 57 | insert(&mut buffer, &mut cursor, &text); |
| 59 | draw(prompt, &buffer, cursor, width, &mut rows)?; | 58 | draw(prompt, &buffer, cursor, &mut rows)?; |
| 60 | } | 59 | } |
| 61 | Event::Key(key) if key.kind == KeyEventKind::Press => { | 60 | Event::Key(key) if key.kind == KeyEventKind::Press => { |
| 62 | match handle_key(key, &mut buffer, &mut cursor, self) { | 61 | match handle_key(key, &mut buffer, &mut cursor, self) { |
| @@ -64,7 +63,7 @@ impl Editor { | |||
| 64 | Action::Submit => { | 63 | Action::Submit => { |
| 65 | // Redraw with the caret at the end so the submitted | 64 | // Redraw with the caret at the end so the submitted |
| 66 | // line looks complete before we move on. | 65 | // line looks complete before we move on. |
| 67 | draw(prompt, &buffer, buffer.len(), width, &mut rows)?; | 66 | draw(prompt, &buffer, buffer.len(), &mut rows)?; |
| 68 | drop(guard); | 67 | drop(guard); |
| 69 | let line: String = buffer.iter().collect(); | 68 | let line: String = buffer.iter().collect(); |
| 70 | self.remember(&line); | 69 | self.remember(&line); |
| @@ -82,7 +81,7 @@ impl Editor { | |||
| 82 | return Ok(Some(String::new())); | 81 | return Ok(Some(String::new())); |
| 83 | } | 82 | } |
| 84 | } | 83 | } |
| 85 | draw(prompt, &buffer, cursor, width, &mut rows)?; | 84 | draw(prompt, &buffer, cursor, &mut rows)?; |
| 86 | } | 85 | } |
| 87 | _ => {} | 86 | _ => {} |
| 88 | } | 87 | } |
| @@ -241,43 +240,51 @@ fn terminal_width() -> usize { | |||
| 241 | } | 240 | } |
| 242 | 241 | ||
| 243 | /// Position of the cursor after consuming `prompt` plus `buffer[..upto]`. | 242 | /// Position of the cursor after consuming `prompt` plus `buffer[..upto]`. |
| 244 | /// Approximates every character as one column; good enough for a prompt line. | 243 | /// |
| 244 | /// Columns are 0-based, matching the cursor-move escape. Wrapping is modelled | ||
| 245 | /// the way terminals actually behave: filling the last column leaves the cursor | ||
| 246 | /// *in* that column, and only the next character wraps. Getting this wrong makes | ||
| 247 | /// the redraw move up one row too many and erase the line above the prompt. | ||
| 245 | fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, usize) { | 248 | fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, usize) { |
| 249 | let width = width.max(1); | ||
| 246 | let mut row = 0usize; | 250 | let mut row = 0usize; |
| 247 | let mut col = 0usize; | 251 | let mut col = 0usize; |
| 248 | for ch in prompt.chars().chain(buffer[..upto.min(buffer.len())].iter().copied()) { | 252 | let mut pending_wrap = false; |
| 249 | advance(ch, &mut row, &mut col, width); | ||
| 250 | } | ||
| 251 | (row, col) | ||
| 252 | } | ||
| 253 | 253 | ||
| 254 | fn advance(ch: char, row: &mut usize, col: &mut usize, width: usize) { | 254 | for ch in prompt |
| 255 | if ch == '\n' { | 255 | .chars() |
| 256 | *row += 1; | 256 | .chain(buffer[..upto.min(buffer.len())].iter().copied()) |
| 257 | *col = 0; | 257 | { |
| 258 | return; | 258 | if ch == '\n' { |
| 259 | } | 259 | row += 1; |
| 260 | *col += 1; | 260 | col = 0; |
| 261 | if *col >= width { | 261 | pending_wrap = false; |
| 262 | *col = 0; | 262 | continue; |
| 263 | *row += 1; | 263 | } |
| 264 | if pending_wrap { | ||
| 265 | row += 1; | ||
| 266 | col = 0; | ||
| 267 | pending_wrap = false; | ||
| 268 | } | ||
| 269 | col += 1; | ||
| 270 | if col >= width { | ||
| 271 | col = width - 1; | ||
| 272 | pending_wrap = true; | ||
| 273 | } | ||
| 264 | } | 274 | } |
| 275 | |||
| 276 | (row, col) | ||
| 265 | } | 277 | } |
| 266 | 278 | ||
| 267 | fn draw( | 279 | fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { |
| 268 | prompt: &str, | 280 | let width = terminal_width(); |
| 269 | buffer: &[char], | ||
| 270 | cursor: usize, | ||
| 271 | width: usize, | ||
| 272 | rows: &mut usize, | ||
| 273 | ) -> io::Result<()> { | ||
| 274 | let mut out = io::stdout(); | 281 | let mut out = io::stdout(); |
| 275 | 282 | ||
| 276 | let total = layout(prompt, buffer, buffer.len(), width).0 + 1; | 283 | let (end_row, _) = layout(prompt, buffer, buffer.len(), width); |
| 277 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); | 284 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); |
| 278 | 285 | ||
| 279 | if *rows > 1 { | 286 | if *rows > 1 { |
| 280 | execute!(out, MoveUp((*rows - 1) as u16))?; | 287 | execute!(out, MoveUp(clamp(*rows - 1)))?; |
| 281 | } | 288 | } |
| 282 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; | 289 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; |
| 283 | 290 | ||
| @@ -292,17 +299,20 @@ fn draw( | |||
| 292 | } | 299 | } |
| 293 | out.write_all(text.as_bytes())?; | 300 | out.write_all(text.as_bytes())?; |
| 294 | 301 | ||
| 295 | let end_row = total - 1; | ||
| 296 | if end_row > cursor_row { | 302 | if end_row > cursor_row { |
| 297 | execute!(out, MoveUp((end_row - cursor_row) as u16))?; | 303 | execute!(out, MoveUp(clamp(end_row - cursor_row)))?; |
| 298 | } | 304 | } |
| 299 | execute!(out, MoveToColumn(cursor_col as u16))?; | 305 | execute!(out, MoveToColumn(clamp(cursor_col)))?; |
| 300 | out.flush()?; | 306 | out.flush()?; |
| 301 | 307 | ||
| 302 | *rows = total; | 308 | *rows = end_row + 1; |
| 303 | Ok(()) | 309 | Ok(()) |
| 304 | } | 310 | } |
| 305 | 311 | ||
| 312 | fn clamp(value: usize) -> u16 { | ||
| 313 | value.min(u16::MAX as usize) as u16 | ||
| 314 | } | ||
| 315 | |||
| 306 | struct RawGuard; | 316 | struct RawGuard; |
| 307 | 317 | ||
| 308 | impl RawGuard { | 318 | impl RawGuard { |
| @@ -376,11 +386,17 @@ mod tests { | |||
| 376 | } | 386 | } |
| 377 | 387 | ||
| 378 | #[test] | 388 | #[test] |
| 379 | fn layout_wraps_at_the_terminal_width() { | 389 | fn layout_wraps_lazily_at_the_terminal_width() { |
| 380 | let buffer: Vec<char> = "abcdef".chars().collect(); | 390 | let buffer: Vec<char> = "abcdef".chars().collect(); |
| 381 | // Prompt takes 2 columns, so the 6 chars fill exactly 8. | 391 | // Prompt takes 2 columns, so six more fill the 8-column row exactly. |
| 382 | assert_eq!(layout("> ", &buffer, 6, 8), (1, 0)); | 392 | // The cursor stays in the last column until another character arrives; |
| 393 | // counting a new row here is what used to erase the line above. | ||
| 394 | assert_eq!(layout("> ", &buffer, 6, 8), (0, 7)); | ||
| 383 | assert_eq!(layout("> ", &buffer, 3, 8), (0, 5)); | 395 | assert_eq!(layout("> ", &buffer, 3, 8), (0, 5)); |
| 396 | |||
| 397 | let mut longer = buffer.clone(); | ||
| 398 | longer.push('g'); | ||
| 399 | assert_eq!(layout("> ", &longer, 7, 8), (1, 1)); | ||
| 384 | } | 400 | } |
| 385 | 401 | ||
| 386 | #[test] | 402 | #[test] |
| @@ -33,53 +33,79 @@ fn paint(code: &str, text: &str) -> String { | |||
| 33 | 33 | ||
| 34 | /// Streams to stdout. Colour is used only when stdout is a terminal, so piping | 34 | /// Streams to stdout. Colour is used only when stdout is a terminal, so piping |
| 35 | /// the output somewhere gives clean text. | 35 | /// the output somewhere gives clean text. |
| 36 | pub struct Renderer { | 36 | /// |
| 37 | /// Generic over the writer so the line-break rules can be tested without a | ||
| 38 | /// terminal: a reasoning stream does not always end with a newline, and the | ||
| 39 | /// answer must not be appended to the same line as the thinking. | ||
| 40 | pub struct Renderer<W: Write> { | ||
| 41 | out: W, | ||
| 42 | color: bool, | ||
| 37 | dimmed: bool, | 43 | dimmed: bool, |
| 38 | wrote_text: bool, | 44 | line_dirty: bool, |
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | impl Renderer { | 47 | impl Renderer<std::io::Stdout> { |
| 42 | pub fn new() -> Self { | 48 | pub fn new() -> Self { |
| 43 | Self { | 49 | Self { |
| 50 | out: std::io::stdout(), | ||
| 51 | color: color(), | ||
| 44 | dimmed: false, | 52 | dimmed: false, |
| 45 | wrote_text: false, | 53 | line_dirty: false, |
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | impl<W: Write> Renderer<W> { | ||
| 59 | pub fn with_color(out: W, color: bool) -> Self { | ||
| 60 | Self { | ||
| 61 | out, | ||
| 62 | color, | ||
| 63 | dimmed: false, | ||
| 64 | line_dirty: false, | ||
| 46 | } | 65 | } |
| 47 | } | 66 | } |
| 48 | 67 | ||
| 49 | fn undim(&mut self) { | 68 | fn close_reasoning(&mut self) { |
| 50 | if self.dimmed { | 69 | if !self.dimmed { |
| 51 | print!("\x1b[0m"); | 70 | return; |
| 52 | self.dimmed = false; | 71 | } |
| 72 | let _ = write!(self.out, "\x1b[0m"); | ||
| 73 | if self.line_dirty { | ||
| 74 | let _ = writeln!(self.out); | ||
| 75 | self.line_dirty = false; | ||
| 53 | } | 76 | } |
| 77 | self.dimmed = false; | ||
| 54 | } | 78 | } |
| 55 | } | 79 | } |
| 56 | 80 | ||
| 57 | impl Sink for Renderer { | 81 | impl<W: Write> Sink for Renderer<W> { |
| 58 | fn text(&mut self, s: &str) { | 82 | fn text(&mut self, s: &str) { |
| 59 | self.undim(); | 83 | self.close_reasoning(); |
| 60 | print!("{s}"); | 84 | let _ = write!(self.out, "{s}"); |
| 61 | let _ = std::io::stdout().flush(); | 85 | let _ = self.out.flush(); |
| 62 | self.wrote_text = true; | 86 | self.line_dirty = !s.ends_with('\n'); |
| 63 | } | 87 | } |
| 64 | 88 | ||
| 65 | fn reasoning(&mut self, s: &str) { | 89 | fn reasoning(&mut self, s: &str) { |
| 66 | if !color() { | 90 | if !self.color { |
| 67 | return; | 91 | return; |
| 68 | } | 92 | } |
| 69 | if !self.dimmed { | 93 | if !self.dimmed { |
| 70 | print!("\x1b[2m"); | 94 | let _ = write!(self.out, "\x1b[2m"); |
| 71 | self.dimmed = true; | 95 | self.dimmed = true; |
| 72 | } | 96 | } |
| 73 | print!("{s}"); | 97 | let _ = write!(self.out, "{s}"); |
| 74 | let _ = std::io::stdout().flush(); | 98 | let _ = self.out.flush(); |
| 99 | self.line_dirty = !s.ends_with('\n'); | ||
| 75 | } | 100 | } |
| 76 | 101 | ||
| 77 | fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { | 102 | fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { |
| 78 | self.undim(); | 103 | self.close_reasoning(); |
| 79 | if self.wrote_text { | 104 | if self.line_dirty { |
| 80 | println!(); | 105 | let _ = writeln!(self.out); |
| 81 | self.wrote_text = false; | 106 | self.line_dirty = false; |
| 82 | } | 107 | } |
| 108 | let _ = self.out.flush(); | ||
| 83 | } | 109 | } |
| 84 | } | 110 | } |
| 85 | 111 | ||
| @@ -234,3 +260,56 @@ impl Approver { | |||
| 234 | == Answer::Yes | 260 | == Answer::Yes |
| 235 | } | 261 | } |
| 236 | } | 262 | } |
| 263 | |||
| 264 | #[cfg(test)] | ||
| 265 | mod tests { | ||
| 266 | use super::*; | ||
| 267 | |||
| 268 | fn rendered(color: bool, run: impl FnOnce(&mut Renderer<Vec<u8>>)) -> String { | ||
| 269 | let mut renderer = Renderer::with_color(Vec::new(), color); | ||
| 270 | run(&mut renderer); | ||
| 271 | String::from_utf8(renderer.out).unwrap() | ||
| 272 | } | ||
| 273 | |||
| 274 | #[test] | ||
| 275 | fn the_answer_starts_on_its_own_line_after_reasoning() { | ||
| 276 | let text = rendered(true, |renderer| { | ||
| 277 | renderer.reasoning("thinking without a newline"); | ||
| 278 | renderer.text("the answer"); | ||
| 279 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 280 | }); | ||
| 281 | assert_eq!( | ||
| 282 | text, | ||
| 283 | "\u{1b}[2mthinking without a newline\u{1b}[0m\nthe answer\n" | ||
| 284 | ); | ||
| 285 | } | ||
| 286 | |||
| 287 | #[test] | ||
| 288 | fn reasoning_that_already_ended_with_a_newline_gets_no_blank_line() { | ||
| 289 | let text = rendered(true, |renderer| { | ||
| 290 | renderer.reasoning("thinking\n"); | ||
| 291 | renderer.text("answer"); | ||
| 292 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 293 | }); | ||
| 294 | assert_eq!(text, "\u{1b}[2mthinking\n\u{1b}[0manswer\n"); | ||
| 295 | } | ||
| 296 | |||
| 297 | #[test] | ||
| 298 | fn reasoning_alone_still_ends_the_line() { | ||
| 299 | let text = rendered(true, |renderer| { | ||
| 300 | renderer.reasoning("only thinking"); | ||
| 301 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 302 | }); | ||
| 303 | assert_eq!(text, "\u{1b}[2monly thinking\u{1b}[0m\n"); | ||
| 304 | } | ||
| 305 | |||
| 306 | #[test] | ||
| 307 | fn reasoning_is_dropped_when_colour_is_off() { | ||
| 308 | let text = rendered(false, |renderer| { | ||
| 309 | renderer.reasoning("invisible"); | ||
| 310 | renderer.text("answer"); | ||
| 311 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 312 | }); | ||
| 313 | assert_eq!(text, "answer\n"); | ||
| 314 | } | ||
| 315 | } | ||
diff --git a/twocalls.log b/twocalls.log deleted file mode 100644 index e69de29..0000000 --- a/twocalls.log +++ /dev/null | |||
