implement -n, -v and -e commandline options
This commit is contained in:
parent
11ce644fca
commit
83ed7ee559
4 changed files with 179 additions and 20 deletions
|
|
@ -5,13 +5,13 @@ use std::io::{ErrorKind, Write, BufRead, BufReader};
|
|||
use std::collections::HashMap;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
|
||||
|
||||
const POSTFIX_TMP_FILE: &str = ".mvwrap";
|
||||
const LOCK_FILE: &str = ".mvwrap";
|
||||
|
||||
pub struct DirList {
|
||||
safe_source: bool,
|
||||
pub entries: Vec<String>,
|
||||
noop: bool,
|
||||
verbose: bool,
|
||||
entries: Vec<String>,
|
||||
}
|
||||
|
||||
impl DirList {
|
||||
|
|
@ -27,7 +27,7 @@ impl DirList {
|
|||
let mut path_list : Vec<String> = vec![];
|
||||
for path in paths {
|
||||
let path_string = path.display().to_string();
|
||||
if path_string != format!("./{}", POSTFIX_TMP_FILE) {
|
||||
if path_string != format!("./{}", LOCK_FILE) {
|
||||
path_list.push(path_string[2..].to_string());
|
||||
}
|
||||
}
|
||||
|
|
@ -35,6 +35,8 @@ impl DirList {
|
|||
|
||||
Self {
|
||||
safe_source: true,
|
||||
noop: false,
|
||||
verbose: false,
|
||||
entries: path_list,
|
||||
}
|
||||
}
|
||||
|
|
@ -48,6 +50,8 @@ impl DirList {
|
|||
|
||||
Self {
|
||||
safe_source: true,
|
||||
noop: false,
|
||||
verbose: false,
|
||||
entries: lines,
|
||||
}
|
||||
}
|
||||
|
|
@ -55,10 +59,20 @@ impl DirList {
|
|||
pub fn from_list(list: &Vec<String>) -> Self {
|
||||
Self {
|
||||
safe_source: false,
|
||||
noop: false,
|
||||
verbose: false,
|
||||
entries: list.to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_noop(&mut self) {
|
||||
self.noop = true;
|
||||
}
|
||||
|
||||
pub fn set_verbose(&mut self) {
|
||||
self.verbose = true;
|
||||
}
|
||||
|
||||
pub fn to_file(&self, target_file: &String) {
|
||||
let mut file = File::create(target_file).expect("no such file");
|
||||
|
||||
|
|
@ -83,17 +97,17 @@ impl DirList {
|
|||
if self.entries.iter().any(|j| j==&target_list.entries[i]) {
|
||||
let unique = Self::get_unique_entry(&target_list);
|
||||
intermediate_files.insert(unique.clone(), target_list.entries[i].clone());
|
||||
fs::rename(&self.entries[i], &unique).expect("failed to rename file");
|
||||
println!("Moving {} -> {}", self.entries[i], unique);
|
||||
if ! self.noop { fs::rename(&self.entries[i], &unique).expect("failed to rename file"); }
|
||||
if self.verbose { println!("Moving {} -> {}", self.entries[i], unique); }
|
||||
} else {
|
||||
fs::rename(&self.entries[i], &target_list.entries[i]).expect("failed to rename file");
|
||||
println!("Moving {} -> {}", self.entries[i], target_list.entries[i]);
|
||||
if ! self.noop { fs::rename(&self.entries[i], &target_list.entries[i]).expect("failed to rename file"); }
|
||||
if self.verbose { println!("Moving {} -> {}", self.entries[i], target_list.entries[i]); }
|
||||
}
|
||||
}
|
||||
}
|
||||
for (src, dst) in intermediate_files.iter() {
|
||||
fs::rename(src, dst).expect("failed to rename file");
|
||||
println!("Moving {} -> {}", src, dst);
|
||||
if ! self.noop { fs::rename(src, dst).expect("failed to rename file"); }
|
||||
if self.verbose { println!("Moving {} -> {}", src, dst); }
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
42
src/main.rs
42
src/main.rs
|
|
@ -1,5 +1,4 @@
|
|||
mod dirlist;
|
||||
|
||||
pub use crate::dirlist::*;
|
||||
|
||||
use std::env;
|
||||
|
|
@ -10,8 +9,11 @@ use std::process;
|
|||
use std::process::Command;
|
||||
use tempfile::NamedTempFile;
|
||||
use dialoguer::Confirm;
|
||||
use clap::Parser;
|
||||
|
||||
const LOCK_FILE: &str = ".mvwrap";
|
||||
const SUFFIX_TMPFILE: &str = ".mvwrap";
|
||||
const DEFAULT_EDITOR: &str = "vi";
|
||||
|
||||
struct Cleanup;
|
||||
|
||||
|
|
@ -21,6 +23,24 @@ impl Drop for Cleanup {
|
|||
}
|
||||
}
|
||||
|
||||
/// Wrapper to use an editor for renaming files
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Cli {
|
||||
/// Only show actions, but don't move files
|
||||
#[arg(short, long)]
|
||||
noop: bool,
|
||||
|
||||
/// Show moves
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
|
||||
/// Override editor to use
|
||||
#[arg(short, long)]
|
||||
editor: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
fn lock_dir() {
|
||||
if Path::new(LOCK_FILE).is_file() {
|
||||
panic!("Lock file \"{}\" already exists!", LOCK_FILE);
|
||||
|
|
@ -35,7 +55,7 @@ fn unlock_dir() {
|
|||
}
|
||||
|
||||
fn create_temp_file() -> String {
|
||||
let tmpfile = NamedTempFile::with_suffix(".mvwrap").expect("Could not create tempfile");
|
||||
let tmpfile = NamedTempFile::with_suffix(SUFFIX_TMPFILE).expect("Could not create tempfile");
|
||||
|
||||
let filepath = tmpfile.path().display().to_string();
|
||||
|
||||
|
|
@ -50,11 +70,10 @@ fn remove_temp_file(tmpfile: &String) {
|
|||
}
|
||||
}
|
||||
|
||||
fn edit_temp_file(tmpfile: &String) {
|
||||
let editor = match env::var("EDITOR") {
|
||||
Ok(e) => e,
|
||||
Err(_) => "vi".to_string(),
|
||||
};
|
||||
fn edit_temp_file(tmpfile: &String, editor_cli: &Option<String>) {
|
||||
let editor = env::var("EDITOR").unwrap_or(DEFAULT_EDITOR.to_string());
|
||||
let editor = editor_cli.clone().unwrap_or(editor);
|
||||
|
||||
let _output = Command::new(editor)
|
||||
.arg(tmpfile)
|
||||
.status()
|
||||
|
|
@ -62,6 +81,8 @@ fn edit_temp_file(tmpfile: &String) {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let args = Cli::parse();
|
||||
|
||||
// Place lockfile to not have multiple mvw processes running in the same dir at the same time
|
||||
lock_dir();
|
||||
let _cleanup = Cleanup; // Cleanup LOCK_FILE on panic
|
||||
|
|
@ -73,7 +94,10 @@ fn main() {
|
|||
let temp_file = create_temp_file();
|
||||
source_list.to_file(&temp_file);
|
||||
|
||||
edit_temp_file(&temp_file);
|
||||
if args.noop { source_list.set_noop(); source_list.set_verbose(); }
|
||||
if args.verbose { source_list.set_verbose(); }
|
||||
|
||||
edit_temp_file(&temp_file, &args.editor);
|
||||
|
||||
let mut target_list = DirList::from_file(&temp_file);
|
||||
|
||||
|
|
@ -92,7 +116,7 @@ fn main() {
|
|||
process::exit(1);
|
||||
}
|
||||
|
||||
edit_temp_file(&temp_file);
|
||||
edit_temp_file(&temp_file, &args.editor);
|
||||
target_list = DirList::from_file(&temp_file);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue