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.
This commit is contained in:
Lux Aliaga 2023-08-19 18:42:09 -04:00
parent 1e2fb49dac
commit 17217173f4
10 changed files with 85 additions and 27 deletions

View File

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

View File

@ -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,
};

View File

@ -20,7 +20,7 @@
use anyhow::Result;
use std::env;
use vento::{
error::{throw_error, ErrorType},
message::{throw_error, ErrorType},
help, item,
};

View File

@ -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,
};

View File

@ -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<Settings> {
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<Settings> {
})
}
fn dir_config() -> Result<String> {
// 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<DeserializedConfig> {
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<String> {
.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

View File

@ -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",

View File

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

View File

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

View File

@ -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;

View File

@ -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<String> {
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!(