diff options
author | Canop <cano.petrole@gmail.com> | 2022-05-18 11:41:37 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2022-05-18 11:44:19 +0200 |
commit | 83be8c6d67df64a902717ddbd3d8ef78648aa992 (patch) | |
tree | f0f1b87552145bc93568be72e99bbfab1f420ac3 /src/display | |
parent | 70f4d09aab75c7610e34bde855f4cd28ce691954 (diff) |
Format counts of files with thousands separator
Fix #549
Diffstat (limited to 'src/display')
-rw-r--r-- | src/display/displayable_tree.rs | 7 | ||||
-rw-r--r-- | src/display/mod.rs | 1 | ||||
-rw-r--r-- | src/display/num_format.rs | 25 |
3 files changed, 30 insertions, 3 deletions
diff --git a/src/display/displayable_tree.rs b/src/display/displayable_tree.rs index f1a827c..9b6ad5e 100644 --- a/src/display/displayable_tree.rs +++ b/src/display/displayable_tree.rs @@ -4,8 +4,9 @@ use { Col, CropWriter, GitStatusDisplay, - SPACE_FILLING, BRANCH_FILLING, MatchedString, + num_format::format_count, + SPACE_FILLING, BRANCH_FILLING, }, crate::{ app::AppState, @@ -106,7 +107,7 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> { ) -> Result<usize, termimad::Error> { Ok(if let Some(s) = line.sum { cond_bg!(count_style, self, selected, self.skin.count); - let s = s.to_count(); + let s = format_count(s.to_count()); cw.queue_g_string(count_style, format!("{s:>count_len$}"))?; 1 } else { @@ -483,7 +484,7 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> { .skip(1) // we don't show the counts of the root .map(|l| l.sum.map_or(0, |s| s.to_count())) .max() - .map(|c| format!("{}", c).len()) + .map(|c| format_count(c).len()) .unwrap_or(0) } else { 0 diff --git a/src/display/mod.rs b/src/display/mod.rs index 0f8f3c1..702aa90 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -28,6 +28,7 @@ mod git_status_display; pub mod flags_display; pub mod status_line; mod matched_string; +mod num_format; mod screen; mod cell_size; diff --git a/src/display/num_format.rs b/src/display/num_format.rs new file mode 100644 index 0000000..d8f5bc5 --- /dev/null +++ b/src/display/num_format.rs @@ -0,0 +1,25 @@ + +/// Format a number with commas as thousands separators +pub fn format_count(count: usize) -> String { + let mut s = count.to_string(); + let l = s.len(); + for i in 1..l { + if i % 3 == 0 { + s.insert(l-i, ','); + } + } + s +} + +#[test] +fn test_format_count() { + assert_eq!(&format_count(1), "1"); + assert_eq!(&format_count(12), "12"); + assert_eq!(&format_count(123), "123"); + assert_eq!(&format_count(1234), "1,234"); + assert_eq!(&format_count(12345), "12,345"); + assert_eq!(&format_count(123456), "123,456"); + assert_eq!(&format_count(1234567), "1,234,567"); + assert_eq!(&format_count(12345678), "12,345,678"); + assert_eq!(&format_count(1234567890), "1,234,567,890"); +} |