mirror of
https://git.sr.ht/~nixgoat/vento
synced 2024-11-16 03:53:04 +00:00
src: Code cleanup
To demessify the project I've decided to clean the code a bit and make it a little less painful to see. The main change here is that instead of defining every single time the active and inactive directories it calls it from a vector in common.rs, which is cleaner and allows for easily implementing a config file to customize those values later on. Also, version bump!
This commit is contained in:
parent
5d8771be3c
commit
c2e276c6e2
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -171,7 +171,7 @@ checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
|
|||
|
||||
[[package]]
|
||||
name = "vento"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"colored",
|
||||
"dirs",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "vento"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
@ -17,20 +17,29 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use std::process;
|
||||
use std::path::{Path, PathBuf};
|
||||
use colored::Colorize;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
||||
pub fn env_config() -> PathBuf { // Configures the directory for Vento
|
||||
pub fn env_config() -> Vec<PathBuf> {
|
||||
// Configures the directories for Vento
|
||||
let emptypath = PathBuf::new();
|
||||
let home = match dirs::home_dir() {
|
||||
Option::Some(dir) => dir,
|
||||
_ => PathBuf::new()
|
||||
_ => PathBuf::new(),
|
||||
};
|
||||
if home == emptypath {
|
||||
println!("❌ {}", format!("Vento was unable to detect your home folder. Have you configured your environment correctly?").red());
|
||||
process::exit(1);
|
||||
} else {
|
||||
return [home, Path::new(".vento").to_path_buf()].iter().collect();
|
||||
let vento_dir = [home, Path::new(".vento").to_path_buf()].iter().collect();
|
||||
let active_dir = [&vento_dir, &Path::new("active").to_path_buf()]
|
||||
.iter()
|
||||
.collect();
|
||||
let inactive_dir = [&vento_dir, &Path::new("inactive").to_path_buf()]
|
||||
.iter()
|
||||
.collect();
|
||||
|
||||
return vec![vento_dir, active_dir, inactive_dir];
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
124
src/inv.rs
124
src/inv.rs
|
@ -17,16 +17,18 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use std::{fs, process};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::io::{self, Write};
|
||||
use colored::Colorize;
|
||||
use super::common;
|
||||
use colored::Colorize;
|
||||
use std::io::{self, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{fs, process};
|
||||
|
||||
pub fn init() { // Initializes Vento
|
||||
let ventodir: PathBuf = common::env_config();
|
||||
|
||||
if ventodir.is_dir() { // Checks if Vento has already been initialized and prompts the user if they want to initialize it again
|
||||
pub fn init() {
|
||||
// Initializes Vento
|
||||
let ventodir = &common::env_config()[0];
|
||||
|
||||
if ventodir.is_dir() {
|
||||
// Checks if Vento has already been initialized and prompts the user if they want to initialize it again
|
||||
let mut answer = String::new();
|
||||
print!("⚠️ {} {}", format!("WARNING:").bold().red(), "Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) ");
|
||||
let _ = io::stdout().flush();
|
||||
|
@ -34,51 +36,95 @@ pub fn init() { // Initializes Vento
|
|||
.read_line(&mut answer)
|
||||
.expect("❌ Failed to read input");
|
||||
match answer.as_str().trim() {
|
||||
"y" | "Y" => {fs::remove_dir_all(&ventodir).expect("❌ Vento was unable to initalize. Do you have the correct permissions?");},
|
||||
"n" | "N" | _ => process::exit(0)
|
||||
"y" | "Y" => {
|
||||
fs::remove_dir_all(&ventodir).expect(
|
||||
"❌ Vento was unable to initalize. Do you have the correct permissions?",
|
||||
);
|
||||
}
|
||||
"n" | "N" | _ => process::exit(0),
|
||||
};
|
||||
};
|
||||
|
||||
create_slots(ventodir);
|
||||
|
||||
create_slots();
|
||||
}
|
||||
|
||||
pub fn list(slot: &str) { // Lists files in inventory
|
||||
let ventodir: PathBuf = common::env_config();
|
||||
let slotdir: PathBuf = [ventodir.to_path_buf(), Path::new(slot).to_path_buf()].iter().collect();
|
||||
pub fn list(slot: &str) {
|
||||
// Lists files in inventory
|
||||
let slotdir: PathBuf = match slot {
|
||||
"active" | "a" => common::env_config()[1].clone(),
|
||||
"inactive" | "i" => common::env_config()[2].clone(),
|
||||
_ => PathBuf::new(),
|
||||
};
|
||||
|
||||
if slotdir.is_dir() { // Checks if inventory selected exists
|
||||
println!("🗃️ {}", format!("Files in {} inventory:", match slot {
|
||||
"active" => format!("{}", slot).bold(),
|
||||
"inactive" | _ => format!("{}", slot).blue().bold()
|
||||
}).green());
|
||||
if slotdir.is_dir() {
|
||||
// Checks if inventory selected exists
|
||||
println!(
|
||||
"🗃️ {}",
|
||||
format!(
|
||||
"Files in {} inventory:",
|
||||
match slot {
|
||||
"active" => format!("{}", slot).bold(),
|
||||
"inactive" | _ => format!("{}", slot).blue().bold(),
|
||||
}
|
||||
)
|
||||
.green()
|
||||
);
|
||||
for file in fs::read_dir(&slotdir).unwrap() {
|
||||
println!(" - {}", file.unwrap().path().file_name().unwrap().to_os_string().into_string().unwrap());
|
||||
};
|
||||
println!(
|
||||
" - {}",
|
||||
file.unwrap()
|
||||
.path()
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_os_string()
|
||||
.into_string()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
println!("❌ {}", format!("Vento was unable to read that slot. Valid slots are {} and {}.", format!("active").green(), format!("inactive").blue()).red());
|
||||
println!(
|
||||
"❌ {}",
|
||||
format!(
|
||||
"Vento was unable to read that slot. Valid slots are {} and {}.",
|
||||
format!("active").green(),
|
||||
format!("inactive").blue()
|
||||
)
|
||||
.red()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch() { // Switches between inventory slots
|
||||
let ventodir: PathBuf = common::env_config();
|
||||
let active: PathBuf = [ventodir.to_path_buf(), Path::new("active").to_path_buf()].iter().collect();
|
||||
let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()].iter().collect();
|
||||
let inactive: PathBuf = [ventodir.to_path_buf(), Path::new("inactive").to_path_buf()].iter().collect();
|
||||
|
||||
fs::rename(&active, &temp).expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
fs::rename(&inactive, &active).expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
fs::rename(&temp, &inactive).expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
pub fn switch() {
|
||||
// Switches between inventory slots
|
||||
let ventodir = &common::env_config()[0];
|
||||
let active = &common::env_config()[1];
|
||||
let inactive = &common::env_config()[2];
|
||||
let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()]
|
||||
.iter()
|
||||
.collect();
|
||||
|
||||
fs::rename(&active, &temp)
|
||||
.expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
fs::rename(&inactive, &active)
|
||||
.expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
fs::rename(&temp, &inactive)
|
||||
.expect("❌ Vento was unable to switch slots. Try running vento init and try again");
|
||||
|
||||
println!("🎉 {}", format!("Switched inventory slots!").green());
|
||||
}
|
||||
|
||||
fn create_slots() {
|
||||
// Used only on init. Creates all required directories.
|
||||
let active = &common::env_config()[1];
|
||||
let inactive = &common::env_config()[2];
|
||||
|
||||
fn create_slots(dir: PathBuf) { // Used only on init. Creates all required directories.
|
||||
let active: PathBuf = [dir.to_path_buf(), Path::new("active").to_path_buf()].iter().collect();
|
||||
let inactive: PathBuf = [dir.to_path_buf(), Path::new("inactive").to_path_buf()].iter().collect();
|
||||
fs::create_dir_all(active)
|
||||
.expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
|
||||
fs::create_dir_all(inactive)
|
||||
.expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
|
||||
|
||||
fs::create_dir_all(active).expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
|
||||
fs::create_dir_all(inactive).expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
|
||||
|
||||
println!("🎉 {}", format!("Vento has been succesfully initialized!").green());
|
||||
println!(
|
||||
"🎉 {}",
|
||||
format!("Vento has been succesfully initialized!").green()
|
||||
);
|
||||
}
|
||||
|
|
47
src/item.rs
47
src/item.rs
|
@ -17,21 +17,24 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use std::fs;
|
||||
use fs_extra::dir::{CopyOptions, move_dir};
|
||||
use std::path::{Path, PathBuf};
|
||||
use colored::Colorize;
|
||||
use super::common;
|
||||
use colored::Colorize;
|
||||
use fs_extra::dir::{move_dir, CopyOptions};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn take(file: &String) {
|
||||
// Takes a file or directory
|
||||
let active = &common::env_config()[1];
|
||||
|
||||
pub fn take(file: &String) { // Takes a file or directory
|
||||
let ventodir = common::env_config();
|
||||
let active: PathBuf = [ventodir.to_path_buf(), Path::new("active").to_path_buf()].iter().collect();
|
||||
|
||||
let sourcepath: PathBuf = Path::new(&file).to_path_buf();
|
||||
let destpath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
|
||||
|
||||
|
||||
if Path::exists(&destpath) {
|
||||
println!("❌ {}", format!("A file with the same name already exists in your inventory!").red());
|
||||
println!(
|
||||
"❌ {}",
|
||||
format!("A file with the same name already exists in your inventory!").red()
|
||||
);
|
||||
} else if sourcepath.is_file() | sourcepath.is_symlink() {
|
||||
fs::copy(&file, &destpath).expect("❌ Vento was unable to copy the file.");
|
||||
fs::remove_file(&file).expect("❌ Vento was unable to remove the file.");
|
||||
|
@ -43,15 +46,20 @@ pub fn take(file: &String) { // Takes a file or directory
|
|||
}
|
||||
}
|
||||
|
||||
pub fn drop(file: &String, dest: PathBuf) { // Drops a file or directory
|
||||
let ventodir = common::env_config();
|
||||
// I know I've declared the same variable multiple times through this project. I might move this to the common file and just call it from there every single time I need it, but that'll be for later.
|
||||
let active: PathBuf = [ventodir.to_path_buf(), Path::new("active").to_path_buf()].iter().collect();
|
||||
|
||||
let sourcepath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
|
||||
let destpath: PathBuf = [Path::new(&dest).to_path_buf(), Path::new(file).to_path_buf()].iter().collect();
|
||||
pub fn drop(file: &String, dest: PathBuf) {
|
||||
// Drops a file or directory
|
||||
let active = &common::env_config()[1];
|
||||
|
||||
if Path::exists(&destpath) { // HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked
|
||||
let sourcepath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
|
||||
let destpath: PathBuf = [
|
||||
Path::new(&dest).to_path_buf(),
|
||||
Path::new(file).to_path_buf(),
|
||||
]
|
||||
.iter()
|
||||
.collect();
|
||||
|
||||
if Path::exists(&destpath) {
|
||||
// HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked
|
||||
println!("❌ {}", format!("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() {
|
||||
fs::copy(&sourcepath, &destpath).expect("❌ Vento was unable to copy the file.");
|
||||
|
@ -59,7 +67,8 @@ pub fn drop(file: &String, dest: PathBuf) { // Drops a file or directory
|
|||
} else if sourcepath.is_dir() {
|
||||
let destpath: PathBuf = Path::new(&dest).to_path_buf();
|
||||
let options = CopyOptions::new();
|
||||
move_dir(&sourcepath, &destpath, &options).expect("❌ Vento was unable to move the directory.");
|
||||
move_dir(&sourcepath, &destpath, &options)
|
||||
.expect("❌ Vento was unable to move the directory.");
|
||||
} else {
|
||||
println!("❌ {}", format!("No such file or directory.").red());
|
||||
}
|
||||
|
|
85
src/main.rs
85
src/main.rs
|
@ -17,60 +17,72 @@
|
|||
*
|
||||
*/
|
||||
|
||||
use std::env;
|
||||
use std::process;
|
||||
use std::path::{Path};
|
||||
use colored::Colorize;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
|
||||
mod common;
|
||||
mod inv;
|
||||
mod item;
|
||||
mod common;
|
||||
|
||||
fn main() {
|
||||
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
|
||||
match args[1].as_str() {
|
||||
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
|
||||
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
|
||||
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");
|
||||
};
|
||||
},
|
||||
}
|
||||
"switch" | "s" => inv::switch(),
|
||||
"take" | "t" => {
|
||||
if args.len() == 3 { // Similar thing with list, but change it with a file and it will show an error instead of defaulting to anything
|
||||
"take" | "t" => {
|
||||
if args.len() == 3 {
|
||||
// Similar thing with list, but change it with a file and it will show an error instead of defaulting to anything
|
||||
item::take(&args[2]);
|
||||
} else {
|
||||
println!("❌ {}", format!("You need to specify a file.").red())
|
||||
};
|
||||
},
|
||||
}
|
||||
"drop" | "d" => {
|
||||
if args.len() == 3 { // Tries to get the current directory if the user hasn't provided a "landing location"
|
||||
item::drop(&args[2], match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => {
|
||||
println!("❌ {}", format!("Vento was unable to detect your current directory. Have you configured your environment correctly?").red());
|
||||
process::exit(1);
|
||||
}
|
||||
});
|
||||
if args.len() == 3 {
|
||||
// Tries to get the current directory if the user hasn't provided a "landing location"
|
||||
item::drop(
|
||||
&args[2],
|
||||
match env::current_dir() {
|
||||
Ok(dir) => dir,
|
||||
Err(_) => {
|
||||
println!("❌ {}", format!("Vento was unable to detect your current directory. Have you configured your environment correctly?").red());
|
||||
process::exit(1);
|
||||
}
|
||||
},
|
||||
);
|
||||
} else if args.len() == 4 {
|
||||
item::drop(&args[2], Path::new(&args[3]).to_path_buf());
|
||||
} else {
|
||||
println!("❌ {}", format!("You need to specify a file.").red())
|
||||
};
|
||||
},
|
||||
_ => println!("❔ Command not found. Type \"vento help\" to see all commands available.")
|
||||
}
|
||||
} else { // If the user provides no commands, it'll fall back to the help guide
|
||||
}
|
||||
_ => {
|
||||
println!("❔ Command not found. Type \"vento help\" to see all commands available.")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If the user provides no commands, it'll fall back to the help guide
|
||||
help();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn help() { // A quick guide to move around in Vento
|
||||
println!("{}, a CLI inventory for your files
|
||||
fn help() {
|
||||
// A quick guide to move around in Vento
|
||||
println!(
|
||||
"{}, a CLI inventory for your files
|
||||
© 2022 Lux Aliaga. Licensed under GPLv3
|
||||
|
||||
{}
|
||||
|
@ -80,12 +92,15 @@ fn help() { // A quick guide to move around in Vento
|
|||
- {}: Switches slots
|
||||
- {}: Initializes Vento
|
||||
- {}: Displays this message",
|
||||
format!("Vento").bold().blue(),
|
||||
format!("Usage:").bold(),
|
||||
format!("take | t <file | directory>").bold().green(),
|
||||
format!("drop | d <file | directory> [destination]").bold().green(),
|
||||
format!("list | l [slot]").bold().green(),
|
||||
format!("switch | s").bold().green(),
|
||||
format!("init | i").bold().green(),
|
||||
format!("help | h").bold().green());
|
||||
format!("Vento").bold().blue(),
|
||||
format!("Usage:").bold(),
|
||||
format!("take | t <file | directory>").bold().green(),
|
||||
format!("drop | d <file | directory> [destination]")
|
||||
.bold()
|
||||
.green(),
|
||||
format!("list | l [slot]").bold().green(),
|
||||
format!("switch | s").bold().green(),
|
||||
format!("init | i").bold().green(),
|
||||
format!("help | h").bold().green()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue