Skip to main content

mago_reporting/
color.rs

1/// Choice for colorizing output.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
3pub enum ColorChoice {
4    /// Automatically detect whether colors should be used based on TTY detection.
5    #[default]
6    Auto,
7    /// Always use colors, regardless of TTY status.
8    Always,
9    /// Never use colors.
10    Never,
11}
12
13impl ColorChoice {
14    /// Determine if colors should be used based on this choice and whether output is a TTY.
15    ///
16    /// # Arguments
17    ///
18    /// * `is_tty` - Whether the output stream is connected to a terminal
19    ///
20    /// # Returns
21    ///
22    /// `true` if colors should be used, `false` otherwise
23    #[must_use]
24    pub fn should_use_colors(self, is_tty: bool) -> bool {
25        // Respect NO_COLOR environment variable (https://no-color.org/)
26        // If NO_COLOR exists and is not "0", disable colors
27        if std::env::var_os("NO_COLOR").is_some_and(|value| value != "0") {
28            return false;
29        }
30
31        match self {
32            ColorChoice::Auto => is_tty,
33            ColorChoice::Always => true,
34            ColorChoice::Never => false,
35        }
36    }
37}