Compare commits
2 Commits
3a4e32471a
...
175feb370a
| Author | SHA1 | Date | |
|---|---|---|---|
| 175feb370a | |||
| 70289215a7 |
38
src/main.rs
38
src/main.rs
@@ -22,7 +22,7 @@ fn main() {
|
||||
if let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
"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),
|
||||
"cp" => copy_files(
|
||||
&mut args,
|
||||
@@ -85,33 +85,21 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
||||
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 {
|
||||
println!(
|
||||
"Shelf \"{}\" is Empty. Are you using the right shelf?",
|
||||
shelf.name
|
||||
);
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
if args.len() > 0 {
|
||||
for file in args {
|
||||
// If the argument is a number instead of a file name
|
||||
let index: usize = match file.parse::<usize>() {
|
||||
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 index = utils::match_file_name_or_index(&file, &mut shelf)?;
|
||||
let file_path = Path::new(&shelf.content[index]);
|
||||
println!(
|
||||
"Removed file {} from shelf {}",
|
||||
@@ -120,9 +108,11 @@ fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
||||
);
|
||||
shelf.content.remove(index);
|
||||
}
|
||||
return Ok(());
|
||||
} else {
|
||||
// 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!(
|
||||
"Copying file {} to {}",
|
||||
source.to_str().unwrap(),
|
||||
destination.to_str().unwrap()
|
||||
destination
|
||||
.canonicalize()
|
||||
.expect("couldn't cannonicalize")
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
let _ = fs::copy(source, destination);
|
||||
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 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,
|
||||
Err(e) => {
|
||||
eprintln!("{e}");
|
||||
|
||||
21
src/utils.rs
21
src/utils.rs
@@ -3,6 +3,7 @@ use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
process,
|
||||
};
|
||||
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),
|
||||
))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user