From f8750f35155c4561b7efdf5167fc8680967b134a Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Mon, 24 Oct 2022 17:57:46 -0300 Subject: [PATCH] item: Store last action This is part of a user-suggested feature made by @Ephera@lemmy.ml over on Lemmy (https://slrpnk.net/post/103331/comment/24020). It stores the last action so it can later be undone by the user. Maybe if I feel generous I might store a longer history in an SQLite database, but for now a simple text file will do. The command for undoing will follow. --- src/common.rs | 34 ++++++++++++++++++++++++++++++++++ src/item.rs | 12 ++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/common.rs b/src/common.rs index 072b1f2..1dd3a9b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -20,6 +20,8 @@ use anyhow::{bail, Result}; use colored::Colorize; use config::Config; +use std::fs::File; +use std::io::Write; use std::path::{Path, PathBuf}; pub struct Settings { @@ -28,6 +30,17 @@ pub struct Settings { pub inactive_dir: PathBuf, } +pub struct HistoryData { + pub path: PathBuf, + pub file: String, + pub action: Action, +} + +pub enum Action { + Take, + Drop, +} + pub fn env_config() -> Result { // Configures the directories for Vento let home = match dirs::home_dir() { @@ -85,3 +98,24 @@ fn dir_config() -> Result { Ok(result) } + +pub fn history(data: HistoryData) -> Result<()> { + let mut last_path = env_config()?.vento_dir; + last_path.push("last"); + let mut last_file = File::create(last_path)?; + + write!( + &mut last_file, + "{} +{} +{}", + data.path.to_str().unwrap(), + data.file, + match data.action { + Action::Take => "take", + Action::Drop => "drop", + } + )?; + + Ok(()) +} diff --git a/src/item.rs b/src/item.rs index a0536d6..6ae8574 100644 --- a/src/item.rs +++ b/src/item.rs @@ -79,6 +79,12 @@ pub fn take(file: &String, slot: &str) -> Result<()> { bail!("{}", "No such file or directory.".red()); } + common::history(common::HistoryData { + path: sourcelocation.clone(), + file: String::from(filename), + action: common::Action::Take, + })?; + println!( "✅ {} {} {} ", "Took".green(), @@ -147,6 +153,12 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> { destpath.pop(); + common::history(common::HistoryData { + path: destpath.clone(), + file: String::from(file), + action: common::Action::Drop, + })?; + println!( "✅ {} {} {} ", "Dropped".green(),