From c1533e6b41efc0f3615187a0e9471fc191e39ae9 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Thu, 3 Nov 2022 19:26:15 -0300 Subject: [PATCH] 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. --- src/bin/vento.rs | 4 +-- src/common.rs | 2 ++ src/history.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/inv.rs | 7 ++++++ src/item.rs | 38 ---------------------------- src/lib.rs | 1 + 6 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 src/history.rs diff --git a/src/bin/vento.rs b/src/bin/vento.rs index d2bc886..676f9e1 100644 --- a/src/bin/vento.rs +++ b/src/bin/vento.rs @@ -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], "")?, diff --git a/src/common.rs b/src/common.rs index 729cddc..668826c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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", } )?; diff --git a/src/history.rs b/src/history.rs new file mode 100644 index 0000000..fa4acd8 --- /dev/null +++ b/src/history.rs @@ -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 . + * + */ +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(()) +} diff --git a/src/inv.rs b/src/inv.rs index c5ff0cf..29b3b9b 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -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(()) } diff --git a/src/item.rs b/src/item.rs index e592654..9a94f74 100644 --- a/src/item.rs +++ b/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(()) -} diff --git a/src/lib.rs b/src/lib.rs index 5729571..9090ff5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,5 +19,6 @@ mod common; pub mod help; +pub mod history; pub mod inv; pub mod item;