Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multibyte line trucation #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils/formatting/content_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use unicode_width::UnicodeWidthStr;

use super::content_split::measure_text_width;
use super::content_split::split_line;
use super::content_split::split_long_word;

use crate::cell::Cell;
use crate::row::Row;
Expand Down Expand Up @@ -121,9 +122,11 @@ pub fn format_row(
let width: usize = info.content_width.into();
if width >= 6 {
// Truncate the line if '...' doesn't fit
if last_line.width() >= width - 3 {
if last_line.width() > width - 3 {
let surplus = (last_line.width() + 3) - width;
last_line.truncate(last_line.width() - surplus);
let split_result = split_long_word(last_line.width() - surplus, &last_line);
Comment on lines 126 to +127
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do a double check :D

last_line.width() - surplus is effectively last_line.width() - (last_line.width() + 3 - width), which can be simplified to width - 3 right?

Not sure why I did it that complicated before.

last_line.clear();
last_line.push_str(&split_result.0);
}
last_line.push_str("...");
}
Expand Down
22 changes: 22 additions & 0 deletions tests/all/utf_8_characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,25 @@ fn multi_character_utf8_word_splitting() {
println!("{expected}");
assert_eq!(expected, "\n".to_string() + &table.to_string());
}

#[test]
fn multi_character_utf8_line_truncation() {
let mut table = Table::new();
let mut row = Row::new();
let test_string = "😀😃😄😁";
let row = row.max_height(1).add_cell(Cell::new(test_string));
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["test"])
.add_row(row.clone())
.set_width(10); // column content (6) + padding + border

let expected = "
+--------+
| test |
+========+
| 😀... |
+--------+";
println!("{expected}");
assert_eq!(expected, "\n".to_string() + &table.to_string());
}