Skip to content

Commit

Permalink
textbox.rs: Respect unicode width
Browse files Browse the repository at this point in the history
Reference:
  ulyssa/iamb#293
  • Loading branch information
simnalamburt committed Sep 29, 2024
1 parent 24f3ec1 commit 87d43b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
15 changes: 11 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/modalkit-ratatui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
23 changes: 18 additions & 5 deletions crates/modalkit-ratatui/src/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 87d43b0

Please sign in to comment.