diff --git a/Cargo.lock b/Cargo.lock index 7788378..8756501 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,9 +52,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.95" +version = "0.2.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" [[package]] name = "main_error" @@ -92,6 +92,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "question" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbb3ede7a8f9a8ab89e714637f2cf40001b58f21340d4242b2f11533e65fa8d" + [[package]] name = "quiren" version = "0.2.0" @@ -100,6 +106,7 @@ dependencies = [ "main_error", "osstrtools", "pico-args", + "question", "thiserror", ] @@ -114,9 +121,9 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", "rand_chacha", @@ -126,9 +133,9 @@ dependencies = [ [[package]] name = "rand_chacha" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", @@ -136,27 +143,27 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ "getrandom", ] [[package]] name = "rand_hc" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ "rand_core", ] [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" dependencies = [ "bitflags", ] diff --git a/Cargo.toml b/Cargo.toml index 42d9dfd..6e79587 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,5 @@ osstrtools = "0.2" thiserror = "1" main_error = "0.1.1" pico-args = "0.4.2" +question = "0.2.2" + diff --git a/README.md b/README.md index aa38603..2e4b441 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Options: -h, --help Prints help information -r, --retry Re-enters the editor after an error -d, --delete Delete files removed in the editor + -n, --dry-run Show changes and ask for confirmation ``` Examples: diff --git a/src/main.rs b/src/main.rs index edffa29..6929b18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use osstrtools::OsStrConcat; +use question::{Answer, Question}; use std::env; use std::ffi::{OsStr, OsString}; use std::fs; @@ -12,10 +13,12 @@ Options: -h, --help Prints help information -r, --retry Re-enters the editor after an error -d, --delete Delete files removed in the editor + -n, --dry-run Show changes and ask for confirmation "; struct Args { delete: bool, + dryrun: bool, } fn main() -> Result<(), main_error::MainError> { @@ -24,6 +27,7 @@ fn main() -> Result<(), main_error::MainError> { let help = pargs.contains(["-h", "--help"]); let retry = pargs.contains(["-r", "--retry"]); let delete = pargs.contains(["-d", "--delete"]); + let dryrun = pargs.contains(["-n", "--dry-run"]); let dir: PathBuf = pargs .free_from_str() @@ -39,7 +43,7 @@ fn main() -> Result<(), main_error::MainError> { use std::io::Read; let mut stdin = std::io::stdin(); - while let Err(err) = quiren(&dir, Args { delete }) { + while let Err(err) = quiren(&dir, Args { delete, dryrun }) { eprintln!("Error: {}", err); eprintln!("Press enter to retry"); @@ -48,7 +52,7 @@ fn main() -> Result<(), main_error::MainError> { return Ok(()); } - Ok(quiren(&dir, Args { delete })?) + Ok(quiren(&dir, Args { delete, dryrun })?) } #[derive(Error, Debug)] @@ -76,7 +80,7 @@ fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> { let text = text.to_string_lossy().into_owned(); - let edited = edit::edit(&text)?; + let mut edited = edit::edit(&text)?; // check if linecount = entry count let new_count = edited.lines().count(); @@ -126,6 +130,37 @@ fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> { return Ok(()); } + if args.dryrun { + loop { + for (i, line) in edited + .lines() + .enumerate() + // Only rename files with modified names + .filter(|(i, line)| { + let name = OsStr::new(line); + name != entries[*i].file_name() + }) + { + let mut new_path = dir.to_owned(); + new_path.push(line); + println!( + "{} -> {}", + &entries[i].path().to_str().unwrap(), + new_path.to_str().unwrap(), + ); + } + let answer = Question::new("Confirm ?") + .default(Answer::YES) + .show_defaults() + .confirm(); + if answer == Answer::NO { + edited = edit::edit(&edited)?; + } else { + break; + } + } + } + for (i, line) in edited .lines() .enumerate()