Skip to content

Commit

Permalink
Count at least a width of 1 for special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
madchicken committed Aug 30, 2024
1 parent 300c07b commit 0b06f8c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion papergrid/src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ pub fn get_text_width(text: &str) -> usize {

/// Returns a char width.
pub fn get_char_width(c: char) -> usize {
unicode_width::UnicodeWidthChar::width(c).unwrap_or_default()
let c_width = if ['\n', '\t', '\r', '\0'].contains(&c) {
1
} else {
unicode_width::UnicodeWidthChar::width(c).unwrap_or_default()
}
}

/// Returns a string width (accouting all characters).
Expand Down
2 changes: 1 addition & 1 deletion tabled/src/settings/width/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn chunks(s: &str, width: usize, prefix: &str, suffix: &str) -> Vec<String> {
let _ = write!(&mut line, "{}", text_style.start());

while !text_slice.is_empty() {
let available_space = width - line_width;
let available_space = width.saturating_sub(line_width);

let part_width = get_string_width(text_slice);
if part_width <= available_space {
Expand Down
2 changes: 1 addition & 1 deletion tabled/src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub(crate) fn split_at_width(s: &str, at_width: usize) -> (usize, usize, usize)
break;
};

let c_width = get_char_width(c);
let c_width = unicode_width::UnicodeWidthChar::width(c).unwrap_or_default();
let c_length = c.len_utf8();

// We cut the chars which takes more then 1 symbol to display,
Expand Down

0 comments on commit 0b06f8c

Please sign in to comment.