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

Compare commits

..

No commits in common. "dadb0d721f5afcbfa8f2e0e1e584b15cb982b819" and "af0a27464b978bff902d745dcb01ffc806a9cd4b" have entirely different histories.

10 changed files with 59 additions and 144 deletions

2
Cargo.lock generated
View file

@ -592,7 +592,7 @@ checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "vento"
version = "1.1.1"
version = "1.1.0"
dependencies = [
"anyhow",
"colored",

View file

@ -1,6 +1,6 @@
[package]
name = "vento"
version = "1.1.1"
version = "1.1.0"
edition = "2021"
readme = "README.md"

View file

@ -60,7 +60,7 @@ $ vento
$ vento -c
// undoing last action
$ vento -u
$ vento -c
// taking a file or directory
$ take <file|directory>

View file

@ -20,7 +20,7 @@
use anyhow::{bail, Result};
use colored::Colorize;
use std::env;
use vento::{help, history, inv};
use vento::{help, inv, item};
fn main() -> Result<()> {
// Handles args in Vento
@ -38,12 +38,12 @@ fn main() -> Result<()> {
match args[1].as_str() {
"-h" | "--help" => help::vento()?,
"-i" | "--init" => inv::init()?,
"-c" | "--switch" => inv::switch(true)?,
"-u" | "--undo" => history::undo()?,
"-c" | "--switch" => inv::switch()?,
"-u" | "--undo" => item::undo()?,
"-s" => match args.len() {
4 => inv::list(&args[2], &args[3])?,
3 => inv::list(&args[2], "")?,
2 => bail!("{}", "You need to specify a slot".red()),
2 => bail!("{}", "You need to specify a slot.".red()),
_ => bail!("{}", "Too many arguments".red()),
},
_ => inv::list("active", args[1].as_str())?,

View file

@ -40,7 +40,6 @@ pub struct HistoryData {
pub enum Action {
Take,
Drop,
Switch,
}
/// Provides required variables for Vento
@ -119,7 +118,6 @@ pub fn history(data: HistoryData) -> Result<()> {
match data.action {
Action::Take => "take",
Action::Drop => "drop",
Action::Switch => "switch",
}
)?;

View file

@ -71,7 +71,7 @@ pub fn drop() -> Result<()> {
© 2022 Lux Aliaga. Licensed under GPLv3
{}
- {}: Takes a file off the inventory and drops it
- {}: Takes a file off the inventory and drops it.
- {}: Displays this message",
"Drop".bold().blue(),
"Usage:".bold(),

View file

@ -1,111 +0,0 @@
/*
* Vento, a CLI inventory for your files.
* Copyright (C) 2022 Lux Aliaga
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use crate::{common, inv, item};
use anyhow::{bail, Result};
use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};
/// Undoes the last action made by Vento using the history file located on the Vento directory
pub fn undo() -> Result<()> {
let lastpath: PathBuf = [
common::env_config()?.vento_dir,
Path::new("last").to_path_buf(),
]
.iter()
.collect();
let lastfile = fs::read_to_string(lastpath)?;
let mut contents = vec![];
for line in lastfile.lines() {
contents.push(line);
}
if contents.len() != 4 {
bail!("Invalid history length".red());
}
match contents[3] {
"take" => {
let destpath = Path::new(contents[0]).to_path_buf();
item::drop(&String::from(contents[1]), contents[2], destpath, false)?;
}
"drop" => {
let path = vec![contents[0], contents[1]].join("/");
item::take(&path, contents[2], false)?;
}
"switch" => {
inv::switch(false)?;
}
_ => bail!("Illegal action".red()),
}
println!(
"✅ {}",
format!(
"{}{}{}",
match contents[3] {
"take" => "Take",
"drop" => "Drop",
"switch" => "Switch",
_ => "Unknown",
}
.bold(),
" action undone".green(),
match contents[3] {
"take" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
contents[0],
" to ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot)".green(),
),
"drop" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot to ".green(),
contents[0],
")".green(),
),
_ => String::from(""),
}
)
);
Ok(())
}

View file

@ -53,7 +53,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
);
}
@ -70,7 +70,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
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() {
@ -78,7 +78,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
bail!(
"{}",
format!(
"No such slot or directory. Valid slots are {} and {}",
"No such slot or directory. Valid slots are {} and {}.",
"active".green().bold(),
"inactive".blue().bold()
)
@ -91,7 +91,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
println!(
"🗃️ {}",
format!(
"No files in {}{}",
"No files in {}{}.",
match slot {
"active" => slot.bold(),
_ => slot.blue().bold(),
@ -165,7 +165,7 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
}
/// Switches inevntory slots between each other, making the currently active inventory inactive and viceversa
pub fn switch(message: bool) -> Result<()> {
pub fn switch() -> Result<()> {
let ventodir = &common::env_config()?.vento_dir;
let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?.inactive_dir;
@ -179,16 +179,7 @@ pub fn switch(message: bool) -> Result<()> {
fs::rename(&inactive, &active).context(rename_error)?;
fs::rename(&temp, &inactive).context(rename_error)?;
common::history(common::HistoryData {
path: PathBuf::new(),
file: String::new(),
slot: String::new(),
action: common::Action::Switch,
})?;
if message {
println!("{}", "Switched inventory slots!".green());
}
println!("🎉 {}", "Switched inventory slots!".green());
Ok(())
}

View file

@ -32,7 +32,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
);
};
let slotdir: PathBuf = match slot {
@ -46,7 +46,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
bail!(
"{}",
format!(
"No such slot. Valid slots are {} and {}",
"No such slot. Valid slots are {} and {}.",
"active".green().bold(),
"inactive".blue().bold()
)
@ -78,7 +78,7 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
let options = CopyOptions::new();
move_dir(&file, &slotdir, &options)?;
} else {
bail!("{}", "No such file or directory".red());
bail!("{}", "No such file or directory.".red());
}
common::history(common::HistoryData {
@ -109,7 +109,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
// Detects if Vento hasn't been initialized and bails if so
bail!(
"{}",
"Vento not initialized. Run \"vento -i\" to initialize Vento".red()
"Vento not initialized. Run \"vento -i\" to initialize Vento.".red()
);
};
@ -124,7 +124,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
bail!(
"{}",
format!(
"No such slot. Valid slots are {} and {}",
"No such slot. Valid slots are {} and {}.",
"active".green().bold(),
"inactive".blue().bold()
)
@ -142,7 +142,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
if Path::exists(&destpath) {
// 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());
}
if sourcepath.is_file() | sourcepath.is_symlink() {
@ -154,7 +154,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
let options = CopyOptions::new();
move_dir(&sourcepath, &destpath, &options)?;
} else {
bail!("{}", "No such file or directory".red());
bail!("{}", "No such file or directory.".red());
}
destpath.pop();
@ -177,3 +177,41 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
Ok(())
}
/// Undoes the last action made by Vento using the history file located on the Vento directory
pub fn undo() -> Result<()> {
let lastpath: PathBuf = [
common::env_config()?.vento_dir,
Path::new("last").to_path_buf(),
]
.iter()
.collect();
let lastfile = fs::read_to_string(lastpath)?;
let mut contents = vec![];
for line in lastfile.lines() {
contents.push(line);
}
if contents.len() != 4 {
bail!("Invalid history length".red());
}
match contents[3] {
"take" => {
let destpath = Path::new(contents[0]).to_path_buf();
drop(&String::from(contents[1]), contents[2], destpath, false)?;
}
"drop" => {
let path = vec![contents[0], contents[1]].join("/");
take(&path, contents[2], false)?;
}
_ => bail!("Illegal action".red()),
}
println!("{}", "Last action undone".green());
Ok(())
}

View file

@ -19,6 +19,5 @@
mod common;
pub mod help;
pub mod history;
pub mod inv;
pub mod item;