1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-12-03 18:37:13 +00:00

inv: Added directory listing

Now you can list the contents of directories with the list command!
This commit is contained in:
Lux Aliaga 2022-09-18 00:27:31 -03:00
parent 6c51a8da6e
commit d11a200d6f
Signed by: lux
GPG key ID: B56C805968637437
2 changed files with 44 additions and 16 deletions

View file

@ -49,23 +49,41 @@ pub fn init() {
create_slots(); create_slots();
} }
pub fn list(slot: &str) { pub fn list(slot: &str, dir: &str) {
// Lists files in inventory // Lists files in inventory
let slotdir: PathBuf = match slot { let mut slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()[1].clone(), "active" | "a" => common::env_config()[1].clone(),
"inactive" | "i" => common::env_config()[2].clone(), "inactive" | "i" => common::env_config()[2].clone(),
_ => PathBuf::new(), _ => 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 slotdir.is_dir() {
if fs::read_dir(&slotdir).unwrap().count() == 0 { if fs::read_dir(&slotdir).unwrap().count() == 0 {
println!( println!(
"🗃️ {}", "🗃️ {}",
format!( format!(
"No files in {}.", "No files in {}{}.",
match slot { match slot {
"active" => format!("{}", slot).bold(), "active" => format!("{}", slot).bold(),
"inactive" | _ => format!("{}", slot).blue().bold(), "inactive" | _ => format!("{}", slot).blue().bold(),
},
if dir != "" {
if cfg!(windows) {
format!("\\{}", dir.to_string())
} else {
format!("/{}", dir.to_string())
}
} else {
"".to_string()
} }
) )
.green() .green()
@ -75,11 +93,20 @@ pub fn list(slot: &str) {
println!( println!(
"🗃️ {}", "🗃️ {}",
format!( format!(
"Files in {} inventory ({}):", "Files in {}{} ({}):",
match slot { match slot {
"active" => format!("{}", slot).bold(), "active" => format!("{}", slot).bold(),
"inactive" | _ => format!("{}", slot).blue().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()) format!("{}", fs::read_dir(&slotdir).unwrap().count())
.white() .white()
.bold() .bold()
@ -115,9 +142,9 @@ pub fn list(slot: &str) {
println!( println!(
"❌ {}", "❌ {}",
format!( format!(
"Vento was unable to read that slot. Valid slots are {} and {}.", "No such slot or directory. Valid slots are {} and {}.",
format!("active").green(), format!("active").green().bold(),
format!("inactive").blue() format!("inactive").blue().bold()
) )
.red() .red()
); );

View file

@ -33,14 +33,15 @@ fn main() {
match args[1].as_str() { match args[1].as_str() {
"help" | "h" => help(), "help" | "h" => help(),
"init" | "i" => inv::init(), "init" | "i" => inv::init(),
"list" | "l" => { "list" | "l" => match args.len() {
if args.len() == 3 { 4 => inv::list(args[2].as_str(), args[3].as_str()),
// If the user has provided a slot, it'll use it. Otherwise, it'll default to the active slot 3 => match args[2].as_str() {
inv::list(args[2].as_str()); "active" | "a" | "inactive" | "i" => inv::list(args[2].as_str(), ""),
} else { _ => inv::list("active", args[2].as_str()),
inv::list("active"); },
}; 2 => inv::list("active", ""),
} _ => println!("{}", format!("Too many arguments.").red()),
},
"switch" | "s" => inv::switch(), "switch" | "s" => inv::switch(),
"take" | "t" => { "take" | "t" => {
if args.len() == 3 { if args.len() == 3 {
@ -98,7 +99,7 @@ fn help() {
format!("drop | d <file | directory> [destination]") format!("drop | d <file | directory> [destination]")
.bold() .bold()
.green(), .green(),
format!("list | l [slot]").bold().green(), format!("list | l [slot] [directory]").bold().green(),
format!("switch | s").bold().green(), format!("switch | s").bold().green(),
format!("init | i").bold().green(), format!("init | i").bold().green(),
format!("help | h").bold().green() format!("help | h").bold().green()