Skip to content

Commit

Permalink
Add trash option
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinono3 committed Dec 6, 2021
1 parent 5b0c2cf commit 3119ed0
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 5 deletions.
208 changes: 208 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 @@ -15,3 +15,4 @@ main_error = "0.1.1"
pico-args = "0.4.2"
question = "0.2.2"
rand = { version = "0.8", features = ["small_rng", "getrandom"], default_features = false }
trash = "2.0.2"
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Options:
-h, --help Prints help information
-r, --retry Re-enters the editor after an error
-n, --dry-run Show changes and ask for confirmation
-t, --trash Trash files instead of deleting them
";

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -33,6 +34,7 @@ pub enum Mode {
pub struct Args {
mode: Mode,
dryrun: bool,
trash: bool,
}

fn main() -> Result<(), main_error::MainError> {
Expand All @@ -41,6 +43,7 @@ fn main() -> Result<(), main_error::MainError> {
let help = pargs.contains(["-h", "--help"]);
let retry = pargs.contains(["-r", "--retry"]);
let dryrun = pargs.contains(["-n", "--dry-run"]);
let trash = pargs.contains(["-t", "--trash"]);

let delete_mode = pargs.contains(["-d", "--delete-mode"]);

Expand All @@ -65,7 +68,7 @@ fn main() -> Result<(), main_error::MainError> {
use std::io::Read;
let mut stdin = std::io::stdin();

while let Err(err) = quiren(&dir, Args { mode, dryrun }) {
while let Err(err) = quiren(&dir, Args { mode, dryrun, trash }) {
eprintln!("Error: {}", err);
eprintln!("Press enter to retry");

Expand All @@ -74,7 +77,7 @@ fn main() -> Result<(), main_error::MainError> {
return Ok(());
}

Ok(quiren(&dir, Args { mode, dryrun })?)
Ok(quiren(&dir, Args { mode, dryrun, trash })?)
}

#[derive(Error, Debug)]
Expand All @@ -91,6 +94,8 @@ pub enum QuirenError {
Tempfile,
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
#[error("error when trashing: {0}")]
TrashError(#[from] trash::Error),
}

pub fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> {
Expand All @@ -116,7 +121,7 @@ pub fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> {

if args.dryrun {
loop {
if confirm_changes(&changes) {
if confirm_changes(&changes, args.trash) {
break;
}

Expand Down Expand Up @@ -168,6 +173,7 @@ pub fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> {

fs::rename(a, b)?
}
Change::Delete(a) if args.trash => trash::delete(a)?,
Change::Delete(a) => fs::remove_file(a)?,
}
}
Expand Down Expand Up @@ -251,14 +257,18 @@ fn extract_deletions<'a>(
Ok(iter)
}

fn confirm_changes(changes: &[Change]) -> bool {

fn confirm_changes(changes: &[Change], trash: bool) -> bool {
let delete_action = if trash { "Trash" } else { "Delete" };

for change in changes {
match change {
Change::Rename(a, b) => println!("Rename: {} -> {}", a.display(), b.display()),
Change::Delete(a) => println!("Delete: {}", a.display()),
Change::Delete(a) => println!("{}: {}", delete_action, a.display()),
}
}


if changes.is_empty() {
println!("No changes.");
return true;
Expand Down

0 comments on commit 3119ed0

Please sign in to comment.