1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2024-11-21 22:33:01 +00:00

Compare commits

..

4 commits

Author SHA1 Message Date
Lux Aliaga e9aa1b2ee1 common: Add Item hierarchy in config
To avoid confusion, this moves the config "display_dir" to a new
section named "Item". For now this is the only config this new section
will have.
2023-08-26 11:24:15 -04:00
Lux Aliaga 1506dc811c item: Don't display slot if not stated explicitly
It is implied that if you're working without the slot argument you are
working with the active slot, therefore it's not needed for the message
to output that.
2023-08-26 10:54:53 -04:00
Lux Aliaga 63777e6f61 history: Add config to disable displaying dirs
This option is separate from the item actions, since it may be
important for some users to still see where the actions are being
undone. Therefore, I've added this option to a new hierarchy named
"history", which is why I've imported serde into the crate.
2023-08-26 10:51:20 -04:00
Lux Aliaga 2ab970c371 item: Add display_dir config
This option allows the user to prevent showing the path where the file
was taken/dropped from. Next up would be implementing this in history
changes.
2023-08-23 08:48:26 -04:00
7 changed files with 114 additions and 52 deletions

1
Cargo.lock generated
View file

@ -842,6 +842,7 @@ dependencies = [
"dirs", "dirs",
"fs_extra", "fs_extra",
"man", "man",
"serde",
"size_format", "size_format",
"tar", "tar",
"xz2", "xz2",

View file

@ -25,6 +25,7 @@ config = "0.13"
xz2 = "0.1" xz2 = "0.1"
tar = "0.4" tar = "0.4"
clap = { version = "4.3.23", features = ["derive"] } clap = { version = "4.3.23", features = ["derive"] }
serde = "1.0"
[build-dependencies] [build-dependencies]
man = "0.3.0" man = "0.3.0"

View file

@ -40,11 +40,11 @@ struct Cli {
fn main() -> Result<()> { fn main() -> Result<()> {
// Handles args in Drop // Handles args in Drop
let cli = Cli::parse(); let cli = Cli::parse();
let unwrapped_slot = cli.slot.unwrap_or(String::from("active")); let unwrapped_slot = cli.slot.clone().unwrap_or(String::from("active"));
let slot = unwrapped_slot.as_str(); let slot = unwrapped_slot.as_str();
let out = cli.output.unwrap_or(get_current_dir()?); let out = cli.output.unwrap_or(get_current_dir()?);
item::drop(&cli.file, slot, out, true)?; item::drop(&cli.file, slot, out, true, cli.slot.is_some())?;
Ok(()) Ok(())
} }

View file

@ -38,8 +38,8 @@ fn main() -> Result<()> {
// Handles args in Vento // Handles args in Vento
override_color()?; override_color()?;
let cli = Cli::parse(); let cli = Cli::parse();
let slot = cli.slot.unwrap_or(String::from("active")); let slot = cli.slot.clone().unwrap_or(String::from("active"));
item::take(&cli.file, &slot, true)?; item::take(&cli.file, &slot, true, cli.slot.is_some())?;
Ok(()) Ok(())
} }

View file

@ -21,6 +21,7 @@ use crate::message::{throw_error, ErrorType};
use anyhow::Result; use anyhow::Result;
use colored::control::set_override; use colored::control::set_override;
use config::Config; use config::Config;
use serde::Deserialize;
use std::env::current_dir; use std::env::current_dir;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
@ -41,10 +42,24 @@ pub struct HistoryData {
pub struct DeserializedConfig { pub struct DeserializedConfig {
pub directory: String, pub directory: String,
pub display_dir: bool,
pub history_display_dir: bool,
pub display_emoji: bool, pub display_emoji: bool,
pub display_colors: bool, pub display_colors: bool,
} }
#[derive(Debug, Deserialize)]
#[allow(unused)]
struct Item {
display_dir: bool,
}
#[derive(Debug, Deserialize)]
#[allow(unused)]
struct History {
display_dir: bool,
}
pub enum Action { pub enum Action {
Take, Take,
Drop, Drop,
@ -84,6 +99,8 @@ pub fn env_config() -> Result<Settings> {
/// Handles reading the config file or variables for Vento. /// Handles reading the config file or variables for Vento.
pub fn parse_config() -> Result<DeserializedConfig> { pub fn parse_config() -> Result<DeserializedConfig> {
let mut directory = String::new(); let mut directory = String::new();
let mut display_dir = true;
let mut history_display_dir = true;
let mut display_emoji = true; let mut display_emoji = true;
let mut display_colors = true; let mut display_colors = true;
let mut config = match dirs::config_dir() { let mut config = match dirs::config_dir() {
@ -106,6 +123,8 @@ pub fn parse_config() -> Result<DeserializedConfig> {
Err(_) => String::new(), Err(_) => String::new(),
}; };
display_dir = settings.get_bool("item.display_dir").unwrap_or(true);
history_display_dir = settings.get_bool("history.display_dir").unwrap_or(true);
display_emoji = settings.get_bool("display_emoji").unwrap_or(true); display_emoji = settings.get_bool("display_emoji").unwrap_or(true);
display_colors = settings.get_bool("display_colors").unwrap_or(true); display_colors = settings.get_bool("display_colors").unwrap_or(true);
} }
@ -113,6 +132,8 @@ pub fn parse_config() -> Result<DeserializedConfig> {
Ok(DeserializedConfig { Ok(DeserializedConfig {
directory, directory,
display_dir,
history_display_dir,
display_emoji, display_emoji,
display_colors, display_colors,
}) })

View file

@ -18,7 +18,8 @@
*/ */
use crate::{ use crate::{
common, inv, item, common::{env_config, parse_config},
inv, item,
message::{append_emoji, throw_error, EmojiType, ErrorType}, message::{append_emoji, throw_error, EmojiType, ErrorType},
}; };
use anyhow::Result; use anyhow::Result;
@ -28,12 +29,9 @@ use std::path::{Path, PathBuf};
/// Undoes the last action made by Vento using the history file located on the Vento directory /// Undoes the last action made by Vento using the history file located on the Vento directory
pub fn undo() -> Result<()> { pub fn undo() -> Result<()> {
let lastpath: PathBuf = [ let lastpath: PathBuf = [env_config()?.vento_dir, Path::new("last").to_path_buf()]
common::env_config()?.vento_dir, .iter()
Path::new("last").to_path_buf(), .collect();
]
.iter()
.collect();
let lastfile = fs::read_to_string(lastpath)?; let lastfile = fs::read_to_string(lastpath)?;
@ -50,11 +48,17 @@ pub fn undo() -> Result<()> {
match contents[3] { match contents[3] {
"take" => { "take" => {
let destpath = Path::new(contents[0]).to_path_buf(); let destpath = Path::new(contents[0]).to_path_buf();
item::drop(&String::from(contents[1]), contents[2], destpath, false)?; item::drop(
&String::from(contents[1]),
contents[2],
destpath,
false,
false,
)?;
} }
"drop" => { "drop" => {
let path = vec![contents[0], contents[1]].join("/"); let path = vec![contents[0], contents[1]].join("/");
item::take(&path, contents[2], false)?; item::take(&path, contents[2], false, false)?;
} }
"switch" => { "switch" => {
inv::switch(false)?; inv::switch(false)?;
@ -78,9 +82,12 @@ pub fn undo() -> Result<()> {
"{}{}{}{}{}{}{}", "{}{}{}{}{}{}{}",
" (".green(), " (".green(),
contents[1].bold(), contents[1].bold(),
", from ".green(), ", ".green(),
contents[0], match parse_config()?.history_display_dir {
" to ".green(), true => format!("{} {} ", "from".green(), contents[0],),
_ => String::new(),
},
"to ".green(),
match contents[2] { match contents[2] {
"active" => contents[2].green(), "active" => contents[2].green(),
"inactive" => contents[2].blue(), "inactive" => contents[2].blue(),
@ -100,8 +107,11 @@ pub fn undo() -> Result<()> {
_ => contents[2].red(), _ => contents[2].red(),
} }
.bold(), .bold(),
" slot to ".green(), " slot".green(),
contents[0], match parse_config()?.history_display_dir {
true => format!(" {} {}", "to".green(), contents[0],),
false => String::new(),
},
")".green(), ")".green(),
), ),
_ => String::from(""), _ => String::from(""),

View file

@ -18,7 +18,7 @@
*/ */
use super::{ use super::{
common, common::{env_config, history, parse_config, Action, HistoryData},
message::{append_emoji, throw_error, EmojiType, ErrorType}, message::{append_emoji, throw_error, EmojiType, ErrorType},
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
@ -28,16 +28,16 @@ use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
/// Takes a file or directory and stores it in an inventory slot /// Takes a file or directory and stores it in an inventory slot
pub fn take(file: &String, slot: &str, message: bool) -> Result<()> { pub fn take(file: &String, slot: &str, message: bool, display_slot: bool) -> Result<()> {
let ventodir = &common::env_config()?.vento_dir; let ventodir = &env_config()?.vento_dir;
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so // Detects if Vento hasn't been initialized and bails if so
throw_error(ErrorType::NotInitialized)?; throw_error(ErrorType::NotInitialized)?;
}; };
let slotdir: PathBuf = match slot { let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?.active_dir, "active" | "a" => env_config()?.active_dir,
"inactive" | "i" => common::env_config()?.inactive_dir, "inactive" | "i" => env_config()?.inactive_dir,
_ => PathBuf::new(), _ => PathBuf::new(),
}; };
@ -78,29 +78,40 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
throw_error(ErrorType::NoFileOrDir)?; throw_error(ErrorType::NoFileOrDir)?;
} }
common::history(common::HistoryData { history(HistoryData {
path: sourcelocation.clone(), path: sourcelocation.clone(),
file: String::from(filename), file: String::from(filename),
slot: String::from(slot), slot: String::from(slot),
action: common::Action::Take, action: Action::Take,
})?; })?;
if message { if message {
println!( println!(
"{}{} {} {} {} {} {} {}", "{}{} {}{}{}",
append_emoji(EmojiType::Success)?, append_emoji(EmojiType::Success)?,
"Took".green(), "Took".green(),
&filename.bold(), &filename.bold(),
"from".green(), match parse_config()?.display_dir {
&sourcelocation.to_str().unwrap(), true => format! {"{} {} ",
"to".green(), " from".green(),
match slot { &sourcelocation.to_str().unwrap(),
"active" => slot.green(), },
"inactive" => slot.blue(), _ => String::new(),
_ => slot.red(), },
} match display_slot {
.bold(), true => format!(
"slot".green() "{} {} {}",
" to".green(),
match slot {
"active" => slot.green(),
"inactive" => slot.blue(),
_ => slot.red(),
}
.bold(),
"slot".green()
),
_ => String::new(),
},
); );
} }
@ -108,9 +119,15 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
} }
/// Drops a file or directory and stores it in an inventory slot /// Drops a file or directory and stores it in an inventory slot
pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<()> { pub fn drop(
file: &String,
slot: &str,
dest: PathBuf,
message: bool,
display_slot: bool,
) -> Result<()> {
// Drops a file or directory // Drops a file or directory
let ventodir = &common::env_config()?.vento_dir; let ventodir = &env_config()?.vento_dir;
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so // Detects if Vento hasn't been initialized and bails if so
@ -118,8 +135,8 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
}; };
let slotdir: PathBuf = match slot { let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?.active_dir, "active" | "a" => env_config()?.active_dir,
"inactive" | "i" => common::env_config()?.inactive_dir, "inactive" | "i" => env_config()?.inactive_dir,
_ => PathBuf::new(), _ => PathBuf::new(),
}; };
@ -163,28 +180,40 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
destpath.pop(); destpath.pop();
common::history(common::HistoryData { history(HistoryData {
path: destpath.clone(), path: destpath.clone(),
file: String::from(file), file: String::from(file),
slot: String::from(slot), slot: String::from(slot),
action: common::Action::Drop, action: Action::Drop,
})?; })?;
if message { if message {
println!( println!(
"{}{} {} {} {} {} {}", "{}{} {}{}{}",
append_emoji(EmojiType::Success)?, append_emoji(EmojiType::Success)?,
"Dropped".green(), "Dropped".green(),
&file.bold(), &file.bold(),
"from".green(), match display_slot {
match slot { true => format!(
"active" => slot.green(), "{} {} {}",
"inactive" => slot.blue(), " from".green(),
_ => slot.red(), match slot {
} "active" => slot.green(),
.bold(), "inactive" => slot.blue(),
"slot into".green(), _ => slot.red(),
&destpath.to_str().unwrap() }
.bold(),
"slot".green(),
),
false => String::new(),
},
match parse_config()?.display_dir {
true => format! {"{} {} ",
" into".green(),
&destpath.to_str().unwrap(),
},
_ => String::new(),
},
); );
}; };