diff --git a/src/main.rs b/src/main.rs index 98fe9f1..ca72f8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,10 @@ -use std::{env, fs, path::Path, process}; +use std::{ + env, fs, io, + path::{Path, PathBuf}, + process, +}; +use crate::structs::{Library, Shelf}; use crate::utils::save_library; mod structs; @@ -16,21 +21,23 @@ fn main() { } }; + let mut current_shelf = utils::get_current_shelf(&mut library); + if let Some(arg) = args.next() { match arg.as_str() { - "add" => add_file(&mut args, &mut library), + "add" => add_files(&arg, &mut args, &mut current_shelf), + "rm" => remove_files(&mut args, &mut current_shelf), "ls" => list_library(&mut library), - &_ => println!("no command"), + &_ => add_files(&arg, &mut args, &mut current_shelf), } } else { println!("no parameters") } - dbg!(&library); - save_library(library); + let _ = save_library(library); process::exit(0); } -fn list_library(library: &mut structs::Library) { +fn list_library(library: &mut Library) { let current_shelf_index = utils::get_shelf_index(&library.shelves, &library.current_shelf).unwrap_or(0); let current_shelf = &library.shelves[current_shelf_index]; @@ -38,27 +45,46 @@ fn list_library(library: &mut structs::Library) { println!("Current Shelf: {}", ¤t_shelf.name); println!("Content: "); for path in ¤t_shelf.content { - println!() + println!("{}", path) } } -fn add_file(args: &mut env::Args, library: &mut structs::Library) { - for file in args { - println!("Adding {}", &file); +fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) { + // Callable function thing + let mut add_file = |file: &str| { let file_path = Path::new(&file); - let absolute_path = Path::canonicalize(&file_path).expect("No Such File"); + let absolute_path = Path::canonicalize(&file_path).expect("something wrong"); - println!("Current path: {}", absolute_path.to_str().unwrap()); - - let shelves = &library.shelves; - let current_shelf = &library.current_shelf; - let shelf_index = utils::get_shelf_index(shelves, current_shelf).unwrap_or(0); - - let current_shelf = &mut library.shelves[*&shelf_index]; - current_shelf + let _ = shelf .content - .push(absolute_path.to_str().unwrap().to_string()) + .push(absolute_path.to_str().unwrap().to_string()); + + println!("Added file: {}", absolute_path.to_str().unwrap()); + return; + }; + + if current_arg != "add" { + add_file(¤t_arg) + } + // For every remaining argument + for file in args { + add_file(&file) } - // process::exit(0) return; } + +fn remove_files(args: &mut env::Args, shelf: &mut Shelf) { + if args.len() > 0 { + for file in args { + match file.parse::() { + Ok(index) => { + println!("path is: {}", &shelf.content[*&index]) + } + Err(_) => {} + } + } + } else { + // If there's no file specified, clear the whole thing + shelf.content.clear() + } +}