diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2023-12-09 18:58:54 +0100 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2023-12-09 18:58:54 +0100 |
commit | 3241022a730dab89f13cbefbefdb583fd6a00994 (patch) | |
tree | d4bad2e3d42e4b6725ecb7e18c9d58909015d012 /src | |
parent | 7b7bad5564d0e87eea4b4bd2d32066063a13b554 (diff) |
feat: Add total size to header bar and change to aggregate, human-readable item count.
This changes the display from `(2034 items)` to
`(2k items, 213 MB)`, providing an overview of the total amount
of storage used along with the total amount of files on a particular
hiearchy level.
Diffstat (limited to 'src')
-rw-r--r-- | src/interactive/widgets/entries.rs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/src/interactive/widgets/entries.rs b/src/interactive/widgets/entries.rs index 8e4132d..9357587 100644 --- a/src/interactive/widgets/entries.rs +++ b/src/interactive/widgets/entries.rs @@ -7,6 +7,7 @@ use chrono::DateTime; use dua::traverse::{EntryData, Tree, TreeIndex}; use human_format; use itertools::Itertools; +use once_cell::sync::Lazy; use std::time::SystemTime; use std::{borrow::Borrow, path::Path}; use tui::{ @@ -23,6 +24,12 @@ use tui_react::{ List, ListProps, }; +static COUNT: Lazy<human_format::Formatter> = Lazy::new(|| { + let mut formatter = human_format::Formatter::new(); + formatter.with_decimals(0).with_separator(""); + formatter +}); + pub struct EntriesProps<'a> { pub tree: &'a Tree, pub root: TreeIndex, @@ -67,11 +74,12 @@ impl Entries { }; let total: u128 = entries.iter().map(|b| b.data.size).sum(); - let item_count: u64 = entries + let (item_count, item_size): (u64, u128) = entries .iter() - .map(|f| f.data.entry_count.unwrap_or(1)) - .sum(); - let title = title(¤t_path(tree, *root), item_count); + .map(|f| (f.data.entry_count.unwrap_or(1), f.data.size)) + .reduce(|a, b| (a.0 + b.0, a.1 + b.1)) + .unwrap_or_default(); + let title = title(¤t_path(tree, *root), item_count, *display, item_size); let title_block = title_block(&title, *border_style); let entry_in_view = entry_in_view(*selected, entries); @@ -153,15 +161,16 @@ fn title_block(title: &str, border_style: Style) -> Block<'_> { .borders(Borders::ALL) } -fn title(current_path: &str, item_count: u64) -> String { +fn title(current_path: &str, item_count: u64, display: DisplayOptions, size: u128) -> String { format!( - " {} ({} item{}) ", + " {} ({} item{}, {}) ", current_path, - item_count, + COUNT.format(item_count as f64), match item_count { 1 => "", _ => "s", - } + }, + display.byte_format.display(size) ) } @@ -254,10 +263,7 @@ fn count_column(entry_count: Option<u64>, style: Style) -> Span<'static> { "{:>4}", match entry_count { Some(count) => { - human_format::Formatter::new() - .with_decimals(0) - .with_separator("") - .format(count as f64) + COUNT.format(count as f64) } None => "".to_string(), } |