diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2022-10-12 16:25:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-12 16:25:38 -0400 |
commit | 2a740f48f79498e6a812670176c45c53e0383c05 (patch) | |
tree | f6c9c017477931ad5ff8a7624e21cc2dd7fade2b /src/utils | |
parent | 1e5f0ea2d9dafa49279151b565154d9acf3b59d7 (diff) |
refactor: tables V2 (#749)
* refactor: move to new data table implementation
* more work towards refactor
* move disk and temp over, fix longstanding bug with disk and temp if removing the last value and selected
* work towards porting over CPU
work towards porting over CPU
fix typo
partially port over cpu, fix some potentially inefficient concat_string calls
more work towards cpu widget migration
some refactoring
* sortable data
sortable data
more refactoring
some sort refactoring
more refactoringgggg
column refactoring
renaming and reorganizing
more refactoring regarding column logic
add sort arrows again
* move over sort menu
* port over process
port over process
precommit
temp
temp two, remember to squash
work
fix broken ltr calculation and CPU hiding
add back row styling
temp
fix a bunch of issues, get proc working
more fixes around click
fix frozen issues
* fix dd process killing
* revert some of the persistent config changes from #257
* fix colouring for trees
* fix missing entries in tree
* keep columns if there is no data
* add and remove tests
* Fix ellipsis
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/gen_util.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs index c91a6852..2af9d1b0 100644 --- a/src/utils/gen_util.rs +++ b/src/utils/gen_util.rs @@ -1,5 +1,9 @@ use std::cmp::Ordering; +use concat_string::concat_string; +use tui::text::Text; +use unicode_segmentation::UnicodeSegmentation; + pub const KILO_LIMIT: u64 = 1000; pub const MEGA_LIMIT: u64 = 1_000_000; pub const GIGA_LIMIT: u64 = 1_000_000_000; @@ -92,10 +96,24 @@ pub fn get_decimal_prefix(quantity: u64, unit: &str) -> (f64, String) { } } +/// Truncates text if it is too long, and adds an ellipsis at the end if needed. +pub fn truncate_text<'a, U: Into<usize>>(content: &str, width: U) -> Text<'a> { + let width = width.into(); + let graphemes: Vec<&str> = UnicodeSegmentation::graphemes(content, true).collect(); + + if graphemes.len() > width && width > 0 { + // Truncate with ellipsis + let first_n = graphemes[..(width - 1)].concat(); + Text::raw(concat_string!(first_n, "…")) + } else { + Text::raw(content.to_string()) + } +} + #[inline] -pub fn sort_partial_fn<T: std::cmp::PartialOrd>(is_reverse: bool) -> fn(T, T) -> Ordering { - if is_reverse { - partial_ordering_rev +pub fn sort_partial_fn<T: std::cmp::PartialOrd>(is_descending: bool) -> fn(T, T) -> Ordering { + if is_descending { + partial_ordering_desc } else { partial_ordering } @@ -113,7 +131,7 @@ pub fn partial_ordering<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering { /// This is simply a wrapper function around [`partial_ordering`] that reverses /// the result. #[inline] -pub fn partial_ordering_rev<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering { +pub fn partial_ordering_desc<T: std::cmp::PartialOrd>(a: T, b: T) -> Ordering { partial_ordering(a, b).reverse() } |