Skip to main content

error_forge/
console_theme.rs

1//! Console theming for error display in CLI applications.
2//!
3//! This module provides ANSI color formatting for error messages
4//! displayed in terminal environments. It auto-detects terminal
5//! capabilities via [`std::io::IsTerminal`] and disables colors when
6//! stderr is not a TTY, when `TERM=dumb`, or when `NO_COLOR` is set
7//! to a non-empty value (<https://no-color.org/>).
8
9use std::io::IsTerminal;
10
11/// Color theme for console error output.
12///
13/// The fields are `&'static str` ANSI escapes — no allocation per
14/// construction, and `const`-constructible for the two preset
15/// constructors ([`ConsoleTheme::with_colors`], [`ConsoleTheme::plain`]).
16pub struct ConsoleTheme {
17    error_color: &'static str,
18    warning_color: &'static str,
19    info_color: &'static str,
20    success_color: &'static str,
21    caption_color: &'static str,
22    reset: &'static str,
23    bold: &'static str,
24    dim: &'static str,
25}
26
27/// Detect if the current terminal supports ANSI colors.
28fn terminal_supports_ansi() -> bool {
29    // Cache the answer for the process. The decision is based on
30    // env vars + the `stderr` handle, both of which are effectively
31    // process-static, so caching is correct (and removes per-call
32    // env-var reads from the hot path).
33    static SUPPORTS_ANSI: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
34
35    *SUPPORTS_ANSI.get_or_init(|| {
36        // Stderr must be a terminal — applies to every platform.
37        if !std::io::stderr().is_terminal() {
38            return false;
39        }
40
41        // Explicit terminal-dumb signal.
42        if let Ok(term) = std::env::var("TERM") {
43            if term == "dumb" {
44                return false;
45            }
46        }
47
48        if no_color_requested(std::env::var_os("NO_COLOR").as_deref()) {
49            return false;
50        }
51
52        // Default to enabled on every supported platform — modern
53        // Windows builds (10.0.10586+) honour ANSI escapes in stderr.
54        true
55    })
56}
57
58/// Whether a `NO_COLOR` value asks for colour to be disabled.
59///
60/// Per <https://no-color.org/>, colour is disabled when the variable is
61/// present and not an empty string, regardless of its value. An unset or
62/// empty `NO_COLOR` leaves colour on.
63fn no_color_requested(value: Option<&std::ffi::OsStr>) -> bool {
64    value.is_some_and(|v| !v.is_empty())
65}
66
67impl Default for ConsoleTheme {
68    fn default() -> Self {
69        if terminal_supports_ansi() {
70            Self::with_colors()
71        } else {
72            Self::plain()
73        }
74    }
75}
76
77impl ConsoleTheme {
78    /// Create a new theme with default colors. Auto-detects terminal
79    /// color support; falls back to [`Self::plain`] if stderr is not
80    /// a TTY, `TERM=dumb`, or `NO_COLOR` is set to a non-empty value.
81    pub fn new() -> Self {
82        Self::default()
83    }
84
85    /// Create a new theme with colors forced on, regardless of
86    /// terminal detection.
87    pub const fn with_colors() -> Self {
88        Self {
89            error_color: "\x1b[31m",   // Red
90            warning_color: "\x1b[33m", // Yellow
91            info_color: "\x1b[34m",    // Blue
92            success_color: "\x1b[32m", // Green
93            caption_color: "\x1b[36m", // Cyan
94            reset: "\x1b[0m",
95            bold: "\x1b[1m",
96            dim: "\x1b[2m",
97        }
98    }
99
100    /// Create a new theme with no colors (plain text). Useful for
101    /// piping output to a file or non-TTY consumer.
102    pub const fn plain() -> Self {
103        Self {
104            error_color: "",
105            warning_color: "",
106            info_color: "",
107            success_color: "",
108            caption_color: "",
109            reset: "",
110            bold: "",
111            dim: "",
112        }
113    }
114
115    /// Format an error message with the error color.
116    pub fn error(&self, text: &str) -> String {
117        format!("{}{}{}", self.error_color, text, self.reset)
118    }
119
120    /// Format a warning message with the warning color.
121    pub fn warning(&self, text: &str) -> String {
122        format!("{}{}{}", self.warning_color, text, self.reset)
123    }
124
125    /// Format an info message with the info color.
126    pub fn info(&self, text: &str) -> String {
127        format!("{}{}{}", self.info_color, text, self.reset)
128    }
129
130    /// Format a success message with the success color.
131    pub fn success(&self, text: &str) -> String {
132        format!("{}{}{}", self.success_color, text, self.reset)
133    }
134
135    /// Format a caption with the caption color.
136    pub fn caption(&self, text: &str) -> String {
137        format!("{}{}{}", self.caption_color, text, self.reset)
138    }
139
140    /// Format text as bold.
141    pub fn bold(&self, text: &str) -> String {
142        format!("{}{}{}", self.bold, text, self.reset)
143    }
144
145    /// Format text as dim.
146    pub fn dim(&self, text: &str) -> String {
147        format!("{}{}{}", self.dim, text, self.reset)
148    }
149
150    /// Format an error display in a structured way.
151    ///
152    /// Writes the caption, the error's `Display` output, the
153    /// retryability marker, and the error's direct source (if any) into
154    /// one `String`. The colour helpers and `to_string` calls build a
155    /// few short-lived strings along the way, so this allocates several
156    /// times per call; it is meant for reporting, not hot paths.
157    pub fn format_error<E: crate::error::ForgeError>(&self, err: &E) -> String {
158        use std::fmt::Write as _;
159        let mut buf = String::with_capacity(160);
160
161        // Caption — written via the helper formatters so the colour
162        // escapes match the rest of the output.
163        let _ = writeln!(buf, "{}", self.caption(&format!("⚠️  {}", err.caption())));
164
165        // Error message.
166        let _ = writeln!(buf, "{}", self.error(&err.to_string()));
167
168        // Retryable status.
169        let marker = if err.is_retryable() {
170            self.success("Yes")
171        } else {
172            self.error("No")
173        };
174        let _ = writeln!(buf, "{}Retryable: {}{}", self.dim, marker, self.reset);
175
176        // Source error if available.
177        if let Some(source) = err.source() {
178            let _ = writeln!(
179                buf,
180                "{}Caused by: {}{}",
181                self.dim,
182                self.error(&source.to_string()),
183                self.reset
184            );
185        }
186
187        buf
188    }
189}
190
191/// Pretty-print an error to stderr with the default theme.
192///
193/// The default theme is cached process-wide via `OnceLock` — the
194/// terminal-capability check runs at most once regardless of how
195/// many errors are printed.
196pub fn print_error<E: crate::error::ForgeError>(err: &E) {
197    static DEFAULT_THEME: std::sync::OnceLock<ConsoleTheme> = std::sync::OnceLock::new();
198    let theme = DEFAULT_THEME.get_or_init(ConsoleTheme::default);
199    eprintln!("{}", theme.format_error(err));
200}
201
202/// Install a panic hook that formats panics using the ConsoleTheme.
203///
204/// The hook prints a themed `PANIC` caption with the panic message and
205/// location to stderr, then calls the hook that was installed before
206/// it, so an existing crash reporter or logging hook keeps running.
207/// When no custom hook was installed, that previous hook is the
208/// standard library's default, which prints its own panic message
209/// (and the backtrace, when `RUST_BACKTRACE` asks for one) after the
210/// themed lines.
211///
212/// # Example
213///
214/// ```
215/// error_forge::install_panic_hook();
216/// # let _ = std::panic::take_hook();
217/// ```
218pub fn install_panic_hook() {
219    let theme = ConsoleTheme::default();
220    let previous = std::panic::take_hook();
221    std::panic::set_hook(Box::new(move |panic_info| {
222        let message = match panic_info.payload().downcast_ref::<&str>() {
223            Some(s) => *s,
224            None => match panic_info.payload().downcast_ref::<String>() {
225                Some(s) => s.as_str(),
226                None => "Unknown panic",
227            },
228        };
229
230        let location = if let Some(location) = panic_info.location() {
231            format!("at {}:{}", location.file(), location.line())
232        } else {
233            "at unknown location".to_string()
234        };
235
236        eprintln!("{}", theme.caption("💥 PANIC"));
237        eprintln!(
238            "{}",
239            theme.error(&format!("{} {}", message, theme.dim(&location)))
240        );
241
242        previous(panic_info);
243    }));
244}
245
246#[cfg(test)]
247mod tests {
248    use super::no_color_requested;
249    use std::ffi::OsStr;
250
251    #[test]
252    fn test_no_color_follows_spec() {
253        // An empty `NO_COLOR` used to disable colour too, contrary to
254        // no-color.org ("when present and not an empty string").
255        assert!(!no_color_requested(None));
256        assert!(!no_color_requested(Some(OsStr::new(""))));
257        assert!(no_color_requested(Some(OsStr::new("1"))));
258        assert!(no_color_requested(Some(OsStr::new("0"))));
259        assert!(no_color_requested(Some(OsStr::new("false"))));
260    }
261}