diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 17:09:59 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 17:09:59 +0100 |
| commit | a05c142be27f8be9d570e60d58476143cf139d58 (patch) | |
| tree | 285e396084114fd430dc2caa45717663d5b9bdd8 /src/line.rs | |
| parent | 0.5.1: silence the GLIBC_2.39 weak-version warning on the target (diff) | |
| download | agent686-a05c142be27f8be9d570e60d58476143cf139d58.tar.gz agent686-a05c142be27f8be9d570e60d58476143cf139d58.zip | |
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
Diffstat (limited to 'src/line.rs')
| -rw-r--r-- | src/line.rs | 90 |
1 files changed, 53 insertions, 37 deletions
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] |
