Skip to content

Commit

Permalink
fix it
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Jan 30, 2025
1 parent f0e3d96 commit 1b0eb26
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/utils/formatting/content_split/normal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

/// returns printed length of string
/// if ansi feature enabled, takes into account escape codes
Expand All @@ -22,12 +23,12 @@ pub fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {
let mut current_width = 0;
let mut parts = String::new();

let mut char_iter = word.chars().peekable();
let mut char_iter = word.graphemes(true).peekable();
// Check if the string might be too long, one character at a time.
// Peek into the next char and check the exit condition.
// That is, pushing the next character would result in the string being too long.
while let Some(c) = char_iter.peek() {
if (current_width + c.width().unwrap_or(1)) > allowed_width {
if (current_width + c.width()) > allowed_width {
break;
}

Expand All @@ -36,12 +37,12 @@ pub fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {

// We default to 1 char, if the character length cannot be determined.
// The user has to live with this, if they decide to add control characters or some fancy
// stuff into their tables. This is considered undefined behavior and we try to handle this
// stuff into their tables. This is considered undefined behavior, and we try to handle this
// to the best of our capabilities.
let character_width = c.width().unwrap_or(1);
let character_width = c.width();

current_width += character_width;
parts.push(c);
parts.push_str(c);
}

// Collect the remaining characters.
Expand All @@ -62,16 +63,11 @@ mod tests {

let (word, remaining) = split_long_word(emoji.width(), &emoji);

println!("{word}");
assert_eq!(word, "\u{1F642}\u{200D}");
assert_eq!(word.len(), 7);
assert_eq!(word.chars().count(), 2);
assert_eq!(word, "\u{1F642}\u{200D}\u{2195}\u{FE0F}");
assert_eq!(word.len(), 13);
assert_eq!(word.chars().count(), 4);
assert_eq!(word.width(), 2);

println!("{remaining}");
assert_eq!(remaining, "\u{2195}\u{FE0F}");
assert_eq!(remaining.len(), 6);
assert_eq!(remaining.chars().count(), 2);
assert_eq!(remaining.width(), 2);
assert!(remaining.is_empty());
}
}

0 comments on commit 1b0eb26

Please sign in to comment.