aboutsummaryrefslogtreecommitdiffstats
path: root/src/line.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 17:13:24 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 17:14:22 +0100
commitb7b01494926a134f39daf8102281889702ddf62b (patch)
tree813aeefd68352968b363aa7328f81eec796b8287 /src/line.rs
parent0.5.2: lazy-wrap redraw, and reasoning ends its own line (diff)
downloadagent686-b7b01494926a134f39daf8102281889702ddf62b.tar.gz
agent686-b7b01494926a134f39daf8102281889702ddf62b.zip
0.5.3: session locking, UTF-8 and size guards for edit, correct character widths
- exclusive flock on session files - edit refuses non-UTF-8 and >64 MiB files with actionable messages - editor uses unicode-width, expands tabs, handles resize
Diffstat (limited to 'src/line.rs')
-rw-r--r--src/line.rs45
1 files changed, 44 insertions, 1 deletions
diff --git a/src/line.rs b/src/line.rs
index accf6cd..1ae210a 100644
--- a/src/line.rs
+++ b/src/line.rs
@@ -57,6 +57,14 @@ impl Editor {
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 rows)?;
59 } 59 }
60 Event::Resize(_, _) => {
61 // The terminal reflowed the block we were tracking, so the
62 // old row count is meaningless. Start a fresh prompt line
63 // rather than risk erasing output above it.
64 println!();
65 rows = 1;
66 draw(prompt, &buffer, cursor, &mut rows)?;
67 }
60 Event::Key(key) if key.kind == KeyEventKind::Press => { 68 Event::Key(key) if key.kind == KeyEventKind::Press => {
61 match handle_key(key, &mut buffer, &mut cursor, self) { 69 match handle_key(key, &mut buffer, &mut cursor, self) {
62 Action::Continue => {} 70 Action::Continue => {}
@@ -266,7 +274,17 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u
266 col = 0; 274 col = 0;
267 pending_wrap = false; 275 pending_wrap = false;
268 } 276 }
269 col += 1; 277
278 let cells = cells(ch, col);
279 if cells == 0 {
280 // Combining marks draw on top of the previous cell.
281 continue;
282 }
283 if col + cells > width {
284 row += 1;
285 col = 0;
286 }
287 col += cells;
270 if col >= width { 288 if col >= width {
271 col = width - 1; 289 col = width - 1;
272 pending_wrap = true; 290 pending_wrap = true;
@@ -276,6 +294,15 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u
276 (row, col) 294 (row, col)
277} 295}
278 296
297/// Columns a character occupies at the given cursor position. Terminals expand
298/// a tab to the next multiple of eight; wide characters occupy two cells.
299fn cells(ch: char, col: usize) -> usize {
300 if ch == '\t' {
301 return 8 - (col % 8);
302 }
303 unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)
304}
305
279fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { 306fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> {
280 let width = terminal_width(); 307 let width = terminal_width();
281 let mut out = io::stdout(); 308 let mut out = io::stdout();
@@ -407,6 +434,22 @@ mod tests {
407 } 434 }
408 435
409 #[test] 436 #[test]
437 fn layout_counts_wide_and_combining_characters() {
438 let wide: Vec<char> = "日本語".chars().collect();
439 assert_eq!(layout("> ", &wide, 3, 20), (0, 8));
440
441 let combining: Vec<char> = "e\u{0301}".chars().collect();
442 assert_eq!(layout("", &combining, 2, 20), (0, 1));
443 }
444
445 #[test]
446 fn layout_expands_tabs_to_the_next_stop() {
447 let tab: Vec<char> = "\t".chars().collect();
448 assert_eq!(layout("> ", &tab, 1, 80), (0, 8));
449 assert_eq!(layout("", &tab, 1, 80), (0, 8));
450 }
451
452 #[test]
410 fn insert_normalises_line_endings() { 453 fn insert_normalises_line_endings() {
411 let mut buffer = vec!['a']; 454 let mut buffer = vec!['a'];
412 let mut cursor = 1; 455 let mut cursor = 1;