tui: fix cursor position to use character count, not byte count

self.cursor is a byte index into the string. When scanning the buffer,
we need to compare character positions, not byte positions or widths.

Convert self.cursor to a character count before comparing with the
buffer scan. Count each non-empty cell as 1 character (the buffer
already represents visual cells, so width doesn't matter here).
This commit is contained in:
ProofOfConcept 2026-03-19 00:46:17 -04:00
parent 6a7ec9732b
commit 1fa298cbdd

View file

@ -821,7 +821,11 @@ impl App {
// Cursor position: scan the rendered buffer to find where the cursor should be.
// This matches ratatui's actual word wrapping instead of trying to simulate it.
let buffer = frame.buffer_mut();
let cursor_char = prompt.len() + self.cursor; // Total chars from start (prompt + input)
// Convert byte index to character index for the input portion
let input_chars_before_cursor = self.input[..self.cursor].chars().count();
let cursor_char_pos = prompt.chars().count() + input_chars_before_cursor;
let mut char_count = 0usize;
let mut cursor_x = input_area.x;
let mut cursor_y = input_area.y;
@ -833,14 +837,13 @@ impl App {
let symbol = cell.symbol();
// Count visible characters (skip zero-width and empty)
if !symbol.is_empty() {
let width = symbol.width();
if char_count + width > cursor_char {
if char_count == cursor_char_pos {
// Found the cursor position
cursor_x = x;
cursor_y = y;
break;
}
char_count += width;
char_count += 1;
}
}
}