From b7b01494926a134f39daf8102281889702ddf62b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:13:24 +0100 Subject: 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 --- src/line.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/line.rs') 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 { insert(&mut buffer, &mut cursor, &text); draw(prompt, &buffer, cursor, &mut rows)?; } + Event::Resize(_, _) => { + // The terminal reflowed the block we were tracking, so the + // old row count is meaningless. Start a fresh prompt line + // rather than risk erasing output above it. + println!(); + rows = 1; + draw(prompt, &buffer, cursor, &mut rows)?; + } Event::Key(key) if key.kind == KeyEventKind::Press => { match handle_key(key, &mut buffer, &mut cursor, self) { Action::Continue => {} @@ -266,7 +274,17 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u col = 0; pending_wrap = false; } - col += 1; + + let cells = cells(ch, col); + if cells == 0 { + // Combining marks draw on top of the previous cell. + continue; + } + if col + cells > width { + row += 1; + col = 0; + } + col += cells; if col >= width { col = width - 1; pending_wrap = true; @@ -276,6 +294,15 @@ fn layout(prompt: &str, buffer: &[char], upto: usize, width: usize) -> (usize, u (row, col) } +/// Columns a character occupies at the given cursor position. Terminals expand +/// a tab to the next multiple of eight; wide characters occupy two cells. +fn cells(ch: char, col: usize) -> usize { + if ch == '\t' { + return 8 - (col % 8); + } + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) +} + fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { let width = terminal_width(); let mut out = io::stdout(); @@ -406,6 +433,22 @@ mod tests { assert!(terminal_width() >= 1); } + #[test] + fn layout_counts_wide_and_combining_characters() { + let wide: Vec = "日本語".chars().collect(); + assert_eq!(layout("> ", &wide, 3, 20), (0, 8)); + + let combining: Vec = "e\u{0301}".chars().collect(); + assert_eq!(layout("", &combining, 2, 20), (0, 1)); + } + + #[test] + fn layout_expands_tabs_to_the_next_stop() { + let tab: Vec = "\t".chars().collect(); + assert_eq!(layout("> ", &tab, 1, 80), (0, 8)); + assert_eq!(layout("", &tab, 1, 80), (0, 8)); + } + #[test] fn insert_normalises_line_endings() { let mut buffer = vec!['a']; -- cgit v1.2.3