1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// Whether command output should use colors.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum ColorChoice {
/// Enables colored output only when the output is going to a terminal or TTY with support.
Auto,
/// Enables colored output regardless of the detected environment.
Always,
/// Disables colored output.
Never,
}
impl ColorChoice {
/// Return the command-line representation of this color choice.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::Always => "always",
Self::Never => "never",
}
}
/// Combine self (higher priority) with an [`anstream::ColorChoice`] (lower priority).
///
/// This method allows prioritizing the user choice, while using the inferred choice for a
/// stream as default.
#[must_use]
pub fn and_colorchoice(self, next: anstream::ColorChoice) -> Self {
match self {
Self::Auto => match next {
anstream::ColorChoice::Auto => Self::Auto,
anstream::ColorChoice::Always | anstream::ColorChoice::AlwaysAnsi => Self::Always,
anstream::ColorChoice::Never => Self::Never,
},
Self::Always | Self::Never => self,
}
}
}
impl From<ColorChoice> for anstream::ColorChoice {
fn from(value: ColorChoice) -> Self {
match value {
ColorChoice::Auto => Self::Auto,
ColorChoice::Always => Self::Always,
ColorChoice::Never => Self::Never,
}
}
}