Fix unicode truncation issues

This commit is contained in:
Dheepak Krishnamurthy 2021-05-06 09:58:07 -06:00
parent e20ec465c6
commit 671d1ece44
3 changed files with 19 additions and 11 deletions

10
Cargo.lock generated
View file

@ -1406,6 +1406,7 @@ dependencies = [
"task-hookrs", "task-hookrs",
"tui", "tui",
"unicode-segmentation", "unicode-segmentation",
"unicode-truncate",
"unicode-width", "unicode-width",
"uuid", "uuid",
] ]
@ -1457,6 +1458,15 @@ version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-truncate"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a04be5ca5f7a4a7270ffea82bc41c59b87c611ed04f20e77c338e8d3c2348e42"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.8" version = "0.1.8"

View file

@ -42,6 +42,7 @@ task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs" }
tui = { version = "0.12", optional = true, default-features = false } tui = { version = "0.12", optional = true, default-features = false }
unicode-segmentation = "1.6" unicode-segmentation = "1.6"
unicode-width = "0.1" unicode-width = "0.1"
unicode-truncate = "0.2"
uuid = { version = "0.8.1", features = ["serde", "v4"] } uuid = { version = "0.8.1", features = ["serde", "v4"] }
[package.metadata.rpm] [package.metadata.rpm]

View file

@ -5,6 +5,7 @@ use std::error::Error;
use std::process::Command; use std::process::Command;
use task_hookrs::task::Task; use task_hookrs::task::Task;
use task_hookrs::uda::UDAValue; use task_hookrs::uda::UDAValue;
use unicode_truncate::UnicodeTruncateStr;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
pub fn vague_format_date_time(from_dt: NaiveDateTime, to_dt: NaiveDateTime) -> String { pub fn vague_format_date_time(from_dt: NaiveDateTime, to_dt: NaiveDateTime) -> String {
@ -277,27 +278,23 @@ impl TaskReportTable {
Some(a) => format!(" [{}]", a.len()), Some(a) => format!(" [{}]", a.len()),
None => format!(""), None => format!(""),
}; };
let mut d = task.description().to_string(); let d = task.description().to_string();
let mut available_width = self.description_width; let mut available_width = self.description_width;
if self.description_width >= c.len() { if self.description_width >= c.len() {
available_width = self.description_width - c.len(); available_width = self.description_width - c.len();
} }
while available_width < d.width() && !d.is_char_boundary(available_width) { let (d, _) = d.unicode_truncate(available_width);
available_width += 1; let mut d = d.to_string();
}
d.truncate(available_width);
if d != *task.description() { if d != *task.description() {
d = format!("{}", d); d = format!("{}", d);
} }
format!("{}{}", d, c) format!("{}{}", d, c)
} }
"description.truncated" => { "description.truncated" => {
let mut d = task.description().to_string(); let d = task.description().to_string();
let mut available_width = self.description_width; let available_width = self.description_width;
while available_width < d.len() && !d.is_char_boundary(available_width) { let (d, _) = d.unicode_truncate(available_width);
available_width += 1; let mut d = d.to_string();
}
d.truncate(available_width);
if d != *task.description() { if d != *task.description() {
d = format!("{}", d); d = format!("{}", d);
} }