]> git.proxmox.com Git - rustc.git/blame - vendor/clap_builder/src/util/color.rs
Merge 1.70 into proxmox/bookworm
[rustc.git] / vendor / clap_builder / src / util / color.rs
CommitLineData
9c376795
FG
1use crate::builder::PossibleValue;
2use crate::derive::ValueEnum;
3
4/// Represents the color preferences for program output
5#[derive(Debug, Copy, Clone, Eq, PartialEq)]
6pub enum ColorChoice {
7 /// Enables colored output only when the output is going to a terminal or TTY.
8 ///
9 /// **NOTE:** This is the default behavior of `clap`.
10 ///
11 /// # Platform Specific
12 ///
13 /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms).
14 ///
15 /// # Examples
16 ///
353b0b11
FG
17 /// ```rust
18 /// # #[cfg(feature = "color")] {
19 /// # use clap_builder as clap;
9c376795
FG
20 /// # use clap::{Command, ColorChoice};
21 /// Command::new("myprog")
22 /// .color(ColorChoice::Auto)
23 /// .get_matches();
353b0b11 24 /// # }
9c376795
FG
25 /// ```
26 Auto,
27
28 /// Enables colored output regardless of whether or not the output is going to a terminal/TTY.
29 ///
30 /// # Platform Specific
31 ///
32 /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms).
33 ///
34 /// # Examples
35 ///
353b0b11
FG
36 /// ```rust
37 /// # #[cfg(feature = "color")] {
38 /// # use clap_builder as clap;
9c376795
FG
39 /// # use clap::{Command, ColorChoice};
40 /// Command::new("myprog")
41 /// .color(ColorChoice::Always)
42 /// .get_matches();
353b0b11 43 /// # }
9c376795
FG
44 /// ```
45 Always,
46
47 /// Disables colored output no matter if the output is going to a terminal/TTY, or not.
48 ///
49 /// # Platform Specific
50 ///
51 /// This setting only applies to Unix, Linux, and macOS (i.e. non-Windows platforms)
52 ///
53 /// # Examples
54 ///
353b0b11
FG
55 /// ```rust
56 /// # #[cfg(feature = "color")] {
57 /// # use clap_builder as clap;
9c376795
FG
58 /// # use clap::{Command, ColorChoice};
59 /// Command::new("myprog")
60 /// .color(ColorChoice::Never)
61 /// .get_matches();
353b0b11 62 /// # }
9c376795
FG
63 /// ```
64 Never,
65}
66
67impl Default for ColorChoice {
68 fn default() -> Self {
69 Self::Auto
70 }
71}
72
73impl std::fmt::Display for ColorChoice {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 self.to_possible_value()
76 .expect("no values are skipped")
77 .get_name()
78 .fmt(f)
79 }
80}
81
82impl std::str::FromStr for ColorChoice {
83 type Err = String;
84
85 fn from_str(s: &str) -> Result<Self, Self::Err> {
86 for variant in Self::value_variants() {
87 if variant.to_possible_value().unwrap().matches(s, false) {
88 return Ok(*variant);
89 }
90 }
353b0b11 91 Err(format!("invalid variant: {s}"))
9c376795
FG
92 }
93}
94
95impl ValueEnum for ColorChoice {
96 fn value_variants<'a>() -> &'a [Self] {
97 &[Self::Auto, Self::Always, Self::Never]
98 }
99
100 fn to_possible_value(&self) -> Option<PossibleValue> {
101 Some(match self {
102 Self::Auto => {
103 PossibleValue::new("auto").help("Use colored output if writing to a terminal/TTY")
104 }
105 Self::Always => PossibleValue::new("always").help("Always use colored output"),
106 Self::Never => PossibleValue::new("never").help("Never use colored output"),
107 })
108 }
109}