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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
pub fn color_choice() -> structopt::clap::AppSettings {
let color = concolor_control::get(concolor_control::Stream::Either);
if color.ansi_color() {
structopt::clap::AppSettings::ColorAlways
} else {
structopt::clap::AppSettings::ColorNever
}
}
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, structopt::StructOpt)]
pub struct Color {
#[structopt(long, default_value = "auto", value_name = "WHEN")]
pub color: ColorChoice,
}
impl Color {
#[cfg(feature = "api_unstable")]
pub fn apply(&self) {
concolor_control::set(self.to_control());
}
pub fn to_control(&self) -> concolor_control::ColorChoice {
match self.color {
ColorChoice::Auto => concolor_control::ColorChoice::Auto,
ColorChoice::Always => concolor_control::ColorChoice::Always,
ColorChoice::Never => concolor_control::ColorChoice::Never,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
impl ColorChoice {
pub const fn choices() -> &'static [Self] {
&[ColorChoice::Auto, ColorChoice::Always, ColorChoice::Never]
}
pub const fn values() -> &'static [&'static str] {
&[AUTO, ALWAYS, NEVER]
}
pub const fn value(self) -> &'static str {
match self {
Self::Auto => AUTO,
Self::Always => ALWAYS,
Self::Never => NEVER,
}
}
}
impl Default for ColorChoice {
fn default() -> Self {
Self::Auto
}
}
impl core::fmt::Display for ColorChoice {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.value().fmt(f)
}
}
impl std::str::FromStr for ColorChoice {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
AUTO => Ok(Self::Auto),
ALWAYS => Ok(Self::Always),
NEVER => Ok(Self::Never),
other => Err(format!("Unknown color choice '{}'", other)),
}
}
}
const AUTO: &str = "auto";
const ALWAYS: &str = "always";
const NEVER: &str = "never";