Compare commits

...

2 Commits

Author SHA1 Message Date
175feb370a applied util function for consistency 2026-08-21 16:47:22 +01:00
70289215a7 made util function for finding files with index 2026-08-21 16:47:13 +01:00
2 changed files with 37 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ fn main() {
if let Some(arg) = args.next() { if let Some(arg) = args.next() {
match arg.as_str() { match arg.as_str() {
"add" => add_files(&arg, &mut args, &mut current_shelf), "add" => add_files(&arg, &mut args, &mut current_shelf),
"rm" => remove_files(&mut args, &mut current_shelf), "rm" => remove_files(&mut args, &mut current_shelf).unwrap(),
"ls" => list_library(&mut library), "ls" => list_library(&mut library),
"cp" => copy_files( "cp" => copy_files(
&mut args, &mut args,
@@ -85,33 +85,21 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
return; return;
} }
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) { fn remove_files(
args: &mut env::Args,
mut shelf: &mut Shelf,
) -> Result<(), Box<dyn std::error::Error>> {
if shelf.content.len() < 1 { if shelf.content.len() < 1 {
println!( println!(
"Shelf \"{}\" is Empty. Are you using the right shelf?", "Shelf \"{}\" is Empty. Are you using the right shelf?",
shelf.name shelf.name
); );
return; return Ok(());
} }
if args.len() > 0 { if args.len() > 0 {
for file in args { for file in args {
// If the argument is a number instead of a file name // If the argument is a number instead of a file name
let index: usize = match file.parse::<usize>() { let index = utils::match_file_name_or_index(&file, &mut shelf)?;
Ok(index) => {
println!("path is: {}", &shelf.content[*&index]);
index
}
Err(_) => match utils::match_file_name(&file, shelf) {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index as usize]);
index
}
Err(e) => {
eprintln!("{e}");
process::exit(1)
}
},
};
let file_path = Path::new(&shelf.content[index]); let file_path = Path::new(&shelf.content[index]);
println!( println!(
"Removed file {} from shelf {}", "Removed file {} from shelf {}",
@@ -120,9 +108,11 @@ fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
); );
shelf.content.remove(index); shelf.content.remove(index);
} }
return Ok(());
} else { } else {
// If there's no file specified, clear the whole thing // If there's no file specified, clear the whole thing
shelf.content.clear() shelf.content.clear();
return Ok(());
} }
} }
@@ -165,7 +155,11 @@ fn copy_files(args: &mut env::Args, binary: String, shelf: &mut Shelf) {
println!( println!(
"Copying file {} to {}", "Copying file {} to {}",
source.to_str().unwrap(), source.to_str().unwrap(),
destination.to_str().unwrap() destination
.canonicalize()
.expect("couldn't cannonicalize")
.to_str()
.unwrap()
); );
let _ = fs::copy(source, destination); let _ = fs::copy(source, destination);
return; return;
@@ -185,7 +179,7 @@ fn copy_files(args: &mut env::Args, binary: String, shelf: &mut Shelf) {
}; };
let mut get_old_file = |old_file: Option<String>| { let mut get_old_file = |old_file: Option<String>| {
let old_file_index = match utils::match_file_name(&old_file.unwrap(), shelf) { let old_file_index = match utils::match_file_name_or_index(&old_file.unwrap(), shelf) {
Ok(index) => index, Ok(index) => index,
Err(e) => { Err(e) => {
eprintln!("{e}"); eprintln!("{e}");

View File

@@ -3,6 +3,7 @@ use std::{
fs, fs,
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
process,
}; };
use xdg::BaseDirectories; use xdg::BaseDirectories;
@@ -118,3 +119,23 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name), format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
)) ))
} }
pub fn match_file_name_or_index(file: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
let index: usize = match file.parse::<usize>() {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index]);
index
}
Err(_) => match match_file_name(&file, shelf) {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index as usize]);
index
}
Err(e) => {
eprintln!("{e}");
process::exit(1)
}
},
};
Ok(index)
}