1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-11-30 08:27:17 +00:00

src: Code comments and cleanup

Basically some last refinements to make the code look a bit more
readable and presentable.
This commit is contained in:
Lux Aliaga 2022-09-27 20:05:08 -03:00
parent 6fa0cba8ca
commit 6ec32037db
Signed by: lux
GPG key ID: B56C805968637437
7 changed files with 117 additions and 92 deletions

View file

@ -24,9 +24,11 @@ use std::path::Path;
use vento::{help, item}; use vento::{help, item};
fn main() -> Result<()> { fn main() -> Result<()> {
// Handles args in Drop
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() >= 2 { if args.len() >= 2 {
if args[1].contains("--slot=") { if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() { match args.len() {
4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf())?, 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() { 3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() {
@ -60,6 +62,7 @@ fn main() -> Result<()> {
} }
} }
} else { } else {
// If the user provides no arguments, Drop will display the help message.
help::drop()?; help::drop()?;
} }
Ok(()) Ok(())

View file

@ -23,9 +23,11 @@ use std::env;
use vento::{help, item}; use vento::{help, item};
fn main() -> Result<()> { fn main() -> Result<()> {
// Handles args in Vento
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() >= 2 { if args.len() >= 2 {
if args[1].contains("--slot=") { if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() { match args.len() {
3 => item::take(&args[2], &args[1].replace("--slot=", ""))?, 3 => item::take(&args[2], &args[1].replace("--slot=", ""))?,
2 => bail!("{}", "You need to specify a file".red()), 2 => bail!("{}", "You need to specify a file".red()),
@ -47,6 +49,7 @@ fn main() -> Result<()> {
} }
} }
} else { } else {
// If the user provides no arguments, Take will display the help message.
help::take()?; help::take()?;
} }
Ok(()) Ok(())

View file

@ -23,10 +23,12 @@ use std::env;
use vento::{help, inv}; use vento::{help, inv};
fn main() -> Result<()> { fn main() -> Result<()> {
// Handles args in Vento
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() >= 2 { 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 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=") { if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() { match args.len() {
3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?, 3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?,
2 => inv::list(&args[1].replace("--slot=", ""), "")?, 2 => inv::list(&args[1].replace("--slot=", ""), "")?,
@ -47,7 +49,7 @@ fn main() -> Result<()> {
} }
} }
} else { } 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", "")?; inv::list("active", "")?;
} }
Ok(()) Ok(())

View file

@ -49,6 +49,7 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
} }
fn dir_config() -> Result<String> { fn dir_config() -> Result<String> {
// Handles reading the config file or variables for Vento.
let mut result = String::new(); let mut result = String::new();
let mut config = match dirs::config_dir() { let mut config = match dirs::config_dir() {
Option::Some(dir) => dir, Option::Some(dir) => dir,

View file

@ -44,7 +44,7 @@ pub fn vento() -> Result<()> {
} }
pub fn take() -> Result<()> { pub fn take() -> Result<()> {
// A quick guide to move around in Vento // A quick guide to move around in Take
println!( println!(
"{}, a file grabber for Vento "{}, a file grabber for Vento
© 2022 Lux Aliaga. Licensed under GPLv3 © 2022 Lux Aliaga. Licensed under GPLv3
@ -63,7 +63,7 @@ pub fn take() -> Result<()> {
} }
pub fn drop() -> Result<()> { pub fn drop() -> Result<()> {
// A quick guide to move around in Vento // A quick guide to move around in Drop
println!( println!(
"{}, a file dropper for Vento "{}, a file dropper for Vento
© 2022 Lux Aliaga. Licensed under GPLv3 © 2022 Lux Aliaga. Licensed under GPLv3

View file

@ -49,31 +49,47 @@ pub fn init() -> Result<()> {
pub fn list(slot: &str, dir: &str) -> Result<()> { pub fn list(slot: &str, dir: &str) -> Result<()> {
// Lists files in inventory // 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]; let ventodir = &common::env_config()?[0];
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!( bail!(
"{}", "{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red() "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 != "" { 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(); slotdir = [&slotdir, &Path::new(dir).to_path_buf()].iter().collect();
} }
if dir.to_string().contains("..") { if dir.to_string().contains("..") {
// Basically preventing from listing anything out of bounds. ls and dir exist for that
bail!("{}", "Cannot access parent.".red()); bail!("{}", "Cannot access parent.".red());
} }
if slotdir.is_dir() { if !slotdir.is_dir() {
// Detects if the consulted slot or directory exists
bail!(
"{}",
format!(
"No such slot or directory. Valid slots are {} and {}.",
"active".green().bold(),
"inactive".blue().bold()
)
.red()
);
};
if fs::read_dir(&slotdir).unwrap().count() == 0 { if fs::read_dir(&slotdir).unwrap().count() == 0 {
// Detects if the slot or directory has any contents
println!( println!(
"🗃️ {}", "🗃️ {}",
format!( format!(
@ -95,7 +111,6 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
.green() .green()
); );
} else { } else {
// Checks if inventory selected exists
println!( println!(
"🗃️ {}", "🗃️ {}",
format!( format!(
@ -148,17 +163,6 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
); );
} }
} }
} else {
bail!(
"{}",
format!(
"No such slot or directory. Valid slots are {} and {}.",
"active".green().bold(),
"inactive".blue().bold()
)
.red()
);
}
Ok(()) Ok(())
} }
@ -171,7 +175,7 @@ pub fn switch() -> Result<()> {
.iter() .iter()
.collect(); .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(&active, &temp).context(rename_error)?;
fs::rename(&inactive, &active).context(rename_error)?; fs::rename(&inactive, &active).context(rename_error)?;
@ -182,7 +186,7 @@ pub fn switch() -> Result<()> {
} }
fn create_slots() -> 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 active = &common::env_config()?[1];
let inactive = &common::env_config()?[2]; let inactive = &common::env_config()?[2];

View file

@ -25,15 +25,16 @@ use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub fn take(file: &String, slot: &String) -> Result<()> { pub fn take(file: &String, slot: &String) -> Result<()> {
// Takes a file or directory
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?[0];
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!( bail!(
"{}", "{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red() "Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
); );
}; };
// Takes a file or directory
let slotdir: PathBuf = match slot.as_str() { let slotdir: PathBuf = match slot.as_str() {
"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(),
@ -41,6 +42,7 @@ pub fn take(file: &String, slot: &String) -> Result<()> {
}; };
if !slotdir.is_dir() { if !slotdir.is_dir() {
// Detects if the slot provided exists
bail!( bail!(
"{}", "{}",
format!( 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(); let destpath: PathBuf = [&slotdir, &Path::new(file).to_path_buf()].iter().collect();
if Path::exists(&destpath) { if Path::exists(&destpath) {
// Checks if there's a file with the same name in the inventory.
bail!( bail!(
"{}", "{}",
"A file with the same name already exists in your inventory!".red() "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::copy(&file, &destpath).context("Vento was unable to copy the file.")?;
fs::remove_file(&file).context("Vento was unable to remove the file.")?; fs::remove_file(&file).context("Vento was unable to remove the file.")?;
} else if sourcepath.is_dir() { } else if sourcepath.is_dir() {
@ -69,6 +75,7 @@ pub fn take(file: &String, slot: &String) -> Result<()> {
} else { } else {
bail!("{}", "No such file or directory.".red()); bail!("{}", "No such file or directory.".red());
} }
Ok(()) Ok(())
} }
@ -77,6 +84,7 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> {
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?[0];
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
bail!( bail!(
"{}", "{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red() "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() { if !slotdir.is_dir() {
// Detects if the slot provided exists
bail!( bail!(
"{}", "{}",
format!( format!(
@ -110,9 +119,12 @@ pub fn drop(file: &String, slot: &String, dest: PathBuf) -> Result<()> {
.collect(); .collect();
if Path::exists(&destpath) { 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()); 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::copy(&sourcepath, &destpath).context("Vento was unable to copy the file.")?;
fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?; fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?;
} else if sourcepath.is_dir() { } else if sourcepath.is_dir() {