diff --git a/Cargo.lock b/Cargo.lock index e0f3fa53..824684b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,6 +446,7 @@ dependencies = [ "shellwords", "strip-ansi-escapes", "thiserror", + "unicode-width", "walkdir", ] @@ -973,6 +974,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index dacbc1a7..25e5d1d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ serde = { version = "1.0.159", features = ["derive"] } serde_yaml = "0.9.21" dns_common_derive = { version = "0.2.1" } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } +unicode-width = "0.1.10" [lib] name = "navi" diff --git a/src/deser/mod.rs b/src/deser/mod.rs index c97f7f20..7b2662e8 100644 --- a/src/deser/mod.rs +++ b/src/deser/mod.rs @@ -1,4 +1,5 @@ use crate::prelude::*; +use unicode_width::UnicodeWidthStr; pub mod raycast; pub mod terminal; @@ -26,9 +27,18 @@ pub fn fix_newlines(txt: &str) -> String { } fn limit_str(text: &str, length: usize) -> String { - if text.len() > length { - format!("{}…", text.chars().take(length - 1).collect::()) + let len = UnicodeWidthStr::width(text); + if len <= length { + format!("{}{}", text, " ".repeat(length - len)) } else { - format!("{:width$}", text, width = length) + let mut new_length = length; + let mut actual_length = 9999; + let mut txt = text.to_owned(); + while actual_length > length { + txt = txt.chars().take(new_length - 1).collect::(); + actual_length = UnicodeWidthStr::width(txt.as_str()); + new_length -= 1; + } + format!("{}…", txt) } }