From 9d24f51b1c2d052d43e6a2acdb0b287c17cc83cc Mon Sep 17 00:00:00 2001 From: mint Date: Wed, 14 Sep 2022 16:33:19 -0300 Subject: [PATCH] inv: list: Add method to catch invalid slot If you tried to type anything that isn't a valid slot, Vento would panic. This commit should fix that. --- src/inv.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/inv.rs b/src/inv.rs index 4ee14b1..93170ef 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -27,13 +27,17 @@ pub fn list(slot: &str) { // Lists files in inventory let ventodir: PathBuf = env_config(); let slotdir: PathBuf = [ventodir.to_path_buf(), Path::new(slot).to_path_buf()].iter().collect(); - println!("🗃️ {}", format!("Files in {} inventory:", match slot { - "active" => format!("{}", slot).bold(), - "inactive" | _ => format!("{}", slot).blue().bold() - }).green()); - for file in fs::read_dir(&slotdir).unwrap() { - println!("- {}", file.unwrap().path().file_name().unwrap().to_os_string().into_string().unwrap()); - }; + if slotdir.is_dir() { // Checks if inventory selected exists + println!("🗃️ {}", format!("Files in {} inventory:", match slot { + "active" => format!("{}", slot).bold(), + "inactive" | _ => format!("{}", slot).blue().bold() + }).green()); + for file in fs::read_dir(&slotdir).unwrap() { + println!("- {}", file.unwrap().path().file_name().unwrap().to_os_string().into_string().unwrap()); + }; + } else { + println!("❌ {}", format!("Vento was unable to read that slot. Valid slots are {} and {}.", format!("active").green(), format!("inactive").blue()).red()); + } } pub fn switch() { // Switches between inventory slots