From b04c0c209d927ba3419469024142c3089d42332d Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 22 Apr 2024 12:52:54 +0200 Subject: [PATCH] Start implementing fmt module in lune-utils --- Cargo.lock | 1 + crates/lune-utils/Cargo.toml | 1 + crates/lune-utils/src/fmt/label.rs | 48 ++++++++++++++++++++++++++++++ crates/lune-utils/src/fmt/mod.rs | 3 ++ crates/lune-utils/src/lib.rs | 1 + 5 files changed, 54 insertions(+) create mode 100644 crates/lune-utils/src/fmt/label.rs create mode 100644 crates/lune-utils/src/fmt/mod.rs diff --git a/Cargo.lock b/Cargo.lock index ca37517..c09a251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1646,6 +1646,7 @@ dependencies = [ name = "lune-utils" version = "0.1.0" dependencies = [ + "console", "dunce", "mlua", "once_cell", diff --git a/crates/lune-utils/Cargo.toml b/crates/lune-utils/Cargo.toml index 0b46d30..27eb317 100644 --- a/crates/lune-utils/Cargo.toml +++ b/crates/lune-utils/Cargo.toml @@ -15,6 +15,7 @@ mlua = { version = "0.9.7", features = ["async"] } tokio = { version = "1", default-features = false, features = ["fs"] } +console = "0.15" dunce = "1.0" once_cell = "1.17" path-clean = "1.0" diff --git a/crates/lune-utils/src/fmt/label.rs b/crates/lune-utils/src/fmt/label.rs new file mode 100644 index 0000000..aa5d84a --- /dev/null +++ b/crates/lune-utils/src/fmt/label.rs @@ -0,0 +1,48 @@ +use std::fmt; + +use console::{style, Color}; + +#[derive(Debug, Clone, Copy)] +pub enum Label { + Info, + Warn, + Error, +} + +impl Label { + /** + Returns the name of the label in lowercase. + */ + #[must_use] + pub fn name(&self) -> &str { + match self { + Self::Info => "info", + Self::Warn => "warn", + Self::Error => "error", + } + } + + /** + Returns the color of the label. + */ + #[must_use] + pub fn color(&self) -> Color { + match self { + Self::Info => Color::Blue, + Self::Warn => Color::Yellow, + Self::Error => Color::Red, + } + } +} + +impl fmt::Display for Label { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}{}{}", + style("[").dim(), + style(self.name().to_ascii_uppercase()).fg(self.color()), + style("]").dim() + ) + } +} diff --git a/crates/lune-utils/src/fmt/mod.rs b/crates/lune-utils/src/fmt/mod.rs new file mode 100644 index 0000000..340cf5b --- /dev/null +++ b/crates/lune-utils/src/fmt/mod.rs @@ -0,0 +1,3 @@ +mod label; + +pub use self::label::Label; diff --git a/crates/lune-utils/src/lib.rs b/crates/lune-utils/src/lib.rs index 4dd2ed9..3357fe1 100644 --- a/crates/lune-utils/src/lib.rs +++ b/crates/lune-utils/src/lib.rs @@ -4,6 +4,7 @@ mod luaurc; mod table_builder; mod version_string; +pub mod fmt; pub mod path; pub use self::luaurc::LuauRc;