diff options
-rw-r--r-- | Cargo.lock | 5 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/interactive/widgets/entries.rs | 30 |
3 files changed, 22 insertions, 14 deletions
@@ -281,6 +281,7 @@ dependencies = [ "itertools", "jwalk", "num_cpus", + "once_cell", "open", "owo-colors", "petgraph", @@ -513,9 +514,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "open" @@ -39,6 +39,7 @@ open = { version = "5.0", optional = true } wild = "2.0.4" owo-colors = "3.5.0" human_format = "1.0.3" +once_cell = "1.19" [[bin]] name="dua" 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(), } |