Skip to content

Commit

Permalink
Merge pull request #8 from sagittarius-a/feature/dry-run
Browse files Browse the repository at this point in the history
Add: Add dry-run mode to ask for confirmation before applying changes
  • Loading branch information
Sinono3 authored Jul 21, 2021
2 parents f059ace + 5676cd9 commit 1bf8728
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
31 changes: 19 additions & 12 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ osstrtools = "0.2"
thiserror = "1"
main_error = "0.1.1"
pico-args = "0.4.2"
question = "0.2.2"

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
41 changes: 38 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use osstrtools::OsStrConcat;
use question::{Answer, Question};
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
Expand All @@ -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> {
Expand All @@ -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()
Expand All @@ -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");

Expand All @@ -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)]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1bf8728

Please sign in to comment.