Skip to content

Commit

Permalink
Merge branch 'total_item_count'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 9, 2023
2 parents 606d60f + 3241022 commit ba2efe4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 19 additions & 9 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -67,7 +74,12 @@ impl Entries {
};

let total: u128 = entries.iter().map(|b| b.data.size).sum();
let title = title(&current_path(tree, *root), entries.len());
let (item_count, item_size): (u64, u128) = entries
.iter()
.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);

Expand Down Expand Up @@ -149,15 +161,16 @@ fn title_block(title: &str, border_style: Style) -> Block<'_> {
.borders(Borders::ALL)
}

fn title(current_path: &str, item_count: usize) -> 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)
)
}

Expand Down Expand Up @@ -250,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(),
}
Expand Down

0 comments on commit ba2efe4

Please sign in to comment.