From 6ec32037db0aa8099b98dc4677ce397fdd25ea84 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Tue, 27 Sep 2022 20:05:08 -0300 Subject: [PATCH] src: Code comments and cleanup Basically some last refinements to make the code look a bit more readable and presentable. --- src/bin/drop.rs | 3 + src/bin/take.rs | 3 + src/bin/vento.rs | 4 +- src/common.rs | 1 + src/help.rs | 4 +- src/inv.rs | 174 ++++++++++++++++++++++++----------------------- src/item.rs | 20 ++++-- 7 files changed, 117 insertions(+), 92 deletions(-) diff --git a/src/bin/drop.rs b/src/bin/drop.rs index 8b1ed38..dd1c232 100644 --- a/src/bin/drop.rs +++ b/src/bin/drop.rs @@ -24,9 +24,11 @@ use std::path::Path; use vento::{help, item}; fn main() -> Result<()> { + // Handles args in Drop let args: Vec = env::args().collect(); if args.len() >= 2 { if args[1].contains("--slot=") { + // Checks if the user has provided the long argument "--slot=" match args.len() { 4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf())?, 3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() { @@ -60,6 +62,7 @@ fn main() -> Result<()> { } } } else { + // If the user provides no arguments, Drop will display the help message. help::drop()?; } Ok(()) diff --git a/src/bin/take.rs b/src/bin/take.rs index d226830..a38544c 100644 --- a/src/bin/take.rs +++ b/src/bin/take.rs @@ -23,9 +23,11 @@ use std::env; use vento::{help, item}; fn main() -> Result<()> { + // Handles args in Vento let args: Vec = env::args().collect(); if args.len() >= 2 { if args[1].contains("--slot=") { + // Checks if the user has provided the long argument "--slot=" match args.len() { 3 => item::take(&args[2], &args[1].replace("--slot=", ""))?, 2 => bail!("{}", "You need to specify a file".red()), @@ -47,6 +49,7 @@ fn main() -> Result<()> { } } } else { + // If the user provides no arguments, Take will display the help message. help::take()?; } Ok(()) diff --git a/src/bin/vento.rs b/src/bin/vento.rs index 40beeee..270f977 100644 --- a/src/bin/vento.rs +++ b/src/bin/vento.rs @@ -23,10 +23,12 @@ use std::env; use vento::{help, inv}; fn main() -> Result<()> { + // Handles args in Vento let args: Vec = env::args().collect(); if args.len() >= 2 { // If the vector for the arguments the command is taking is larger than 2, it most likely means the user has provided an argument if args[1].contains("--slot=") { + // Checks if the user has provided the long argument "--slot=" match args.len() { 3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?, 2 => inv::list(&args[1].replace("--slot=", ""), "")?, @@ -47,7 +49,7 @@ fn main() -> Result<()> { } } } else { - // If the user provides no commands, Vento will display the files in the active slot. + // If the user provides no arguments, Vento will display the files in the active slot. inv::list("active", "")?; } Ok(()) diff --git a/src/common.rs b/src/common.rs index 9cf8f46..f7eec75 100644 --- a/src/common.rs +++ b/src/common.rs @@ -49,6 +49,7 @@ pub fn env_config() -> Result> { } fn dir_config() -> Result { + // Handles reading the config file or variables for Vento. let mut result = String::new(); let mut config = match dirs::config_dir() { Option::Some(dir) => dir, diff --git a/src/help.rs b/src/help.rs index 83ce899..dedff27 100644 --- a/src/help.rs +++ b/src/help.rs @@ -44,7 +44,7 @@ pub fn vento() -> Result<()> { } pub fn take() -> Result<()> { - // A quick guide to move around in Vento + // A quick guide to move around in Take println!( "{}, a file grabber for Vento © 2022 Lux Aliaga. Licensed under GPLv3 @@ -63,7 +63,7 @@ pub fn take() -> Result<()> { } pub fn drop() -> Result<()> { - // A quick guide to move around in Vento + // A quick guide to move around in Drop println!( "{}, a file dropper for Vento © 2022 Lux Aliaga. Licensed under GPLv3 diff --git a/src/inv.rs b/src/inv.rs index 32eb86e..c935846 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -49,106 +49,34 @@ pub fn init() -> Result<()> { pub fn list(slot: &str, dir: &str) -> Result<()> { // Lists files in inventory - let mut slotdir: PathBuf = match slot { - "active" | "a" => common::env_config()?[1].clone(), - "inactive" | "i" => common::env_config()?[2].clone(), - _ => PathBuf::new(), - }; - let ventodir = &common::env_config()?[0]; if !ventodir.is_dir() { + // Detects if Vento hasn't been initialized and bails if so bail!( "{}", "Vento not initialized. Run \"vento -i\" to initialize Vento.".red() ); } + let mut slotdir: PathBuf = match slot { + "active" | "a" => common::env_config()?[1].clone(), + "inactive" | "i" => common::env_config()?[2].clone(), + _ => PathBuf::new(), + }; + if dir != "" { + // Detects if the directory argument is not empty, and if so appends the path provided to the slot directory variable slotdir = [&slotdir, &Path::new(dir).to_path_buf()].iter().collect(); } if dir.to_string().contains("..") { + // Basically preventing from listing anything out of bounds. ls and dir exist for that bail!("{}", "Cannot access parent.".red()); } - if slotdir.is_dir() { - if fs::read_dir(&slotdir).unwrap().count() == 0 { - println!( - "🗃️ {}", - format!( - "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() - ); - } else { - // Checks if inventory selected exists - println!( - "🗃️ {}", - format!( - "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() - ) - .green() - ); - for file in fs::read_dir(&slotdir).unwrap() { - let file = file.unwrap().path(); - - println!( - " - [{}] {}{}", - if file.clone().is_dir() { - "D".blue() - } else if file.clone().is_symlink() { - "S".yellow() - } else { - "F".green() - }, - file.clone() - .file_name() - .unwrap() - .to_os_string() - .into_string() - .unwrap(), - if file.clone().is_file() { - format!( - " ({}B)", - SizeFormatterBinary::new(file.clone().metadata().unwrap().len()) - ) - } else { - format!("") - } - ); - } - } - } else { + if !slotdir.is_dir() { + // Detects if the consulted slot or directory exists bail!( "{}", format!( @@ -158,6 +86,82 @@ pub fn list(slot: &str, dir: &str) -> Result<()> { ) .red() ); + }; + + if fs::read_dir(&slotdir).unwrap().count() == 0 { + // Detects if the slot or directory has any contents + println!( + "🗃️ {}", + format!( + "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() + ); + } else { + println!( + "🗃️ {}", + format!( + "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() + ) + .green() + ); + for file in fs::read_dir(&slotdir).unwrap() { + let file = file.unwrap().path(); + + println!( + " - [{}] {}{}", + if file.clone().is_dir() { + "D".blue() + } else if file.clone().is_symlink() { + "S".yellow() + } else { + "F".green() + }, + file.clone() + .file_name() + .unwrap() + .to_os_string() + .into_string() + .unwrap(), + if file.clone().is_file() { + format!( + " ({}B)", + SizeFormatterBinary::new(file.clone().metadata().unwrap().len()) + ) + } else { + format!("") + } + ); + } } Ok(()) } @@ -171,7 +175,7 @@ pub fn switch() -> Result<()> { .iter() .collect(); - let rename_error = "Vento was unable to switch slots. Try running vento init and try again"; + let rename_error = "Vento was unable to switch slots. Try running \"vento -i\" and try again"; fs::rename(&active, &temp).context(rename_error)?; fs::rename(&inactive, &active).context(rename_error)?; @@ -182,7 +186,7 @@ pub fn switch() -> Result<()> { } fn create_slots() -> Result<()> { - // Used only on init. Creates all required directories. + // Used only on init. Creates all required directories let active = &common::env_config()?[1]; let inactive = &common::env_config()?[2]; diff --git a/src/item.rs b/src/item.rs index 52e4f29..4a2ca88 100644 --- a/src/item.rs +++ b/src/item.rs @@ -25,15 +25,16 @@ use std::fs; use std::path::{Path, PathBuf}; pub fn take(file: &String, slot: &String) -> Result<()> { + // Takes a file or directory let ventodir = &common::env_config()?[0]; if !ventodir.is_dir() { + // Detects if Vento hasn't been initialized and bails if so bail!( "{}", "Vento not initialized. Run \"vento -i\" to initialize Vento.".red() ); }; - // Takes a file or directory let slotdir: PathBuf = match slot.as_str() { "active" | "a" => common::env_config()?[1].clone(), "inactive" | "i" => common::env_config()?[2].clone(), @@ -41,6 +42,7 @@ pub fn take(file: &String, slot: &String) -> Result<()> { }; if !slotdir.is_dir() { + // Detects if the slot provided exists bail!( "{}", format!( @@ -56,11 +58,15 @@ pub fn take(file: &String, slot: &String) -> Result<()> { let destpath: PathBuf = [&slotdir, &Path::new(file).to_path_buf()].iter().collect(); if Path::exists(&destpath) { + // Checks if there's a file with the same name in the inventory. bail!( "{}", "A file with the same name already exists in your inventory!".red() ); - } else if sourcepath.is_file() | sourcepath.is_symlink() { + } + + if sourcepath.is_file() | sourcepath.is_symlink() { + // Checks the path's file type fs::copy(&file, &destpath).context("Vento was unable to copy the file.")?; fs::remove_file(&file).context("Vento was unable to remove the file.")?; } else if sourcepath.is_dir() { @@ -69,6 +75,7 @@ pub fn take(file: &String, slot: &String) -> Result<()> { } else { bail!("{}", "No such file or directory.".red()); } + Ok(()) } @@ -77,6 +84,7 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> { let ventodir = &common::env_config()?[0]; if !ventodir.is_dir() { + // Detects if Vento hasn't been initialized and bails if so bail!( "{}", "Vento not initialized. Run \"vento -i\" to initialize Vento.".red() @@ -90,6 +98,7 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> { }; if !slotdir.is_dir() { + // Detects if the slot provided exists bail!( "{}", format!( @@ -110,9 +119,12 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> { .collect(); if Path::exists(&destpath) { - // HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked + // Checks if there's a file with the same name in the destination path. bail!("{}", "A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.".red()); - } else if sourcepath.is_file() | sourcepath.is_symlink() { + } + + if sourcepath.is_file() | sourcepath.is_symlink() { + // Checks the path's file type fs::copy(&sourcepath, &destpath).context("Vento was unable to copy the file.")?; fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?; } else if sourcepath.is_dir() {