Skip to content

Commit

Permalink
Improve trash emptying UX
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Jun 10, 2024
1 parent 8b7ef8a commit 7ab9d5a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
31 changes: 30 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trasher"
version = "3.3.5"
version = "3.3.6"
authors = ["Clément Nerma <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down Expand Up @@ -30,3 +30,4 @@ mountpoints = "0.2.1"
indicatif = "0.17.8"
fs_extra = "1.3.0"
comfy-table = "7.1.0"
walkdir = "2.5.0"
44 changes: 32 additions & 12 deletions src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fs, io::stdin, path::PathBuf};

use anyhow::{Context, Result};
use indicatif::{ProgressBar, ProgressStyle};

use crate::{error, fuzzy::FuzzyFinderItem, info, success, warn};
use crate::{fuzzy::FuzzyFinderItem, info, success, warn};

use super::{args::*, bail, debug, fsutils::*, items::*};

Expand Down Expand Up @@ -330,23 +331,42 @@ pub fn empty(config: &Config) -> Result<()> {

info!("Emptying the trash...");

let mut failed = false;

for trash_dir in trash_dirs {
info!("Emptying trash directory: {}", trash_dir.display());

if let Err(err) = fs::remove_dir_all(&trash_dir) {
error!(
"Failed to empty the trash at path: {}\n> {err}\n",
trash_dir.display()
);
warn!("> Listing files and directories to delete...");

let items = list_deletable_fs_items(&trash_dir)?;

warn!("> Deleting all {} items...", items.len());

let pbr = ProgressBar::new(items.len().try_into().unwrap());

pbr.set_style(ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.expect("Invalid progress bar template")
.progress_chars("#>-"));

for (i, item) in items.iter().enumerate() {
let metadata = item
.symlink_metadata()
.with_context(|| format!("Failed to get metadata for item: {}", item.display()))?
.file_type();

failed = true;
if metadata.is_dir() {
fs::remove_dir(item)
.with_context(|| format!("Failed to remove directory: {}", item.display()))?;
} else {
fs::remove_file(item)
.with_context(|| format!("Failed to remove file: {}", item.display()))?;
}

if i % 25 == 0 || i + 1 == items.len() {
pbr.set_position((i + 1).try_into().unwrap());
}
}
}

if failed {
bail!("Failed to empty trash directories");
pbr.finish();
}

success!("Trash was successfully emptied.");
Expand Down
10 changes: 10 additions & 0 deletions src/fsutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use comfy_table::{presets::UTF8_FULL_CONDENSED, ContentArrangement, Table};
use fs_extra::dir::TransitProcessResult;
use indicatif::{ProgressBar, ProgressStyle};
use mountpoints::mountpaths;
use walkdir::WalkDir;

use crate::{debug, error, Config};

Expand Down Expand Up @@ -465,3 +466,12 @@ pub fn are_on_same_fs(a: &Path, b: &Path) -> Result<bool> {

Ok(a_fs_id == b_fs_id)
}

pub fn list_deletable_fs_items(path: &Path) -> Result<Vec<PathBuf>> {
WalkDir::new(path)
.contents_first(true)
.into_iter()
.map(|entry| entry.map(|entry| entry.into_path()))
.collect::<Result<Vec<PathBuf>, _>>()
.context("Failed to read directory entry")
}

0 comments on commit 7ab9d5a

Please sign in to comment.