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.
This commit is contained in:
Lux Aliaga 2022-10-24 17:57:46 -03:00
parent 5f5ae6e41e
commit f8750f3515
Signed by: lux
GPG Key ID: B56C805968637437
2 changed files with 46 additions and 0 deletions

View File

@ -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<Settings> {
// Configures the directories for Vento
let home = match dirs::home_dir() {
@ -85,3 +98,24 @@ fn dir_config() -> Result<String> {
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(())
}

View File

@ -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(),