1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2024-11-21 06:13:16 +00:00

Compare commits

...

7 commits

Author SHA1 Message Date
Lux Aliaga dadb0d721f
readme: Fixed typo
The command to undo the last action is "vento -u" and not "vento -c".
2022-11-03 22:56:55 -03:00
Lux Aliaga ab313e2f50
manifest: Bump version to v1.1.1
Enough changes have been done for a newer version and I can confidently
call this a stable release. Enjoy.
2022-11-03 21:32:30 -03:00
Lux Aliaga ef3dcf8acc
inv: Emoji consistency
The slot switching action had a different emoji for confirming the
command was successful compared to other actions, so I switched it for
the green checkmark.
2022-11-03 21:15:00 -03:00
Lux Aliaga 1cbfc5a568
history: Nicer messages for undo result
It includes more details such as:
- What action was undone
- What item was affected in the action
- What that action involved
2022-11-03 21:06:28 -03:00
Lux Aliaga 19aa1955c8
inv: Add "message" arg to slot switching
When undoing a slot switching command, it would show the message
normally show when running the command by itself, which is not supposed
to happen. This commit fixes that by adding a boolean argument that will
print the message when true.
2022-11-03 19:42:39 -03:00
Lux Aliaga c1533e6b41
inv: Include slot switching in undoable actions
Turns out the history didn't include undoing slot switching, so an
action could accidentally be undone in the wrong slot. I've fixed that
and also reorganized the source so undoing actions wouldn't be pegged to
the item module.
2022-11-03 19:26:15 -03:00
Lux Aliaga 6ad43bbf82
src: Remove periods from end of sentences
For many messages there were some consistency issues in terms of
punctuation. I've fixed this issue by removing every period from every
sentence.
2022-11-03 19:08:50 -03:00
10 changed files with 144 additions and 59 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -60,7 +60,7 @@ $ vento
$ vento -c
// undoing last action
$ vento -c
$ vento -u
// 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, inv, item};
use vento::{help, history, inv};
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()?,
"-u" | "--undo" => item::undo()?,
"-c" | "--switch" => inv::switch(true)?,
"-u" | "--undo" => history::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,6 +40,7 @@ pub struct HistoryData {
pub enum Action {
Take,
Drop,
Switch,
}
/// Provides required variables for Vento
@ -118,6 +119,7 @@ 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(),

111
src/history.rs Normal file
View file

@ -0,0 +1,111 @@
/*
* 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() -> Result<()> {
pub fn switch(message: bool) -> Result<()> {
let ventodir = &common::env_config()?.vento_dir;
let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?.inactive_dir;
@ -179,7 +179,16 @@ pub fn switch() -> Result<()> {
fs::rename(&inactive, &active).context(rename_error)?;
fs::rename(&temp, &inactive).context(rename_error)?;
println!("🎉 {}", "Switched inventory slots!".green());
common::history(common::HistoryData {
path: PathBuf::new(),
file: String::new(),
slot: String::new(),
action: common::Action::Switch,
})?;
if message {
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,41 +177,3 @@ 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,5 +19,6 @@
mod common;
pub mod help;
pub mod history;
pub mod inv;
pub mod item;