Skip to content

Commit

Permalink
fix: πŸ› remove double-width characters correctly
Browse files Browse the repository at this point in the history
Signed-off-by: εˆ˜ζ΄‹ <[email protected]>
Signed-off-by: bestgopher <[email protected]>
  • Loading branch information
bestgopher committed Feb 26, 2025
1 parent 03965f9 commit a11f049
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ impl Term {
loop {
match slf.read_key()? {
Key::Backspace => {
if prefix_len < chars.len() && chars.pop().is_some() {
slf.clear_chars(1)?;
if prefix_len < chars.len() {
if let Some(ch) = chars.pop() {
slf.clear_chars(crate::utils::char_width(ch))?;
}
}
slf.flush()?;
}
Expand Down
7 changes: 6 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ fn str_width(s: &str) -> usize {
}

#[cfg(feature = "ansi-parsing")]
fn char_width(c: char) -> usize {
pub(crate) fn char_width(c: char) -> usize {
#[cfg(feature = "unicode-width")]
{
use unicode_width::UnicodeWidthChar;
Expand All @@ -732,6 +732,11 @@ fn char_width(c: char) -> usize {
}
}

#[cfg(not(feature = "ansi-parsing"))]
pub(crate) fn char_width(_c: char) -> usize {
1
}

/// Truncates a string to a certain number of characters.
///
/// This ensures that escape codes are not screwed up in the process.
Expand Down

0 comments on commit a11f049

Please sign in to comment.