Skip to content

Commit

Permalink
size & date in trash screen (#935)
Browse files Browse the repository at this point in the history
Trash screen (which you obtain with `:open_trash` on compatible platforms) now has 2 optional columns: one for the size and one for the deletion date.

Those internals work as expected: `:toggle_date`, `:toggle_size`, `:sort_by_date`, `:sort_by_size`.
  • Loading branch information
Canop authored Aug 22, 2024
1 parent 206ae57 commit 4d1fd62
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 90 deletions.
7 changes: 7 additions & 0 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 @@ -36,6 +36,7 @@ custom_error = "1.6"
deser-hjson = "2.2.3"
directories = "4.0"
file-size = "1.0.3"
flex-grow = "0.1"
git2 = { version = "0.19", default-features = false } # waiting for a good pure-rust alternative
glob = "0.3"
id-arena = "2.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/app/cmd_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl CmdResult {
pub fn verb_not_found(text: &str) -> CmdResult {
CmdResult::DisplayError(format!("verb not found: {:?}", &text))
}
pub fn from_optional_state(
pub fn from_optional_browser_state(
os: Result<BrowserState, TreeBuildError>,
message: Option<&'static str>,
in_new_panel: bool,
Expand Down
6 changes: 3 additions & 3 deletions src/browser/browser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl BrowserState {
bs.displayed_tree_mut().try_select_path(&tree.selected_line().path);
}
}
CmdResult::from_optional_state(
CmdResult::from_optional_browser_state(
new_state,
message,
in_new_panel,
Expand Down Expand Up @@ -148,7 +148,7 @@ impl BrowserState {
}
}
let dam = Dam::unlimited();
Ok(CmdResult::from_optional_state(
Ok(CmdResult::from_optional_browser_state(
BrowserState::new(
target,
if keep_pattern {
Expand Down Expand Up @@ -181,7 +181,7 @@ impl BrowserState {
in_new_panel: bool,
) -> CmdResult {
match &self.displayed_tree().selected_line().path.parent() {
Some(path) => CmdResult::from_optional_state(
Some(path) => CmdResult::from_optional_browser_state(
BrowserState::new(
path.to_path_buf(),
self.displayed_tree().options.without_pattern(),
Expand Down
2 changes: 1 addition & 1 deletion src/filesystems/filesystems_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl PanelState for FilesystemState {
let dam = Dam::unlimited();
let mut tree_options = self.tree_options();
tree_options.show_root_fs = true;
CmdResult::from_optional_state(
CmdResult::from_optional_browser_state(
BrowserState::new(
self.no_opt_selected_path().to_path_buf(),
tree_options,
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ pub mod skin;
pub mod syntactic;
pub mod task_sync;
pub mod terminal;
pub mod trash;
pub mod tree;
pub mod tree_build;
pub mod verb;

#[cfg(unix)]
pub mod filesystems;


#[cfg(unix)]
pub mod net;

#[cfg(feature = "trash")]
pub mod trash;

37 changes: 35 additions & 2 deletions src/trash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@

#[cfg(feature = "trash")]
mod trash_sort;
mod trash_state;
mod trash_state_cols;

#[cfg(feature = "trash")]
pub use trash_state::*;

use {
trash::{
self as trash_crate,
TrashItem,
TrashItemSize,
},
};

/// Determine whether an item in the trash is a directory.
///
/// There's probably a simpler solution in the trash crate, but I didn't found it.
fn item_is_dir(item: &TrashItem) -> bool {
match trash_crate::os_limited::metadata(item) {
Ok(metadata) => {
match metadata.size {
TrashItemSize::Bytes(_) => false,
TrashItemSize::Entries(_) => true,
}
}
Err(_) => false,
}
}

/// Return either the byte size or the number of entries of a trash item.
///
/// Return None when it couldn't be determined.
fn item_unified_size(item: &TrashItem) -> Option<u64> {
match trash_crate::os_limited::metadata(item).ok()?.size {
TrashItemSize::Bytes(v) => Some(v),
TrashItemSize::Entries(v) => v.try_into().ok(),
}
}
25 changes: 25 additions & 0 deletions src/trash/trash_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use {
super::*,
crate::{
tree::*,
},
trash::{
TrashItem,
},
};

/// Sort trash items according to the current tree options.
pub fn sort(
items: &mut [TrashItem],
tree_options: &TreeOptions,
) {
info!("sorting itemsi by {:?}", tree_options.sort);
match tree_options.sort {
Sort::Date => items.sort_by_key(|item| std::cmp::Reverse(item.time_deleted)),
Sort::Size => items.sort_by_key(|item| std::cmp::Reverse(
item_unified_size(item).unwrap_or(0)
)),
_ => items.sort_by_key(|item| (item.name.clone(), item.original_parent.clone())),
}

}
Loading

0 comments on commit 4d1fd62

Please sign in to comment.