1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-11-29 08:05:43 +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};
fn main() -> Result<()> {
// Handles args in Drop
let args: Vec<String> = 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(())

View file

@ -23,9 +23,11 @@ use std::env;
use vento::{help, item};
fn main() -> Result<()> {
// Handles args in Vento
let args: Vec<String> = 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(())

View file

@ -23,10 +23,12 @@ use std::env;
use vento::{help, inv};
fn main() -> Result<()> {
// Handles args in Vento
let args: Vec<String> = 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(())

View file

@ -49,6 +49,7 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
}
fn dir_config() -> Result<String> {
// 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,

View file

@ -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

View file

@ -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];

View file

@ -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() {