From c182de4fb5f2a7fe3386e062794f1c131f47d38d Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Sat, 3 Jan 2026 08:46:53 -0300 Subject: [PATCH] refactor: add colors module for terminal output --- crates/plotnik-lib/src/colors.rs | 57 ++++++++++++++++++++++++++++++++ crates/plotnik-lib/src/lib.rs | 2 ++ 2 files changed, 59 insertions(+) create mode 100644 crates/plotnik-lib/src/colors.rs diff --git a/crates/plotnik-lib/src/colors.rs b/crates/plotnik-lib/src/colors.rs new file mode 100644 index 00000000..644f43e3 --- /dev/null +++ b/crates/plotnik-lib/src/colors.rs @@ -0,0 +1,57 @@ +//! ANSI color codes for terminal output. +//! +//! Four semantic colors with orthogonal dim modifier: +//! - Blue: Definition names, keys, type names +//! - Green: String literals, terminal markers +//! - Dim: Structure, nav, effects, metadata +//! - Reset: Return to default + +/// ANSI color palette for CLI output. +/// +/// Designed for jq-inspired colorization that works in both light and dark themes. +/// Uses only standard 16-color ANSI codes (no RGB). +#[derive(Clone, Copy, Debug)] +pub struct Colors { + pub blue: &'static str, + pub green: &'static str, + pub dim: &'static str, + pub reset: &'static str, +} + +impl Default for Colors { + fn default() -> Self { + Self::OFF + } +} + +impl Colors { + /// Colors enabled (ANSI escape codes). + pub const ON: Self = Self { + blue: "\x1b[34m", + green: "\x1b[32m", + dim: "\x1b[2m", + reset: "\x1b[0m", + }; + + /// Colors disabled (empty strings). + pub const OFF: Self = Self { + blue: "", + green: "", + dim: "", + reset: "", + }; + + /// Create colors based on enabled flag. + pub fn new(enabled: bool) -> Self { + if enabled { + Self::ON + } else { + Self::OFF + } + } + + /// Check if colors are enabled. + pub fn is_enabled(&self) -> bool { + !self.blue.is_empty() + } +} diff --git a/crates/plotnik-lib/src/lib.rs b/crates/plotnik-lib/src/lib.rs index 05278d97..cdf99a45 100644 --- a/crates/plotnik-lib/src/lib.rs +++ b/crates/plotnik-lib/src/lib.rs @@ -18,6 +18,7 @@ pub mod analyze; pub mod bytecode; +pub mod colors; pub mod compile; pub mod diagnostics; pub mod emit; @@ -32,6 +33,7 @@ pub mod typegen; /// Fatal errors (like fuel exhaustion) use the outer `Result`. pub type PassResult = std::result::Result<(T, Diagnostics), Error>; +pub use colors::Colors; pub use diagnostics::{Diagnostics, DiagnosticsPrinter, Severity, Span}; pub use query::{Query, QueryBuilder}; pub use query::{SourceId, SourceMap};