1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-12-02 09:39:15 +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();
}
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()
);

View file

@ -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 <file | directory> [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()