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 FORCE_COLOR environment variable (https://no-color.org/)
26 // If FORCE_COLOR exists and is not "0", force colors
27 if std::env::var_os("FORCE_COLOR").is_some_and(|value| value != "0") {
28 return true;
29 }
30
31 // Respect NO_COLOR environment variable (https://no-color.org/)
32 // If NO_COLOR exists and is not "0", disable colors
33 if std::env::var_os("NO_COLOR").is_some_and(|value| value != "0") {
34 return false;
35 }
36
37 match self {
38 ColorChoice::Auto => is_tty,
39 ColorChoice::Always => true,
40 ColorChoice::Never => false,
41 }
42 }
43}