1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-07-23 04:50:53 +00:00

Compare commits

..

No commits in common. "edcf1408ee8afcfae845063fec55716e76d65857" and "756c56ea9f7cc9909ff08e84b6ed26722f039a14" have entirely different histories.

5 changed files with 63 additions and 68 deletions

View file

@ -18,4 +18,3 @@ After installing, run `vento init`. This will create a `.vento` folder in your h
## Credits
- [Chesapeake](https://moth.monster/) for the original concept
- [jo!](https://codeberg.org/j0) for helping me with Rust concepts!

View file

@ -17,11 +17,11 @@
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
use std::path::{Path, PathBuf};
use std::process;
pub fn env_config() -> Result<Vec<PathBuf>> {
pub fn env_config() -> Vec<PathBuf> {
// Configures the directories for Vento
let emptypath = PathBuf::new();
let home = match dirs::home_dir() {
@ -29,15 +29,17 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
_ => PathBuf::new(),
};
if home == emptypath {
bail!("❌ {}", format!("Vento was unable to detect your home folder. Have you configured your environment correctly?").red());
};
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();
println!("{}", format!("Vento was unable to detect your home folder. Have you configured your environment correctly?").red());
process::exit(1);
} else {
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();
Ok(vec![vento_dir, active_dir, inactive_dir])
return vec![vento_dir, active_dir, inactive_dir];
};
}

View file

@ -18,16 +18,15 @@
*/
use super::common;
use anyhow::{bail, Context, Result};
use colored::Colorize;
use size_format::SizeFormatterBinary;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::{fs, process};
pub fn init() -> Result<()> {
pub fn init() {
// Initializes Vento
let ventodir = &common::env_config()?[0];
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
@ -38,20 +37,23 @@ pub fn init() -> Result<()> {
.read_line(&mut answer)
.expect("❌ Failed to read input");
match answer.as_str().trim() {
"y" | "Y" => fs::remove_dir_all(&ventodir)?,
_ => 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()?;
Ok(())
create_slots();
}
pub fn list(slot: &str, dir: &str) -> Result<()> {
pub fn list(slot: &str, dir: &str) {
// Lists files in inventory
let mut slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(),
"inactive" | "i" => common::env_config()?[2].clone(),
"active" | "a" => common::env_config()[1].clone(),
"inactive" | "i" => common::env_config()[2].clone(),
_ => PathBuf::new(),
};
@ -60,8 +62,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
}
if dir.to_string().contains("..") {
bail!("{}", format!("Cannot access parent.").red());
// process::exit(1);
println!("{}", format!("Cannot access parent.").red());
process::exit(1);
}
if slotdir.is_dir() {
@ -151,14 +153,13 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
.red()
);
}
Ok(())
}
pub fn switch() -> Result<()> {
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 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();
@ -171,21 +172,20 @@ pub fn switch() -> Result<()> {
.expect("❌ Vento was unable to switch slots. Try running vento init and try again");
println!("🎉 {}", format!("Switched inventory slots!").green());
Ok(())
}
fn create_slots() -> Result<()> {
fn create_slots() {
// Used only on init. Creates all required directories.
let active = &common::env_config()?[1];
let inactive = &common::env_config()?[2];
let active = &common::env_config()[1];
let inactive = &common::env_config()[2];
fs::create_dir_all(active)
.context("❌ Vento was unable to initalize. Do you have the correct permissions?")?;
.expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
fs::create_dir_all(inactive)
.context("❌ Vento was unable to initalize. Do you have the correct permissions?")?;
.expect("❌ Vento was unable to initalize. Do you have the correct permissions?");
println!(
"🎉 {}", format!("Vento has been succesfully initialized!").green()
"🎉 {}",
format!("Vento has been succesfully initialized!").green()
);
Ok(())
}

View file

@ -18,21 +18,20 @@
*/
use super::common;
use anyhow::{bail, Context, Result};
use colored::Colorize;
use fs_extra::dir::{move_dir, CopyOptions};
use std::fs;
use std::path::{Path, PathBuf};
pub fn take(file: &String) -> Result<()> {
pub fn take(file: &String) {
// Takes a file or directory
let active = &common::env_config()?[1];
let active = &common::env_config()[1];
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) {
bail!(
println!(
"❌ {}",
format!("A file with the same name already exists in your inventory!").red()
);
@ -45,12 +44,11 @@ pub fn take(file: &String) -> Result<()> {
} else {
println!("{}", format!("No such file or directory.").red());
}
Ok(())
}
pub fn drop(file: &String, dest: PathBuf) -> Result<()> {
pub fn drop(file: &String, dest: PathBuf) {
// Drops a file or directory
let active = &common::env_config()?[1];
let active = &common::env_config()[1];
let sourcepath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
let destpath: PathBuf = [
@ -62,17 +60,16 @@ pub fn drop(file: &String, dest: PathBuf) -> Result<()> {
if Path::exists(&destpath) {
// HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked
bail!("{}", format!("A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.").red());
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).context("❌ Vento was unable to copy the file.")?;
fs::remove_file(&sourcepath).context("❌ Vento was unable to remove the file.")?;
fs::copy(&sourcepath, &destpath).expect("❌ Vento was unable to copy the file.");
fs::remove_file(&sourcepath).expect("❌ Vento was unable to remove the file.");
} 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.");
} else {
bail!("{}", format!("No such file or directory.").red());
println!("{}", format!("No such file or directory.").red());
}
Ok(())
}

View file

@ -17,7 +17,6 @@
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
use std::env;
use std::path::Path;
@ -27,29 +26,29 @@ mod common;
mod inv;
mod item;
fn main() -> Result<()> {
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() {
"help" | "h" => help()?,
"init" | "i" => inv::init()?,
"help" | "h" => help(),
"init" | "i" => inv::init(),
"list" | "l" => match args.len() {
4 => inv::list(args[2].as_str(), args[3].as_str())?,
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())?,
"active" | "a" | "inactive" | "i" => inv::list(args[2].as_str(), ""),
_ => inv::list("active", args[2].as_str()),
},
2 => inv::list("active", "")?,
_ => bail!("{}", format!("Too many arguments.").red()),
2 => inv::list("active", ""),
_ => println!("{}", format!("Too many arguments.").red()),
},
"switch" | "s" => inv::switch()?,
"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
item::take(&args[2])?;
item::take(&args[2]);
} else {
bail!("{}", format!("You need to specify a file.").red())
println!("{}", format!("You need to specify a file.").red())
};
}
"drop" | "d" => {
@ -64,9 +63,9 @@ fn main() -> Result<()> {
process::exit(1);
}
},
)?;
);
} else if args.len() == 4 {
item::drop(&args[2], Path::new(&args[3]).to_path_buf())?;
item::drop(&args[2], Path::new(&args[3]).to_path_buf());
} else {
println!("{}", format!("You need to specify a file.").red())
};
@ -77,12 +76,11 @@ fn main() -> Result<()> {
}
} else {
// If the user provides no commands, it'll fall back to the help guide
help()?;
help();
}
Ok(())
}
fn help() -> Result<()> {
fn help() {
// A quick guide to move around in Vento
println!(
"{}, a CLI inventory for your files
@ -106,5 +104,4 @@ fn help() -> Result<()> {
format!("init | i").bold().green(),
format!("help | h").bold().green()
);
Ok(())
}