Skip to content

Commit

Permalink
Calculates mark pane item count consistently with the rest of the app
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrwach committed Dec 10, 2023
1 parent a9dd549 commit 2c69ea1
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/interactive/widgets/mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use dua::{
traverse::{Tree, TreeIndex},
ByteFormat,
};
use human_format;
use itertools::Itertools;
use once_cell::sync::Lazy;
use std::{
borrow::Borrow,
collections::{btree_map::Entry, BTreeMap},
Expand All @@ -26,6 +28,12 @@ use tui_react::{
};
use unicode_segmentation::UnicodeSegmentation;

static COUNT: Lazy<human_format::Formatter> = Lazy::new(|| {
let mut formatter = human_format::Formatter::new();
formatter.with_decimals(0).with_separator("");
formatter
});

pub enum MarkMode {
Delete,
#[cfg(feature = "trash-move")]
Expand All @@ -41,6 +49,7 @@ pub struct EntryMark {
pub index: usize,
pub num_errors_during_deletion: usize,
pub is_dir: bool,
pub entry_count: Option<u64>,
}

#[derive(Default)]
Expand All @@ -51,6 +60,7 @@ pub struct MarkPane {
has_focus: bool,
last_sorting_index: usize,
total_size: u128,
item_count: u64,
}

pub struct MarkPaneProps {
Expand Down Expand Up @@ -89,6 +99,7 @@ impl MarkPane {
index: sorting_index,
num_errors_during_deletion: 0,
is_dir,
entry_count: e.entry_count,
});
}
}
Expand All @@ -101,7 +112,7 @@ impl MarkPane {
if self.marked.is_empty() {
None
} else {
self.total_size = calculate_size(&self.marked);
(self.total_size, self.item_count) = calculate_size_and_count(&self.marked);
Some(self)
}
}
Expand Down Expand Up @@ -242,7 +253,7 @@ impl MarkPane {
let marked: &_ = &self.marked;
let title = format!(
"Marked {} items ({}) ",
marked.len(),
COUNT.format(self.item_count as f64),
format.display(self.total_size)
);
let selected = self.selected;
Expand Down Expand Up @@ -430,14 +441,15 @@ impl MarkPane {
}
}

pub fn calculate_size(marked: &EntryMarkMap) -> u128 {
pub fn calculate_size_and_count(marked: &EntryMarkMap) -> (u128, u64) {
let entries: Vec<&EntryMark> = marked
.iter()
.map(|(_k, v)| v)
.sorted_by(|a, b| Ord::cmp(&a.path, &b.path))
.collect();

let mut size = 0u128;
let mut item_count = 0u64;
for (idx, entry) in entries.iter().enumerate() {
let mut is_subdirectory = false;
for other in &entries[0..idx] {
Expand All @@ -448,9 +460,10 @@ pub fn calculate_size(marked: &EntryMarkMap) -> u128 {
}
if !is_subdirectory {
size += entry.size;
item_count += entry.entry_count.unwrap_or(1);
}
}
size
(size, item_count)
}

#[cfg(test)]
Expand All @@ -475,6 +488,7 @@ mod mark_pane_tests {
size: 10,
path: PathBuf::from("root"),
is_dir: true,
entry_count: Some(2),
..Default::default()
},
);
Expand All @@ -495,6 +509,6 @@ mod mark_pane_tests {
},
);

assert_eq!(calculate_size(&marked), 15u128);
assert_eq!(calculate_size_and_count(&marked), (15u128, 3u64));
}
}

0 comments on commit 2c69ea1

Please sign in to comment.