From 17217173f4b010adcf235e8aeb02b4f4adeb6f85 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Sat, 19 Aug 2023 18:42:09 -0400 Subject: [PATCH] src: Revamp message displaying Errors and messages will now be handled by a single module called message.rs. This module has a new function called append_emoji(), which will prefix emojis to each message. It also adds an enum named EmojiType, which will contain each possible emoji prefix. Lastly, add two new configs: "display_emoji" and "display_colors". "display_colors" is yet to be implemented. --- src/archive.rs | 17 ++++++++++++----- src/bin/drop.rs | 2 +- src/bin/take.rs | 2 +- src/bin/vento.rs | 2 +- src/common.rs | 36 +++++++++++++++++++++++++++++------- src/history.rs | 5 +++-- src/inv.rs | 14 ++++++++------ src/item.rs | 8 +++++--- src/lib.rs | 2 +- src/{error.rs => message.rs} | 24 ++++++++++++++++++++++++ 10 files changed, 85 insertions(+), 27 deletions(-) rename src/{error.rs => message.rs} (79%) diff --git a/src/archive.rs b/src/archive.rs index f8909e2..bab5eec 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -17,7 +17,10 @@ * */ -use crate::common; +use crate::{ + common, + message::{append_emoji, EmojiType} +}; use anyhow::Result; use colored::Colorize; use std::{fs::File, path::PathBuf}; @@ -40,7 +43,8 @@ pub fn export_inv(slot: &str, output: PathBuf, message: bool) -> Result<()> { if message { println!( - "✅ {} {} {} {}", + "{}{} {} {} {}", + append_emoji(EmojiType::Success)?, "Exported".green(), match slot { "a" | "active" => "active".green(), @@ -66,7 +70,8 @@ pub fn export_dir(output: PathBuf, message: bool) -> Result<()> { if message { println!( - "✅ {} {}", + "{}{} {}", + append_emoji(EmojiType::Success)?, "Exported Vento directory into".green(), &output.to_str().unwrap() ); @@ -89,7 +94,8 @@ pub fn import_inv(input: PathBuf, slot: &str, message: bool) -> Result<()> { if message { println!( - "✅ {} {} {} {} {}", + "{}{} {} {} {} {}", + append_emoji(EmojiType::Success)?, "Imported".green(), &input.to_str().unwrap(), "into".green(), @@ -116,7 +122,8 @@ pub fn import_dir(input: PathBuf, message: bool) -> Result<()> { if message { println!( - "✅ {} {} {}", + "{}{} {} {}", + append_emoji(EmojiType::Success)?, "Imported".green(), &input.to_str().unwrap(), "into Vento directory".green(), diff --git a/src/bin/drop.rs b/src/bin/drop.rs index af59138..3f6d1f1 100644 --- a/src/bin/drop.rs +++ b/src/bin/drop.rs @@ -22,7 +22,7 @@ use std::env; use std::path::Path; use vento::{ common::get_current_dir, - error::{throw_error, ErrorType}, + message::{throw_error, ErrorType}, help, item, }; diff --git a/src/bin/take.rs b/src/bin/take.rs index ae0b5a6..e581427 100644 --- a/src/bin/take.rs +++ b/src/bin/take.rs @@ -20,7 +20,7 @@ use anyhow::Result; use std::env; use vento::{ - error::{throw_error, ErrorType}, + message::{throw_error, ErrorType}, help, item, }; diff --git a/src/bin/vento.rs b/src/bin/vento.rs index 7ec5ffb..0576abb 100644 --- a/src/bin/vento.rs +++ b/src/bin/vento.rs @@ -21,7 +21,7 @@ use anyhow::Result; use std::{env, path::PathBuf}; use vento::{ archive, - error::{throw_error, ErrorType}, + message::{throw_error, ErrorType}, help, history, inv, }; diff --git a/src/common.rs b/src/common.rs index ffe7900..09534a4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -17,7 +17,7 @@ * */ -use crate::error::{throw_error, ErrorType}; +use crate::message::{throw_error, ErrorType}; use anyhow::Result; use config::Config; use std::env::current_dir; @@ -38,6 +38,12 @@ pub struct HistoryData { pub action: Action, } +pub struct DeserializedConfig { + pub directory: String, + pub display_emoji: bool, + pub display_colors: bool, +} + pub enum Action { Take, Drop, @@ -53,7 +59,7 @@ pub fn env_config() -> Result { if home == PathBuf::new() { throw_error(ErrorType::NoHomeDirectory)?; }; - let custom_dir = Path::new(&dir_config()?).to_path_buf(); + let custom_dir = Path::new(&parse_config()?.directory).to_path_buf(); let vento_dir: PathBuf = if custom_dir != PathBuf::new() { Path::new(&custom_dir).to_path_buf() } else { @@ -74,9 +80,11 @@ pub fn env_config() -> Result { }) } -fn dir_config() -> Result { - // Handles reading the config file or variables for Vento. - let mut result = String::new(); +/// Handles reading the config file or variables for Vento. +pub fn parse_config() -> Result { + let mut directory = String::new(); + let mut display_emoji = true; + let mut display_colors = true; let mut config = match dirs::config_dir() { Option::Some(dir) => dir, _ => PathBuf::new(), @@ -92,14 +100,28 @@ fn dir_config() -> Result { .add_source(config::Environment::with_prefix("VENTO")) .build()?; - result = match settings.get_string("directory") { + directory = match settings.get_string("directory") { Ok(value) => value, Err(_) => String::new(), }; + + display_emoji = match settings.get_bool("display_emoji") { + Ok(value) => value, + Err(_) => true, + }; + + display_colors = match settings.get_bool("display_colors") { + Ok(value) => value, + Err(_) => true, + }; } }; - Ok(result) + Ok(DeserializedConfig { + directory, + display_emoji, + display_colors, + }) } /// Writes an action into the history file diff --git a/src/history.rs b/src/history.rs index 43e9a27..9abccdb 100644 --- a/src/history.rs +++ b/src/history.rs @@ -19,7 +19,7 @@ use crate::{ common, - error::{throw_error, ErrorType}, + message::{append_emoji, throw_error, ErrorType, EmojiType}, inv, item, }; use anyhow::Result; @@ -64,7 +64,8 @@ pub fn undo() -> Result<()> { } println!( - "✅ {}{}{}", + "{}{}{}{}", + append_emoji(EmojiType::Success)?, match contents[3] { "take" => "Take", "drop" => "Drop", diff --git a/src/inv.rs b/src/inv.rs index 1af0ff2..d5e82ae 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -19,7 +19,7 @@ use super::{ common, - error::{throw_error, ErrorType}, + message::{throw_error, append_emoji, ErrorType, EmojiType}, }; use anyhow::{bail, Context, Result}; use colored::Colorize; @@ -35,7 +35,7 @@ pub fn init() -> Result<()> { if ventodir.is_dir() { // Checks if Vento has already been initialized and prompts the user if they want to initialize it again let mut answer = String::new(); - print!("⚠️ {} Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) ", "WARNING:".bold().red()); + print!("{}{} Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) ", append_emoji(EmojiType::Warning)?, "WARNING:".bold().red()); let _ = io::stdout().flush(); io::stdin().read_line(&mut answer)?; match answer.as_str().trim() { @@ -89,7 +89,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> { if fs::read_dir(&slotdir).unwrap().count() == 0 { // Detects if the slot or directory has any contents println!( - "🗃️ {}", + "{}{}", + append_emoji(EmojiType::Inventory)?, format!( "No files in {}{}", match slot { @@ -110,7 +111,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> { ); } else { println!( - "🗃️ {}", + "{}{}", + append_emoji(EmojiType::Inventory)?, format!( "Files in {}{} ({}):", match slot { @@ -187,7 +189,7 @@ pub fn switch(message: bool) -> Result<()> { })?; if message { - println!("✅ {}", "Switched inventory slots!".green()); + println!("{}{}", append_emoji(EmojiType::Success)?, "Switched inventory slots!".green()); } Ok(()) } @@ -200,6 +202,6 @@ fn create_slots() -> Result<()> { fs::create_dir_all(active)?; fs::create_dir_all(inactive)?; - println!("🎉 {}", "Vento has been succesfully initialized!".green()); + println!("{}{}", append_emoji(EmojiType::Celebrate)?, "Vento has been succesfully initialized!".green()); Ok(()) } diff --git a/src/item.rs b/src/item.rs index 2389579..5365f06 100644 --- a/src/item.rs +++ b/src/item.rs @@ -19,7 +19,7 @@ use super::{ common, - error::{throw_error, ErrorType}, + message::{throw_error, append_emoji, ErrorType, EmojiType}, }; use anyhow::{bail, Result}; use colored::Colorize; @@ -87,7 +87,8 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> { if message { println!( - "✅ {} {} {} {} {} {} {}", + "{}{} {} {} {} {} {} {}", + append_emoji(EmojiType::Success)?, "Took".green(), &filename.bold(), "from".green(), @@ -171,7 +172,8 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<( if message { println!( - "✅ {} {} {} {} {} {}", + "{}{} {} {} {} {} {}", + append_emoji(EmojiType::Success)?, "Dropped".green(), &file.bold(), "from".green(), diff --git a/src/lib.rs b/src/lib.rs index e218b03..c7e45ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ pub mod archive; pub mod common; -pub mod error; +pub mod message; pub mod help; pub mod history; pub mod inv; diff --git a/src/error.rs b/src/message.rs similarity index 79% rename from src/error.rs rename to src/message.rs index 1a6593f..7812349 100644 --- a/src/error.rs +++ b/src/message.rs @@ -19,6 +19,7 @@ use anyhow::{bail, Result}; use colored::Colorize; +use crate::common::parse_config; pub enum ErrorType { TooManyArgs, @@ -35,6 +36,29 @@ pub enum ErrorType { NoFileOrDir, } +pub enum EmojiType { + Celebrate, + Success, + Warning, + Inventory, +} + + +pub fn append_emoji(message: EmojiType) -> Result { + let mut output: String = String::new(); + + if parse_config()?.display_emoji { + match message { + EmojiType::Celebrate => output = String::from("🎉 "), + EmojiType::Success => output = String::from("✅ "), + EmojiType::Inventory => output = String::from("🗃️ "), + EmojiType::Warning => output = String::from("⚠️ "), + }; + } + + Ok(output) +} + /// Displays an error and exits pub fn throw_error(error: ErrorType) -> Result<()> { bail!(