Skip to content

Commit

Permalink
Merge pull request #3 from sagittarius-a/master
Browse files Browse the repository at this point in the history
[Feature] Allow file removing with a dedicated arg
  • Loading branch information
Sinono3 authored Jul 20, 2021
2 parents 1c03b38 + d3d5af1 commit c148cf8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Usage: quiren [options] [dir]
Options:
-h, --help Prints help information
-r, --retry Re-enters the editor after an error
-d, --delete Delete files removed in the editor
```

Examples:
Expand Down
48 changes: 39 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ Usage: quiren [options] [dir]
Options:
-h, --help Prints help information
-r, --retry Re-enters the editor after an error
-d, --delete Delete files removed in the editor
";

struct Args {
delete: bool,
}

fn main() -> Result<(), main_error::MainError> {
let mut pargs = pico_args::Arguments::from_env();

let help = pargs.contains(["-h", "--help"]) ;
let retry = pargs.contains(["-r", "--retry"]) ;
let help = pargs.contains(["-h", "--help"]);
let retry = pargs.contains(["-r", "--retry"]);
let delete = pargs.contains(["-d", "--delete"]);

let dir: PathBuf = pargs
.free_from_str()
Expand All @@ -33,7 +39,7 @@ fn main() -> Result<(), main_error::MainError> {
use std::io::Read;
let mut stdin = std::io::stdin();

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

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

Ok(quiren(&dir)?)
Ok(quiren(&dir, Args { delete })?)
}

#[derive(Error, Debug)]
Expand All @@ -57,16 +63,16 @@ enum QuirenError {
IoError(#[from] std::io::Error),
}

fn quiren(dir: &Path) -> Result<(), QuirenError> {
fn quiren(dir: &Path, args: Args) -> Result<(), QuirenError> {
let mut entries: Vec<_> = dir.read_dir()?.map(|e| e.unwrap()).collect();

entries.sort_by_key(|e| e.file_name());

let text: OsString = entries
let entries_name = entries
.iter()
.map(|e| e.file_name())
.collect::<Vec<OsString>>()
.concat("\n");
.collect::<Vec<OsString>>();
let text = entries_name.clone().concat("\n");

let text = text.to_string_lossy().into_owned();

Expand All @@ -75,7 +81,7 @@ fn quiren(dir: &Path) -> Result<(), QuirenError> {
// check if linecount = entry count
let new_count = edited.lines().count();

if new_count != entries.len() {
if new_count != entries.len() && !args.delete {
return Err(QuirenError::EntryCountMismatch(entries.len(), new_count));
}

Expand All @@ -96,6 +102,30 @@ fn quiren(dir: &Path) -> Result<(), QuirenError> {
}
}

// Delete files that have been deleted in the editor and return
// Managing deletion AND rename is too complex. Users must perform
// there operations separately
if args.delete {
let r: Vec<OsString> = edited
.lines()
.map(OsString::from)
.collect::<Vec<OsString>>();
if args.delete {
let to_delete: Vec<_> = entries_name
.iter()
.filter(|existed| !r.contains(existed))
.collect();

for d in to_delete {
let mut new_path = dir.to_owned();
new_path.push(d);
fs::remove_file(new_path).unwrap();
}
}

return Ok(());
}

for (i, line) in edited
.lines()
.enumerate()
Expand Down

0 comments on commit c148cf8

Please sign in to comment.