mirror of https://git.sr.ht/~nixgoat/vento
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:
parent
5f5ae6e41e
commit
f8750f3515
|
@ -20,6 +20,8 @@
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
@ -28,6 +30,17 @@ pub struct Settings {
|
||||||
pub inactive_dir: PathBuf,
|
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> {
|
pub fn env_config() -> Result<Settings> {
|
||||||
// Configures the directories for Vento
|
// Configures the directories for Vento
|
||||||
let home = match dirs::home_dir() {
|
let home = match dirs::home_dir() {
|
||||||
|
@ -85,3 +98,24 @@ fn dir_config() -> Result<String> {
|
||||||
|
|
||||||
Ok(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(())
|
||||||
|
}
|
||||||
|
|
12
src/item.rs
12
src/item.rs
|
@ -79,6 +79,12 @@ pub fn take(file: &String, slot: &str) -> Result<()> {
|
||||||
bail!("{}", "No such file or directory.".red());
|
bail!("{}", "No such file or directory.".red());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
common::history(common::HistoryData {
|
||||||
|
path: sourcelocation.clone(),
|
||||||
|
file: String::from(filename),
|
||||||
|
action: common::Action::Take,
|
||||||
|
})?;
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"✅ {} {} {} ",
|
"✅ {} {} {} ",
|
||||||
"Took".green(),
|
"Took".green(),
|
||||||
|
@ -147,6 +153,12 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> {
|
||||||
|
|
||||||
destpath.pop();
|
destpath.pop();
|
||||||
|
|
||||||
|
common::history(common::HistoryData {
|
||||||
|
path: destpath.clone(),
|
||||||
|
file: String::from(file),
|
||||||
|
action: common::Action::Drop,
|
||||||
|
})?;
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"✅ {} {} {} ",
|
"✅ {} {} {} ",
|
||||||
"Dropped".green(),
|
"Dropped".green(),
|
||||||
|
|
Loading…
Reference in New Issue