Skip to content

Commit b327b41

Browse files
committed
fix: πŸ› remove double-width characters correctly
Signed-off-by: εˆ˜ζ΄‹ <[email protected]> Signed-off-by: bestgopher <[email protected]>
1 parent f37cb6e commit b327b41

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

β€Žsrc/lib.rsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub use crate::term::{
8383
user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
8484
};
8585
pub use crate::utils::{
86-
colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with,
86+
char_width, colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with,
8787
set_colors_enabled, set_colors_enabled_stderr, style, truncate_str, Alignment, Attribute,
8888
Color, Emoji, Style, StyledObject,
8989
};

β€Žsrc/term.rsβ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,10 @@ impl Term {
336336
loop {
337337
match slf.read_key()? {
338338
Key::Backspace => {
339-
if prefix_len < chars.len() && chars.pop().is_some() {
340-
slf.clear_chars(1)?;
339+
if prefix_len < chars.len() {
340+
if let Some(ch) = chars.pop() {
341+
slf.clear_chars(crate::utils::char_width(ch))?;
342+
}
341343
}
342344
slf.flush()?;
343345
}

β€Žsrc/utils.rsβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ fn str_width(s: &str) -> usize {
719719
}
720720

721721
#[cfg(feature = "ansi-parsing")]
722-
fn char_width(c: char) -> usize {
722+
pub fn char_width(c: char) -> usize {
723723
#[cfg(feature = "unicode-width")]
724724
{
725725
use unicode_width::UnicodeWidthChar;
@@ -732,6 +732,11 @@ fn char_width(c: char) -> usize {
732732
}
733733
}
734734

735+
#[cfg(not(feature = "ansi-parsing"))]
736+
pub fn char_width(_c: char) -> usize {
737+
1
738+
}
739+
735740
/// Truncates a string to a certain number of characters.
736741
///
737742
/// This ensures that escape codes are not screwed up in the process.

0 commit comments

Comments
Β (0)