diff options
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/display/components/table.rs | 34 |
3 files changed, 40 insertions, 12 deletions
@@ -92,6 +92,7 @@ dependencies = [ "tokio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-resolver 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)", "tui 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -200,7 +201,7 @@ dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -236,7 +237,7 @@ dependencies = [ "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -251,7 +252,7 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1483,7 +1484,7 @@ name = "textwrap" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1566,7 +1567,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1602,7 +1603,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "unicode-width" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1927,7 +1928,7 @@ dependencies = [ "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "09c8070a9942f5e7cfccd93f490fdebd230ee3c3c9f107cb25bad5351ef671cf" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" -"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" +"checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" "checksum unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" @@ -32,6 +32,7 @@ lazy_static = "1.4.0" tokio = { version = "0.2", features = ["rt-core", "sync"] } trust-dns-resolver = "0.18.1" async-trait = "0.1.21" +unicode-width = "0.1.8" [target.'cfg(target_os="linux")'.dependencies] procfs = "0.7.4" @@ -41,4 +42,4 @@ insta = "0.12.0" pnet_base = "0.26.0" cargo-insta = "0.11.0" packet-builder = "0.5.0" -regex = "1" +regex = "1"
\ No newline at end of file diff --git a/src/display/components/table.rs b/src/display/components/table.rs index 059b5ed..f61886c 100644 --- a/src/display/components/table.rs +++ b/src/display/components/table.rs @@ -1,4 +1,6 @@ use ::std::collections::{BTreeMap, HashMap}; +use ::std::iter::FromIterator; +use ::unicode_width::UnicodeWidthChar; use ::tui::backend::Backend; use ::tui::layout::Rect; @@ -51,11 +53,35 @@ pub struct Table<'a> { breakpoints: BTreeMap<u16, ColumnData>, } +fn truncate_iter_to_unicode_width<Input, Collect>(iter: Input, width: usize) -> Collect +where + Input: Iterator<Item = char>, + Collect: FromIterator<char>, +{ + let mut chunk_width = 0; + iter.take_while(|ch| { + chunk_width += ch.width().unwrap_or(0); + chunk_width <= width + }) + .collect() +} + fn truncate_middle(row: &str, max_length: u16) -> String { - if row.len() as u16 > max_length { - let first_slice = &row[0..(max_length as usize / 2) - 2]; - let second_slice = &row[(row.len() - (max_length / 2) as usize + 2)..row.len()]; - format!("{}[..]{}", first_slice, second_slice) + if max_length < 6 { + truncate_iter_to_unicode_width(row.chars(), max_length as usize) + } else if row.len() as u16 > max_length { + let split_point = (max_length as usize / 2) - 2; + let first_slice = truncate_iter_to_unicode_width::<_, String>(row.chars(), split_point); + let second_slice = + truncate_iter_to_unicode_width::<_, Vec<_>>(row.chars().rev(), split_point) + .into_iter() + .rev() + .collect::<String>(); + if max_length % 2 == 0 { + format!("{}[..]{}", first_slice, second_slice) + } else { + format!("{}[..]{}", first_slice, second_slice) + } } else { row.to_string() } |