mirror of https://git.sr.ht/~nixgoat/vento
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.
This commit is contained in:
parent
6ad43bbf82
commit
c1533e6b41
|
@ -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
|
||||
|
@ -39,7 +39,7 @@ fn main() -> Result<()> {
|
|||
"-h" | "--help" => help::vento()?,
|
||||
"-i" | "--init" => inv::init()?,
|
||||
"-c" | "--switch" => inv::switch()?,
|
||||
"-u" | "--undo" => item::undo()?,
|
||||
"-u" | "--undo" => history::undo()?,
|
||||
"-s" => match args.len() {
|
||||
4 => inv::list(&args[2], &args[3])?,
|
||||
3 => inv::list(&args[2], "")?,
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
)?;
|
||||
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* 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;
|
||||
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();
|
||||
crate::item::drop(&String::from(contents[1]), contents[2], destpath, false)?;
|
||||
}
|
||||
"drop" => {
|
||||
let path = vec![contents[0], contents[1]].join("/");
|
||||
crate::item::take(&path, contents[2], false)?;
|
||||
}
|
||||
"switch" => {
|
||||
crate::inv::switch()?;
|
||||
}
|
||||
_ => bail!("Illegal action".red()),
|
||||
}
|
||||
|
||||
println!("✅ {}", "Last action undone".green());
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -179,6 +179,13 @@ pub fn switch() -> 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,
|
||||
})?;
|
||||
|
||||
println!("🎉 {}", "Switched inventory slots!".green());
|
||||
Ok(())
|
||||
}
|
||||
|
|
38
src/item.rs
38
src/item.rs
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -19,5 +19,6 @@
|
|||
|
||||
mod common;
|
||||
pub mod help;
|
||||
pub mod history;
|
||||
pub mod inv;
|
||||
pub mod item;
|
||||
|
|
Loading…
Reference in New Issue