diff options
| -rw-r--r-- | CHANGELOG.md | 11 | ||||
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rwxr-xr-x | scripts/ux-check.sh | 86 | ||||
| -rw-r--r-- | src/line.rs | 93 |
5 files changed, 179 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fd94b9..d33608d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
| @@ -7,6 +7,17 @@ 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.6.1 | ||
| 11 | |||
| 12 | ### Fixed | ||
| 13 | - The redraw moved up by the number of rows the prompt block occupied, but the | ||
| 14 | caret can be on any of those rows. With a two-line prompt and the caret moved | ||
| 15 | up (Home, Left, Up-history), the next redraw overshot by a row and cleared the | ||
| 16 | line above the prompt — usually the model's previous output. The editor now | ||
| 17 | tracks which row the caret is on and moves up by that. | ||
| 18 | - Added `scripts/ux-check.sh`: drives the editor inside tmux and asserts on the | ||
| 19 | rendered screen. Byte-level tests could not see this class of bug. | ||
| 20 | |||
| 10 | ## 0.6.0 | 21 | ## 0.6.0 |
| 11 | 22 | ||
| 12 | ### Added | 23 | ### Added |
| @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" | |||
| 10 | 10 | ||
| 11 | [[package]] | 11 | [[package]] |
| 12 | name = "agent686" | 12 | name = "agent686" |
| 13 | version = "0.6.0" | 13 | version = "0.6.1" |
| 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.6.0" | 3 | version = "0.6.1" |
| 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/scripts/ux-check.sh b/scripts/ux-check.sh new file mode 100755 index 0000000..019f63d --- /dev/null +++ b/scripts/ux-check.sh | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | #!/usr/bin/env bash | ||
| 2 | # Drives the editor on a real terminal (tmux) and checks the things byte-level | ||
| 3 | # tests cannot see: that a multi-line prompt is drawn where it belongs, and that | ||
| 4 | # moving the caret inside it does not erase the output above. | ||
| 5 | set -uo pipefail | ||
| 6 | |||
| 7 | here=$(cd "$(dirname "$0")/.." && pwd) | ||
| 8 | port=${PORT:-8791} | ||
| 9 | session=agent686-ux | ||
| 10 | work=$(mktemp -d) | ||
| 11 | mock_pid= | ||
| 12 | fail=0 | ||
| 13 | |||
| 14 | cleanup() { | ||
| 15 | tmux kill-session -t "$session" 2>/dev/null | ||
| 16 | [ -n "$mock_pid" ] && kill "$mock_pid" 2>/dev/null | ||
| 17 | rm -rf "$work" | ||
| 18 | } | ||
| 19 | trap cleanup EXIT | ||
| 20 | |||
| 21 | fail_msg() { | ||
| 22 | echo "FAIL: $*" >&2 | ||
| 23 | fail=1 | ||
| 24 | } | ||
| 25 | |||
| 26 | python3 "$here/scripts/mock-provider.py" --port "$port" --quiet --delay 0 >/dev/null 2>&1 & | ||
| 27 | mock_pid=$! | ||
| 28 | sleep 0.6 | ||
| 29 | |||
| 30 | tmux kill-session -t "$session" 2>/dev/null | ||
| 31 | tmux new-session -d -s "$session" -x 80 -y 24 | ||
| 32 | tmux send-keys -t "$session" \ | ||
| 33 | "clear; cd $work && AGENT686_DATA=$work/.data OPENCODE_API_KEY=test \ | ||
| 34 | $here/target/debug/agent686 --yolo --base-url http://127.0.0.1:$port/v1" Enter | ||
| 35 | sleep 1.5 | ||
| 36 | |||
| 37 | # One turn so there is output above the prompt that must survive. | ||
| 38 | tmux send-keys -t "$session" "hi" Enter | ||
| 39 | sleep 1.2 | ||
| 40 | screen=$(tmux capture-pane -p -t "$session") | ||
| 41 | echo "$screen" | grep -q "Hello from the mock provider" || fail_msg "the turn produced no answer" | ||
| 42 | grep_line=$(echo "$screen" | grep -n "Hello from the mock provider" | head -1 | cut -d: -f1) | ||
| 43 | |||
| 44 | # Two-line prompt, no trailing newline. | ||
| 45 | printf 'line one\nline two' | tmux load-buffer -b uxcheck - | ||
| 46 | tmux paste-buffer -p -b uxcheck -t "$session" | ||
| 47 | sleep 0.5 | ||
| 48 | paste_y=$(tmux display-message -p -t "$session" '#{cursor_y}') | ||
| 49 | |||
| 50 | tmux send-keys -t "$session" Home | ||
| 51 | sleep 0.4 | ||
| 52 | home_y=$(tmux display-message -p -t "$session" '#{cursor_y}') | ||
| 53 | [ "$home_y" -eq $((paste_y - 1)) ] || fail_msg "Home put the caret on row $home_y, expected $((paste_y - 1))" | ||
| 54 | |||
| 55 | tmux send-keys -t "$session" Right | ||
| 56 | sleep 0.4 | ||
| 57 | right_y=$(tmux display-message -p -t "$session" '#{cursor_y}') | ||
| 58 | [ "$right_y" -eq "$home_y" ] || fail_msg "Right moved the caret to row $right_y, expected $home_y" | ||
| 59 | |||
| 60 | screen=$(tmux capture-pane -p -t "$session") | ||
| 61 | if ! echo "$screen" | grep -q "Hello from the mock provider"; then | ||
| 62 | fail_msg "moving the caret inside a two-line prompt erased the output above it" | ||
| 63 | fi | ||
| 64 | if ! echo "$screen" | grep -q "line two"; then | ||
| 65 | fail_msg "the second prompt line disappeared" | ||
| 66 | fi | ||
| 67 | if ! echo "$screen" | grep -q "^> line one"; then | ||
| 68 | fail_msg "the first prompt line is not drawn with its prompt" | ||
| 69 | fi | ||
| 70 | |||
| 71 | # Submitting must not duplicate the block or leave the answer mid-line. | ||
| 72 | tmux send-keys -t "$session" Enter | ||
| 73 | sleep 1.0 | ||
| 74 | screen=$(tmux capture-pane -p -t "$session") | ||
| 75 | [ "$(echo "$screen" | grep -c '^line two$')" -eq 1 ] \ | ||
| 76 | || fail_msg "the submitted prompt should appear exactly once" | ||
| 77 | if echo "$screen" | grep -q 'line two.*Hello from the mock'; then | ||
| 78 | fail_msg "the answer was written on the same line as the prompt" | ||
| 79 | fi | ||
| 80 | |||
| 81 | if [ "$fail" -ne 0 ]; then | ||
| 82 | echo "--- final screen ---" >&2 | ||
| 83 | echo "$screen" >&2 | ||
| 84 | exit 1 | ||
| 85 | fi | ||
| 86 | echo "ux-check: ok (two-line prompt, caret movement, output above intact)" | ||
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 { | |||
| 43 | let guard = RawGuard::enable()?; | 43 | let guard = RawGuard::enable()?; |
| 44 | let mut buffer: Vec<char> = Vec::new(); | 44 | let mut buffer: Vec<char> = Vec::new(); |
| 45 | let mut cursor = 0usize; | 45 | let mut cursor = 0usize; |
| 46 | let mut rows = 1usize; | 46 | let mut state = DrawState::default(); |
| 47 | 47 | ||
| 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 | draw(prompt, &buffer, cursor, &mut rows)?; | 51 | draw(prompt, &buffer, cursor, &mut state)?; |
| 52 | 52 | ||
| 53 | loop { | 53 | loop { |
| 54 | let event = event::read()?; | 54 | let event = event::read()?; |
| 55 | match event { | 55 | match event { |
| 56 | Event::Paste(text) => { | 56 | Event::Paste(text) => { |
| 57 | insert(&mut buffer, &mut cursor, &text); | 57 | insert(&mut buffer, &mut cursor, &text); |
| 58 | draw(prompt, &buffer, cursor, &mut rows)?; | 58 | draw(prompt, &buffer, cursor, &mut state)?; |
| 59 | } | 59 | } |
| 60 | Event::Resize(_, _) => { | 60 | Event::Resize(_, _) => { |
| 61 | // The terminal reflowed the block we were tracking, so the | 61 | // The terminal reflowed the block we were tracking, so the |
| 62 | // old row count is meaningless. Start a fresh prompt line | 62 | // old row count is meaningless. Start a fresh prompt line |
| 63 | // rather than risk erasing output above it. | 63 | // rather than risk erasing output above it. |
| 64 | println!(); | 64 | println!(); |
| 65 | rows = 1; | 65 | state = DrawState::default(); |
| 66 | draw(prompt, &buffer, cursor, &mut rows)?; | 66 | draw(prompt, &buffer, cursor, &mut state)?; |
| 67 | } | 67 | } |
| 68 | Event::Key(key) if key.kind == KeyEventKind::Press => { | 68 | Event::Key(key) if key.kind == KeyEventKind::Press => { |
| 69 | match handle_key(key, &mut buffer, &mut cursor, self) { | 69 | match handle_key(key, &mut buffer, &mut cursor, self) { |
| @@ -71,7 +71,7 @@ impl Editor { | |||
| 71 | Action::Submit => { | 71 | Action::Submit => { |
| 72 | // Redraw with the caret at the end so the submitted | 72 | // Redraw with the caret at the end so the submitted |
| 73 | // line looks complete before we move on. | 73 | // line looks complete before we move on. |
| 74 | draw(prompt, &buffer, buffer.len(), &mut rows)?; | 74 | draw(prompt, &buffer, buffer.len(), &mut state)?; |
| 75 | drop(guard); | 75 | drop(guard); |
| 76 | let line: String = buffer.iter().collect(); | 76 | let line: String = buffer.iter().collect(); |
| 77 | self.remember(&line); | 77 | self.remember(&line); |
| @@ -89,7 +89,7 @@ impl Editor { | |||
| 89 | return Ok(Some(String::new())); | 89 | return Ok(Some(String::new())); |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | draw(prompt, &buffer, cursor, &mut rows)?; | 92 | draw(prompt, &buffer, cursor, &mut state)?; |
| 93 | } | 93 | } |
| 94 | _ => {} | 94 | _ => {} |
| 95 | } | 95 | } |
| @@ -303,15 +303,40 @@ fn cells(ch: char, col: usize) -> usize { | |||
| 303 | unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) | 303 | unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) |
| 304 | } | 304 | } |
| 305 | 305 | ||
| 306 | fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { | 306 | /// Where the caret physically is, relative to the first row of the input block. |
| 307 | let width = terminal_width(); | 307 | /// The previous draw can leave the caret on any row, so a redraw must move up by |
| 308 | /// that row, not by the number of rows the block occupies. | ||
| 309 | #[derive(Default, Clone, Copy)] | ||
| 310 | struct DrawState { | ||
| 311 | rows: usize, | ||
| 312 | cursor_row: usize, | ||
| 313 | } | ||
| 314 | |||
| 315 | fn draw( | ||
| 316 | prompt: &str, | ||
| 317 | buffer: &[char], | ||
| 318 | cursor: usize, | ||
| 319 | state: &mut DrawState, | ||
| 320 | ) -> io::Result<()> { | ||
| 308 | let mut out = io::stdout(); | 321 | let mut out = io::stdout(); |
| 322 | draw_to(&mut out, prompt, buffer, cursor, state)?; | ||
| 323 | out.flush() | ||
| 324 | } | ||
| 325 | |||
| 326 | fn draw_to<W: Write>( | ||
| 327 | out: &mut W, | ||
| 328 | prompt: &str, | ||
| 329 | buffer: &[char], | ||
| 330 | cursor: usize, | ||
| 331 | state: &mut DrawState, | ||
| 332 | ) -> io::Result<()> { | ||
| 333 | let width = terminal_width(); | ||
| 309 | 334 | ||
| 310 | let (end_row, _) = layout(prompt, buffer, buffer.len(), width); | 335 | let (end_row, _) = layout(prompt, buffer, buffer.len(), width); |
| 311 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); | 336 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); |
| 312 | 337 | ||
| 313 | if *rows > 1 { | 338 | if state.cursor_row > 0 { |
| 314 | execute!(out, MoveUp(clamp(*rows - 1)))?; | 339 | execute!(out, MoveUp(clamp(state.cursor_row)))?; |
| 315 | } | 340 | } |
| 316 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; | 341 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; |
| 317 | 342 | ||
| @@ -330,9 +355,9 @@ fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::R | |||
| 330 | execute!(out, MoveUp(clamp(end_row - cursor_row)))?; | 355 | execute!(out, MoveUp(clamp(end_row - cursor_row)))?; |
| 331 | } | 356 | } |
| 332 | execute!(out, MoveToColumn(clamp(cursor_col)))?; | 357 | execute!(out, MoveToColumn(clamp(cursor_col)))?; |
| 333 | out.flush()?; | ||
| 334 | 358 | ||
| 335 | *rows = end_row + 1; | 359 | state.rows = end_row + 1; |
| 360 | state.cursor_row = cursor_row; | ||
| 336 | Ok(()) | 361 | Ok(()) |
| 337 | } | 362 | } |
| 338 | 363 | ||
| @@ -433,6 +458,48 @@ mod tests { | |||
| 433 | assert!(terminal_width() >= 1); | 458 | assert!(terminal_width() >= 1); |
| 434 | } | 459 | } |
| 435 | 460 | ||
| 461 | fn drawn(prompt: &str, buffer: &str, cursor: usize, state: &mut DrawState) -> String { | ||
| 462 | let chars: Vec<char> = buffer.chars().collect(); | ||
| 463 | let mut out = Vec::new(); | ||
| 464 | draw_to(&mut out, prompt, &chars, cursor, state).unwrap(); | ||
| 465 | String::from_utf8(out).unwrap() | ||
| 466 | } | ||
| 467 | |||
| 468 | #[test] | ||
| 469 | fn redraw_moves_up_by_the_caret_row_not_the_block_height() { | ||
| 470 | let mut state = DrawState::default(); | ||
| 471 | |||
| 472 | // First draw with the caret at the end of a two-line buffer. | ||
| 473 | let first = drawn("> ", "one\ntwo", 7, &mut state); | ||
| 474 | assert_eq!(state.cursor_row, 1, "caret ends on the second row"); | ||
| 475 | assert!(!first.contains("\u{1b}[1A"), "first draw must not move up"); | ||
| 476 | |||
| 477 | // Move the caret to the first row. The previous caret was on row 1, so | ||
| 478 | // the redraw may move up exactly one row — not `rows - 1` from row 0, | ||
| 479 | // which would land on the line above the prompt and erase it. | ||
| 480 | let second = drawn("> ", "one\ntwo", 0, &mut state); | ||
| 481 | assert!( | ||
| 482 | second.starts_with("\u{1b}[1A"), | ||
| 483 | "expected one MoveUp, got {:?}", | ||
| 484 | second | ||
| 485 | ); | ||
| 486 | assert_eq!(state.cursor_row, 0); | ||
| 487 | |||
| 488 | // Drawing again with the caret already on row 0 must not move up | ||
| 489 | // *before* clearing; the only MoveUp is the one after writing, which | ||
| 490 | // puts the caret back on the first row. | ||
| 491 | let third = drawn("> ", "one\ntwo", 0, &mut state); | ||
| 492 | assert!( | ||
| 493 | !third.starts_with("\u{1b}[1A"), | ||
| 494 | "must not move up before clearing: {third:?}" | ||
| 495 | ); | ||
| 496 | assert_eq!( | ||
| 497 | third.matches("\u{1b}[1A").count(), | ||
| 498 | 1, | ||
| 499 | "exactly one repositioning move after writing: {third:?}" | ||
| 500 | ); | ||
| 501 | } | ||
| 502 | |||
| 436 | #[test] | 503 | #[test] |
| 437 | fn layout_counts_wide_and_combining_characters() { | 504 | fn layout_counts_wide_and_combining_characters() { |
| 438 | let wide: Vec<char> = "日本語".chars().collect(); | 505 | let wide: Vec<char> = "日本語".chars().collect(); |
