Skip to content

Commit 87d43b0

Browse files
committed
textbox.rs: Respect unicode width
Reference: ulyssa/iamb#293
1 parent 24f3ec1 commit 87d43b0

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

Cargo.lock

+11-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/modalkit-ratatui/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ modalkit = { workspace = true }
1919
ratatui = { version = "0.26" }
2020
regex = { workspace = true }
2121
serde = { version = "^1.0", features = ["derive"] }
22+
unicode-width = "0.2.0"
2223

2324
[dev-dependencies]
2425
rand = { workspace = true }

crates/modalkit-ratatui/src/textbox.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ use modalkit::editing::{
5858
use modalkit::errors::{EditError, EditResult, UIResult};
5959
use modalkit::prelude::*;
6060

61+
use unicode_width::UnicodeWidthStr;
62+
6163
use super::{ScrollActions, TerminalCursor, WindowOps};
6264

6365
/// Line annotation shown in the left gutter.
@@ -859,13 +861,18 @@ where
859861
}
860862
}
861863

862-
let _ = buf.set_stringn(x, y, s, width, self.style);
863-
864864
if cursor_line {
865-
let coff = (cursor.x - start) as u16;
865+
let coff = s[..s
866+
.char_indices()
867+
.map(|(i, _)| i)
868+
.nth(cursor.x.saturating_sub(start))
869+
.unwrap_or(s.len())].width_cjk() as u16;
870+
866871
state.term_cursor = (x + coff, y);
867872
}
868873

874+
let _ = buf.set_stringn(x, y, s, width, self.style);
875+
869876
self._highlight_followers(line, start, end, (x, y), &finfo, buf);
870877
self._highlight_line(line, start, end, (x, y), &hinfo, buf);
871878

@@ -1002,13 +1009,19 @@ where
10021009

10031010
let s = s.to_string();
10041011
let w = (right - x) as usize;
1005-
let (xres, _) = buf.set_stringn(x, y, s, w, self.style);
10061012

10071013
if cursor_line {
1008-
let coff = cursor.x.saturating_sub(start) as u16;
1014+
let coff = s[..s
1015+
.char_indices()
1016+
.map(|(i, _)| i)
1017+
.nth(cursor.x.saturating_sub(start))
1018+
.unwrap_or(s.len())].width_cjk() as u16;
1019+
10091020
state.term_cursor = (x + coff, y);
10101021
}
10111022

1023+
let (xres, _) = buf.set_stringn(x, y, s, w, self.style);
1024+
10121025
self._highlight_followers(line, start, end, (x, y), &finfo, buf);
10131026
self._highlight_line(line, start, end, (x, y), &hinfo, buf);
10141027

0 commit comments

Comments
 (0)