diff --git a/Cargo.lock b/Cargo.lock index 9b5917c..223f3b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,7 +487,7 @@ version = "0.0.1" dependencies = [ "textwrap", "unicode-segmentation", - "unicode-width", + "unicode-width 0.1.11", ] [[package]] @@ -599,7 +599,7 @@ dependencies = [ "temp-dir", "thiserror", "unicode-segmentation", - "unicode-width", + "unicode-width 0.1.11", ] [[package]] @@ -615,6 +615,7 @@ dependencies = [ "regex", "serde", "serde_json", + "unicode-width 0.2.0", ] [[package]] @@ -850,7 +851,7 @@ dependencies = [ "stability", "strum", "unicode-segmentation", - "unicode-width", + "unicode-width 0.1.11", ] [[package]] @@ -1136,7 +1137,7 @@ checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" dependencies = [ "smawk", "unicode-linebreak", - "unicode-width", + "unicode-width 0.1.11", ] [[package]] @@ -1208,6 +1209,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "version_check" version = "0.9.4" diff --git a/crates/modalkit-ratatui/Cargo.toml b/crates/modalkit-ratatui/Cargo.toml index 5e0ad26..0ac7111 100644 --- a/crates/modalkit-ratatui/Cargo.toml +++ b/crates/modalkit-ratatui/Cargo.toml @@ -19,6 +19,7 @@ modalkit = { workspace = true } ratatui = { version = "0.26" } regex = { workspace = true } serde = { version = "^1.0", features = ["derive"] } +unicode-width = "0.2.0" [dev-dependencies] rand = { workspace = true } diff --git a/crates/modalkit-ratatui/src/textbox.rs b/crates/modalkit-ratatui/src/textbox.rs index 8a72ccd..157285f 100644 --- a/crates/modalkit-ratatui/src/textbox.rs +++ b/crates/modalkit-ratatui/src/textbox.rs @@ -58,6 +58,8 @@ use modalkit::editing::{ use modalkit::errors::{EditError, EditResult, UIResult}; use modalkit::prelude::*; +use unicode_width::UnicodeWidthStr; + use super::{ScrollActions, TerminalCursor, WindowOps}; /// Line annotation shown in the left gutter. @@ -859,13 +861,18 @@ where } } - let _ = buf.set_stringn(x, y, s, width, self.style); - if cursor_line { - let coff = (cursor.x - start) as u16; + let coff = s[..s + .char_indices() + .map(|(i, _)| i) + .nth(cursor.x.saturating_sub(start)) + .unwrap_or(s.len())].width_cjk() as u16; + state.term_cursor = (x + coff, y); } + let _ = buf.set_stringn(x, y, s, width, self.style); + self._highlight_followers(line, start, end, (x, y), &finfo, buf); self._highlight_line(line, start, end, (x, y), &hinfo, buf); @@ -1002,13 +1009,19 @@ where let s = s.to_string(); let w = (right - x) as usize; - let (xres, _) = buf.set_stringn(x, y, s, w, self.style); if cursor_line { - let coff = cursor.x.saturating_sub(start) as u16; + let coff = s[..s + .char_indices() + .map(|(i, _)| i) + .nth(cursor.x.saturating_sub(start)) + .unwrap_or(s.len())].width_cjk() as u16; + state.term_cursor = (x + coff, y); } + let (xres, _) = buf.set_stringn(x, y, s, w, self.style); + self._highlight_followers(line, start, end, (x, y), &finfo, buf); self._highlight_line(line, start, end, (x, y), &hinfo, buf);