cleaned up, made adding files the default operation, made rm remove all files, and started trying to remove specific files
This commit is contained in:
68
src/main.rs
68
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;
|
use crate::utils::save_library;
|
||||||
|
|
||||||
mod structs;
|
mod structs;
|
||||||
@@ -16,21 +21,23 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut current_shelf = utils::get_current_shelf(&mut library);
|
||||||
|
|
||||||
if let Some(arg) = args.next() {
|
if let Some(arg) = args.next() {
|
||||||
match arg.as_str() {
|
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),
|
"ls" => list_library(&mut library),
|
||||||
&_ => println!("no command"),
|
&_ => add_files(&arg, &mut args, &mut current_shelf),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("no parameters")
|
println!("no parameters")
|
||||||
}
|
}
|
||||||
dbg!(&library);
|
let _ = save_library(library);
|
||||||
save_library(library);
|
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_library(library: &mut structs::Library) {
|
fn list_library(library: &mut Library) {
|
||||||
let current_shelf_index =
|
let current_shelf_index =
|
||||||
utils::get_shelf_index(&library.shelves, &library.current_shelf).unwrap_or(0);
|
utils::get_shelf_index(&library.shelves, &library.current_shelf).unwrap_or(0);
|
||||||
let current_shelf = &library.shelves[current_shelf_index];
|
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!("Current Shelf: {}", ¤t_shelf.name);
|
||||||
println!("Content: ");
|
println!("Content: ");
|
||||||
for path in ¤t_shelf.content {
|
for path in ¤t_shelf.content {
|
||||||
println!()
|
println!("{}", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_file(args: &mut env::Args, library: &mut structs::Library) {
|
fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
||||||
for file in args {
|
// Callable function thing
|
||||||
println!("Adding {}", &file);
|
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("No Such File");
|
let absolute_path = Path::canonicalize(&file_path).expect("something wrong");
|
||||||
|
|
||||||
println!("Current path: {}", absolute_path.to_str().unwrap());
|
let _ = shelf
|
||||||
|
|
||||||
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
|
|
||||||
.content
|
.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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
||||||
|
if args.len() > 0 {
|
||||||
|
for file in args {
|
||||||
|
match file.parse::<usize>() {
|
||||||
|
Ok(index) => {
|
||||||
|
println!("path is: {}", &shelf.content[*&index])
|
||||||
|
}
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If there's no file specified, clear the whole thing
|
||||||
|
shelf.content.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user