Skip to content

Commit

Permalink
feat: display the total count of entries-to-be-deleted in the mark pane.
Browse files Browse the repository at this point in the history
This allows to better estimate how much work will be needed to perform
the deletion.

For example, when marking 3 items for deletion, previously one would see
`3 items marked`, but now one will see all items and sub-items, like
`120k`items marked`, which reflects the work that will be done much more
precisely.
  • Loading branch information
Byron committed Dec 10, 2023
2 parents a9dd549 + 81eadf8 commit 98d5b5a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 1 addition & 8 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::interactive::widgets::COUNT;
use crate::interactive::{
path_of,
widgets::{entry_color, EntryMarkMap},
DisplayOptions, EntryDataBundle, SortMode,
};
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 @@ -24,12 +23,6 @@ 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
17 changes: 12 additions & 5 deletions src/interactive/widgets/mark.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::interactive::widgets::COUNT;
use crate::interactive::{
fit_string_graphemes_with_ellipsis, path_of, widgets::entry_color, CursorDirection,
};
Expand Down Expand Up @@ -41,6 +42,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 +53,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 +92,7 @@ impl MarkPane {
index: sorting_index,
num_errors_during_deletion: 0,
is_dir,
entry_count: e.entry_count,
});
}
}
Expand All @@ -101,7 +105,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 +246,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 +434,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 +453,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 +481,7 @@ mod mark_pane_tests {
size: 10,
path: PathBuf::from("root"),
is_dir: true,
entry_count: Some(2),
..Default::default()
},
);
Expand All @@ -495,6 +502,6 @@ mod mark_pane_tests {
},
);

assert_eq!(calculate_size(&marked), 15u128);
assert_eq!(calculate_size_and_count(&marked), (15u128, 3u64));
}
}
7 changes: 7 additions & 0 deletions src/interactive/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ pub use header::*;
pub use help::*;
pub use main::*;
pub use mark::*;
use once_cell::sync::Lazy;

use tui::style::Color;

pub const COLOR_MARKED: Color = Color::Yellow;
pub const COLOR_MARKED_DARK: Color = Color::Rgb(176, 126, 0);

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

fn entry_color(fg: Option<Color>, is_file: bool, is_marked: bool) -> Option<Color> {
match (is_file, is_marked) {
(true, false) => fg,
Expand Down

0 comments on commit 98d5b5a

Please sign in to comment.