diff --git a/src/inv.rs b/src/inv.rs index 7952ac8..909c63a 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -49,23 +49,41 @@ pub fn init() { create_slots(); } -pub fn list(slot: &str) { +pub fn list(slot: &str, dir: &str) { // Lists files in inventory - let slotdir: PathBuf = match slot { + let mut slotdir: PathBuf = match slot { "active" | "a" => common::env_config()[1].clone(), "inactive" | "i" => common::env_config()[2].clone(), _ => PathBuf::new(), }; + if dir != "" { + slotdir = [&slotdir, &Path::new(dir).to_path_buf()].iter().collect(); + } + + if dir.to_string().contains("..") { + println!("❌ {}", format!("Cannot access parent.").red()); + process::exit(1); + } + if slotdir.is_dir() { if fs::read_dir(&slotdir).unwrap().count() == 0 { println!( "🗃️ {}", format!( - "No files in {}.", + "No files in {}{}.", match slot { "active" => format!("{}", slot).bold(), "inactive" | _ => format!("{}", slot).blue().bold(), + }, + if dir != "" { + if cfg!(windows) { + format!("\\{}", dir.to_string()) + } else { + format!("/{}", dir.to_string()) + } + } else { + "".to_string() } ) .green() @@ -75,11 +93,20 @@ pub fn list(slot: &str) { println!( "🗃️ {}", format!( - "Files in {} inventory ({}):", + "Files in {}{} ({}):", match slot { "active" => format!("{}", slot).bold(), "inactive" | _ => format!("{}", slot).blue().bold(), }, + if dir != "" { + if cfg!(windows) { + format!("\\{}", dir.to_string()) + } else { + format!("/{}", dir.to_string()) + } + } else { + " inventory".to_string() + }, format!("{}", fs::read_dir(&slotdir).unwrap().count()) .white() .bold() @@ -115,9 +142,9 @@ pub fn list(slot: &str) { println!( "❌ {}", format!( - "Vento was unable to read that slot. Valid slots are {} and {}.", - format!("active").green(), - format!("inactive").blue() + "No such slot or directory. Valid slots are {} and {}.", + format!("active").green().bold(), + format!("inactive").blue().bold() ) .red() ); diff --git a/src/main.rs b/src/main.rs index babf729..fd75920 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,14 +33,15 @@ fn main() { match args[1].as_str() { "help" | "h" => help(), "init" | "i" => inv::init(), - "list" | "l" => { - if args.len() == 3 { - // If the user has provided a slot, it'll use it. Otherwise, it'll default to the active slot - inv::list(args[2].as_str()); - } else { - inv::list("active"); - }; - } + "list" | "l" => match args.len() { + 4 => inv::list(args[2].as_str(), args[3].as_str()), + 3 => match args[2].as_str() { + "active" | "a" | "inactive" | "i" => inv::list(args[2].as_str(), ""), + _ => inv::list("active", args[2].as_str()), + }, + 2 => inv::list("active", ""), + _ => println!("❌ {}", format!("Too many arguments.").red()), + }, "switch" | "s" => inv::switch(), "take" | "t" => { if args.len() == 3 { @@ -98,7 +99,7 @@ fn help() { format!("drop | d [destination]") .bold() .green(), - format!("list | l [slot]").bold().green(), + format!("list | l [slot] [directory]").bold().green(), format!("switch | s").bold().green(), format!("init | i").bold().green(), format!("help | h").bold().green()