1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-11-28 07:25:42 +00:00

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 anyhow::Result;
use colored::Colorize; use colored::Colorize;
use std::{fs::File, path::PathBuf}; use std::{fs::File, path::PathBuf};
@ -40,7 +43,8 @@ pub fn export_inv(slot: &str, output: PathBuf, message: bool) -> Result<()> {
if message { if message {
println!( println!(
"✅ {} {} {} {}", "{}{} {} {} {}",
append_emoji(EmojiType::Success)?,
"Exported".green(), "Exported".green(),
match slot { match slot {
"a" | "active" => "active".green(), "a" | "active" => "active".green(),
@ -66,7 +70,8 @@ pub fn export_dir(output: PathBuf, message: bool) -> Result<()> {
if message { if message {
println!( println!(
"✅ {} {}", "{}{} {}",
append_emoji(EmojiType::Success)?,
"Exported Vento directory into".green(), "Exported Vento directory into".green(),
&output.to_str().unwrap() &output.to_str().unwrap()
); );
@ -89,7 +94,8 @@ pub fn import_inv(input: PathBuf, slot: &str, message: bool) -> Result<()> {
if message { if message {
println!( println!(
"✅ {} {} {} {} {}", "{}{} {} {} {} {}",
append_emoji(EmojiType::Success)?,
"Imported".green(), "Imported".green(),
&input.to_str().unwrap(), &input.to_str().unwrap(),
"into".green(), "into".green(),
@ -116,7 +122,8 @@ pub fn import_dir(input: PathBuf, message: bool) -> Result<()> {
if message { if message {
println!( println!(
"✅ {} {} {}", "{}{} {} {}",
append_emoji(EmojiType::Success)?,
"Imported".green(), "Imported".green(),
&input.to_str().unwrap(), &input.to_str().unwrap(),
"into Vento directory".green(), "into Vento directory".green(),

View file

@ -22,7 +22,7 @@ use std::env;
use std::path::Path; use std::path::Path;
use vento::{ use vento::{
common::get_current_dir, common::get_current_dir,
error::{throw_error, ErrorType}, message::{throw_error, ErrorType},
help, item, help, item,
}; };

View file

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

View file

@ -21,7 +21,7 @@ use anyhow::Result;
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use vento::{ use vento::{
archive, archive,
error::{throw_error, ErrorType}, message::{throw_error, ErrorType},
help, history, inv, 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 anyhow::Result;
use config::Config; use config::Config;
use std::env::current_dir; use std::env::current_dir;
@ -38,6 +38,12 @@ pub struct HistoryData {
pub action: Action, pub action: Action,
} }
pub struct DeserializedConfig {
pub directory: String,
pub display_emoji: bool,
pub display_colors: bool,
}
pub enum Action { pub enum Action {
Take, Take,
Drop, Drop,
@ -53,7 +59,7 @@ pub fn env_config() -> Result<Settings> {
if home == PathBuf::new() { if home == PathBuf::new() {
throw_error(ErrorType::NoHomeDirectory)?; 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() { let vento_dir: PathBuf = if custom_dir != PathBuf::new() {
Path::new(&custom_dir).to_path_buf() Path::new(&custom_dir).to_path_buf()
} else { } 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.
// Handles reading the config file or variables for Vento. pub fn parse_config() -> Result<DeserializedConfig> {
let mut result = String::new(); let mut directory = String::new();
let mut display_emoji = true;
let mut display_colors = true;
let mut config = match dirs::config_dir() { let mut config = match dirs::config_dir() {
Option::Some(dir) => dir, Option::Some(dir) => dir,
_ => PathBuf::new(), _ => PathBuf::new(),
@ -92,14 +100,28 @@ fn dir_config() -> Result<String> {
.add_source(config::Environment::with_prefix("VENTO")) .add_source(config::Environment::with_prefix("VENTO"))
.build()?; .build()?;
result = match settings.get_string("directory") { directory = match settings.get_string("directory") {
Ok(value) => value, Ok(value) => value,
Err(_) => String::new(), 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 /// Writes an action into the history file

View file

@ -19,7 +19,7 @@
use crate::{ use crate::{
common, common,
error::{throw_error, ErrorType}, message::{append_emoji, throw_error, ErrorType, EmojiType},
inv, item, inv, item,
}; };
use anyhow::Result; use anyhow::Result;
@ -64,7 +64,8 @@ pub fn undo() -> Result<()> {
} }
println!( println!(
"✅ {}{}{}", "{}{}{}{}",
append_emoji(EmojiType::Success)?,
match contents[3] { match contents[3] {
"take" => "Take", "take" => "Take",
"drop" => "Drop", "drop" => "Drop",

View file

@ -19,7 +19,7 @@
use super::{ use super::{
common, common,
error::{throw_error, ErrorType}, message::{throw_error, append_emoji, ErrorType, EmojiType},
}; };
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use colored::Colorize; use colored::Colorize;
@ -35,7 +35,7 @@ pub fn init() -> Result<()> {
if ventodir.is_dir() { if ventodir.is_dir() {
// Checks if Vento has already been initialized and prompts the user if they want to initialize it again // Checks if Vento has already been initialized and prompts the user if they want to initialize it again
let mut answer = String::new(); 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(); let _ = io::stdout().flush();
io::stdin().read_line(&mut answer)?; io::stdin().read_line(&mut answer)?;
match answer.as_str().trim() { 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 { if fs::read_dir(&slotdir).unwrap().count() == 0 {
// Detects if the slot or directory has any contents // Detects if the slot or directory has any contents
println!( println!(
"🗃️ {}", "{}{}",
append_emoji(EmojiType::Inventory)?,
format!( format!(
"No files in {}{}", "No files in {}{}",
match slot { match slot {
@ -110,7 +111,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
); );
} else { } else {
println!( println!(
"🗃️ {}", "{}{}",
append_emoji(EmojiType::Inventory)?,
format!( format!(
"Files in {}{} ({}):", "Files in {}{} ({}):",
match slot { match slot {
@ -187,7 +189,7 @@ pub fn switch(message: bool) -> Result<()> {
})?; })?;
if message { if message {
println!("{}", "Switched inventory slots!".green()); println!("{}{}", append_emoji(EmojiType::Success)?, "Switched inventory slots!".green());
} }
Ok(()) Ok(())
} }
@ -200,6 +202,6 @@ fn create_slots() -> Result<()> {
fs::create_dir_all(active)?; fs::create_dir_all(active)?;
fs::create_dir_all(inactive)?; fs::create_dir_all(inactive)?;
println!("🎉 {}", "Vento has been succesfully initialized!".green()); println!("{}{}", append_emoji(EmojiType::Celebrate)?, "Vento has been succesfully initialized!".green());
Ok(()) Ok(())
} }

View file

@ -19,7 +19,7 @@
use super::{ use super::{
common, common,
error::{throw_error, ErrorType}, message::{throw_error, append_emoji, ErrorType, EmojiType},
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use colored::Colorize; use colored::Colorize;
@ -87,7 +87,8 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
if message { if message {
println!( println!(
"✅ {} {} {} {} {} {} {}", "{}{} {} {} {} {} {} {}",
append_emoji(EmojiType::Success)?,
"Took".green(), "Took".green(),
&filename.bold(), &filename.bold(),
"from".green(), "from".green(),
@ -171,7 +172,8 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
if message { if message {
println!( println!(
"✅ {} {} {} {} {} {}", "{}{} {} {} {} {} {}",
append_emoji(EmojiType::Success)?,
"Dropped".green(), "Dropped".green(),
&file.bold(), &file.bold(),
"from".green(), "from".green(),

View file

@ -19,7 +19,7 @@
pub mod archive; pub mod archive;
pub mod common; pub mod common;
pub mod error; pub mod message;
pub mod help; pub mod help;
pub mod history; pub mod history;
pub mod inv; pub mod inv;

View file

@ -19,6 +19,7 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use colored::Colorize; use colored::Colorize;
use crate::common::parse_config;
pub enum ErrorType { pub enum ErrorType {
TooManyArgs, TooManyArgs,
@ -35,6 +36,29 @@ pub enum ErrorType {
NoFileOrDir, 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 /// Displays an error and exits
pub fn throw_error(error: ErrorType) -> Result<()> { pub fn throw_error(error: ErrorType) -> Result<()> {
bail!( bail!(