1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2024-11-21 14:23:05 +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",
"fs_extra",
"man",
"serde",
"size_format",
"tar",
"xz2",

View file

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

View file

@ -40,11 +40,11 @@ struct Cli {
fn main() -> Result<()> {
// Handles args in Drop
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 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(())
}

View file

@ -38,8 +38,8 @@ fn main() -> Result<()> {
// Handles args in Vento
override_color()?;
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(())
}

View file

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

View file

@ -18,7 +18,8 @@
*/
use crate::{
common, inv, item,
common::{env_config, parse_config},
inv, item,
message::{append_emoji, throw_error, EmojiType, ErrorType},
};
use anyhow::Result;
@ -28,10 +29,7 @@ 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(),
]
let lastpath: PathBuf = [env_config()?.vento_dir, Path::new("last").to_path_buf()]
.iter()
.collect();
@ -50,11 +48,17 @@ pub fn undo() -> Result<()> {
match contents[3] {
"take" => {
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" => {
let path = vec![contents[0], contents[1]].join("/");
item::take(&path, contents[2], false)?;
item::take(&path, contents[2], false, false)?;
}
"switch" => {
inv::switch(false)?;
@ -78,8 +82,11 @@ pub fn undo() -> Result<()> {
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
contents[0],
", ".green(),
match parse_config()?.history_display_dir {
true => format!("{} {} ", "from".green(), contents[0],),
_ => String::new(),
},
"to ".green(),
match contents[2] {
"active" => contents[2].green(),
@ -100,8 +107,11 @@ pub fn undo() -> Result<()> {
_ => contents[2].red(),
}
.bold(),
" slot to ".green(),
contents[0],
" slot".green(),
match parse_config()?.history_display_dir {
true => format!(" {} {}", "to".green(), contents[0],),
false => String::new(),
},
")".green(),
),
_ => String::from(""),

View file

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