works decent enough for whole dirs now

This commit is contained in:
Ward Wouts 2024-11-20 13:08:28 +01:00
parent 3ba9097489
commit 414294ac68

View file

@ -41,28 +41,19 @@ fn read_current_dir() -> Vec<String> {
fn lock_dir() { fn lock_dir() {
if Path::new(LOCK_FILE).is_file() { if Path::new(LOCK_FILE).is_file() {
panic!("Lock file \"{}\" exists!", LOCK_FILE); panic!("Lock file \"{}\" already exists!", LOCK_FILE);
} }
let _file = match File::create(LOCK_FILE) { let _file = File::create(LOCK_FILE).expect("Could not create LOCK_FILE");
Ok(file) => file,
Err(_) => panic!("Could not create lock file: {}", LOCK_FILE),
};
} }
fn unlock_dir() { fn unlock_dir() {
if Path::new(LOCK_FILE).is_file() { if Path::new(LOCK_FILE).is_file() {
let _result = match fs::remove_file(LOCK_FILE){ let _result = fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE");
Ok(result) => result,
Err(_) => panic!("Could not remove lock file: {}", LOCK_FILE),
};
} }
} }
fn create_temp_file(paths: &Vec<String>) -> String { fn create_temp_file(paths: &Vec<String>) -> String {
let mut tmpfile = match NamedTempFile::new(){ let mut tmpfile = NamedTempFile::with_suffix(".mvwrap").expect("Could not create tempfile");
Ok(file) => file,
Err(e) => panic!("Could not create tempfile: {}", e),
};
for path in paths { for path in paths {
let _ = writeln!(tmpfile, "{}", path); let _ = writeln!(tmpfile, "{}", path);
@ -70,20 +61,14 @@ fn create_temp_file(paths: &Vec<String>) -> String {
let filepath = tmpfile.path().display().to_string(); let filepath = tmpfile.path().display().to_string();
let _ = match tmpfile.keep(){ tmpfile.keep().expect("Can't keep tempfile");
Ok(res) => res,
Err(e) => panic!("Can't keep tempfile: {}", e),
};
filepath filepath
} }
fn remove_temp_file(tmpfile: &String) { fn remove_temp_file(tmpfile: &String) {
if Path::new(tmpfile).is_file() { if Path::new(tmpfile).is_file() {
let _result = match fs::remove_file(tmpfile){ let _result = fs::remove_file(tmpfile).expect("Could not remove tempfile");
Ok(result) => result,
Err(_) => panic!("Could not remove tempfile: {}", tmpfile),
};
} }
} }
@ -159,13 +144,32 @@ fn run_checks(src_paths: &Vec<String>, dst_paths: &Vec<String>) -> bool {
// Make sure all destination files are unique // Make sure all destination files are unique
let dst_paths_length_unique = unique_length(&dst_paths); let dst_paths_length_unique = unique_length(&dst_paths);
if dst_paths.len() != dst_paths_length_unique { if dst_paths.len() != dst_paths_length_unique {
println!("Clashing destination names!"); println!("ERROR: You're trying to move multiple files to the same name.");
// TODO: show which ones! show_doubles(&src_paths, &dst_paths);
return true return true
} }
false false
} }
fn show_doubles(src_paths: &Vec<String>, dst_paths: &Vec<String>) {
let mut paths = HashMap::new();
let dst_len = dst_paths.len();
for i in 0..dst_len {
paths.entry(&dst_paths[i]).or_insert(Vec::new());
paths.get_mut(&dst_paths[i]).unwrap().push(i);
}
for (path, lines) in paths.iter() {
if lines.len() > 1 {
for i in lines.iter() {
println!("[line: {}] {} -> {}", i+1, src_paths[*i], path);
}
}
}
}
fn main() { fn main() {
// Place lockfile to not have multiple mvw processes running in the same dir at the same time // Place lockfile to not have multiple mvw processes running in the same dir at the same time
lock_dir(); lock_dir();
@ -183,9 +187,7 @@ fn main() {
let mut new_paths = read_temp_file(&temp_file); let mut new_paths = read_temp_file(&temp_file);
// Compare length, if not equal offer to try again (or panic for now)
while run_checks(&paths, &new_paths) { while run_checks(&paths, &new_paths) {
let confirmation = Confirm::new() let confirmation = Confirm::new()
.with_prompt("Continue editing?") .with_prompt("Continue editing?")
.interact() .interact()
@ -202,7 +204,6 @@ fn main() {
// (also don't overwrite existing files that are not in the input list!) // (also don't overwrite existing files that are not in the input list!)
move_safely(&paths, &new_paths); move_safely(&paths, &new_paths);
//display_temp_file(&temp_file); //display_temp_file(&temp_file);