summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2023-12-09 18:58:54 +0100
committerSebastian Thiel <sebastian.thiel@icloud.com>2023-12-09 18:58:54 +0100
commit3241022a730dab89f13cbefbefdb583fd6a00994 (patch)
treed4bad2e3d42e4b6725ecb7e18c9d58909015d012
parent7b7bad5564d0e87eea4b4bd2d32066063a13b554 (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.
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml1
-rw-r--r--src/interactive/widgets/entries.rs30
3 files changed, 22 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e095d87..bea4dd7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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"
diff --git a/Cargo.toml b/Cargo.toml
index 8370b42..817b94c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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(&current_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(&current_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(),
}