rm function now working
This commit is contained in:
46
src/main.rs
46
src/main.rs
@@ -31,6 +31,7 @@ fn main() {
|
|||||||
&_ => add_files(&arg, &mut args, &mut current_shelf),
|
&_ => add_files(&arg, &mut args, &mut current_shelf),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Should be replaced by help function
|
||||||
println!("no parameters")
|
println!("no parameters")
|
||||||
}
|
}
|
||||||
let _ = save_library(library);
|
let _ = save_library(library);
|
||||||
@@ -45,7 +46,7 @@ fn list_library(library: &mut Library) {
|
|||||||
println!("Current Shelf: {}", ¤t_shelf.name);
|
println!("Current Shelf: {}", ¤t_shelf.name);
|
||||||
println!("Content: ");
|
println!("Content: ");
|
||||||
for path in ¤t_shelf.content {
|
for path in ¤t_shelf.content {
|
||||||
println!("{}", path)
|
println!(" - {}", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +54,16 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
|||||||
// Callable function thing
|
// Callable function thing
|
||||||
let mut add_file = |file: &str| {
|
let mut add_file = |file: &str| {
|
||||||
let file_path = Path::new(&file);
|
let file_path = Path::new(&file);
|
||||||
let absolute_path = Path::canonicalize(&file_path).expect("something wrong");
|
let absolute_path = Path::canonicalize(&file_path).expect("Path cannot be Cannonicalized");
|
||||||
|
|
||||||
|
match utils::match_file_name(&file, shelf) {
|
||||||
|
Ok(_) => {
|
||||||
|
// Prevent existing files from being added to shelf
|
||||||
|
println!("File {} is already in current shelf", file);
|
||||||
|
process::exit(0)
|
||||||
|
}
|
||||||
|
Err(_) => {} // Continue with function
|
||||||
|
}
|
||||||
|
|
||||||
let _ = shelf
|
let _ = shelf
|
||||||
.content
|
.content
|
||||||
@@ -63,6 +73,7 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If add wasn't specified add the file
|
||||||
if current_arg != "add" {
|
if current_arg != "add" {
|
||||||
add_file(¤t_arg)
|
add_file(¤t_arg)
|
||||||
}
|
}
|
||||||
@@ -74,14 +85,39 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
||||||
|
if shelf.content.len() < 1 {
|
||||||
|
println!(
|
||||||
|
"Shelf \"{}\" is Empty. Are you using the right shelf?",
|
||||||
|
shelf.name
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if args.len() > 0 {
|
if args.len() > 0 {
|
||||||
for file in args {
|
for file in args {
|
||||||
match file.parse::<usize>() {
|
// If the argument is a number instead of a file name
|
||||||
|
let index: usize = match file.parse::<usize>() {
|
||||||
Ok(index) => {
|
Ok(index) => {
|
||||||
println!("path is: {}", &shelf.content[*&index])
|
println!("path is: {}", &shelf.content[*&index]);
|
||||||
|
index
|
||||||
}
|
}
|
||||||
Err(_) => {}
|
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]);
|
||||||
|
println!(
|
||||||
|
"Removed file {} from shelf {}",
|
||||||
|
file_path.canonicalize().unwrap().to_str().unwrap(),
|
||||||
|
shelf.name
|
||||||
|
);
|
||||||
|
shelf.content.remove(index);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If there's no file specified, clear the whole thing
|
// If there's no file specified, clear the whole thing
|
||||||
|
|||||||
37
src/utils.rs
37
src/utils.rs
@@ -1,5 +1,5 @@
|
|||||||
use crate::structs::{Library, Shelf};
|
use crate::structs::{Library, Shelf};
|
||||||
use std::{env, fs, io, io::Read};
|
use std::{env, fs, io, io::Read, path::Path};
|
||||||
use xdg::BaseDirectories;
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
|
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
|
||||||
@@ -51,6 +51,8 @@ pub fn get_current_shelf(library: &mut Library) -> &mut Shelf {
|
|||||||
|
|
||||||
// Get current shelf index and push the file
|
// Get current shelf index and push the file
|
||||||
let current_shelf = &mut library.shelves[*&shelf_index];
|
let current_shelf = &mut library.shelves[*&shelf_index];
|
||||||
|
current_shelf.content.sort();
|
||||||
|
current_shelf.content.dedup();
|
||||||
return current_shelf;
|
return current_shelf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,3 +71,36 @@ pub fn initialize_library() -> Library {
|
|||||||
};
|
};
|
||||||
return default_library;
|
return default_library;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
|
||||||
|
// Turn given file into string
|
||||||
|
let match_path = Path::new(file_name);
|
||||||
|
|
||||||
|
// If the file path doesnt exist
|
||||||
|
if !match_path.exists() {
|
||||||
|
let file_ending = match_path.file_name().expect("Error getting file name");
|
||||||
|
// For every entry in current shelf
|
||||||
|
for (i, path) in shelf.content.iter().enumerate() {
|
||||||
|
// Turn the string back into a path
|
||||||
|
let shelf_path = Path::new(path).canonicalize()?;
|
||||||
|
|
||||||
|
if shelf_path.ends_with(file_ending) {
|
||||||
|
return Ok(i);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} // else if it does
|
||||||
|
|
||||||
|
let cannonical_path = match_path.canonicalize()?;
|
||||||
|
|
||||||
|
for (i, path) in shelf.content.iter().enumerate() {
|
||||||
|
let shelf_path = Path::new(path).canonicalize()?;
|
||||||
|
if cannonical_path == shelf_path {
|
||||||
|
return Ok(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(io::Error::new(
|
||||||
|
io::ErrorKind::NotFound,
|
||||||
|
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user