clap_builder/builder/
styling.rs1pub use anstyle::*;
4
5#[derive(Clone, Debug)]
22#[allow(missing_copy_implementations)] pub struct Styles {
24 header: Style,
25 error: Style,
26 usage: Style,
27 literal: Style,
28 placeholder: Style,
29 valid: Style,
30 invalid: Style,
31 context: Style,
32 context_value: Option<Style>,
33}
34
35impl Styles {
36 pub const fn plain() -> Self {
38 Self {
39 header: Style::new(),
40 error: Style::new(),
41 usage: Style::new(),
42 literal: Style::new(),
43 placeholder: Style::new(),
44 valid: Style::new(),
45 invalid: Style::new(),
46 context: Style::new(),
47 context_value: None,
48 }
49 }
50
51 pub const fn styled() -> Self {
53 #[cfg(feature = "color")]
54 {
55 Self {
56 header: Style::new().bold().underline(),
57 error: Style::new()
58 .fg_color(Some(Color::Ansi(AnsiColor::Red)))
59 .bold(),
60 usage: Style::new().bold().underline(),
61 literal: Style::new().bold(),
62 placeholder: Style::new(),
63 valid: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))),
64 invalid: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))),
65 context: Style::new(),
66 context_value: None,
67 }
68 }
69 #[cfg(not(feature = "color"))]
70 {
71 Self::plain()
72 }
73 }
74
75 #[inline]
77 pub const fn header(mut self, style: Style) -> Self {
78 self.header = style;
79 self
80 }
81
82 #[inline]
84 pub const fn error(mut self, style: Style) -> Self {
85 self.error = style;
86 self
87 }
88
89 #[inline]
91 pub const fn usage(mut self, style: Style) -> Self {
92 self.usage = style;
93 self
94 }
95
96 #[inline]
98 pub const fn literal(mut self, style: Style) -> Self {
99 self.literal = style;
100 self
101 }
102
103 #[inline]
105 pub const fn placeholder(mut self, style: Style) -> Self {
106 self.placeholder = style;
107 self
108 }
109
110 #[inline]
112 pub const fn valid(mut self, style: Style) -> Self {
113 self.valid = style;
114 self
115 }
116
117 #[inline]
119 pub const fn invalid(mut self, style: Style) -> Self {
120 self.invalid = style;
121 self
122 }
123
124 #[inline]
128 pub const fn context(mut self, style: Style) -> Self {
129 self.context = style;
130 self
131 }
132
133 #[inline]
137 pub const fn context_value(mut self, style: Style) -> Self {
138 self.context_value = Some(style);
139 self
140 }
141}
142
143impl Styles {
145 #[inline(always)]
147 pub const fn get_header(&self) -> &Style {
148 &self.header
149 }
150
151 #[inline(always)]
153 pub const fn get_error(&self) -> &Style {
154 &self.error
155 }
156
157 #[inline(always)]
159 pub const fn get_usage(&self) -> &Style {
160 &self.usage
161 }
162
163 #[inline(always)]
165 pub const fn get_literal(&self) -> &Style {
166 &self.literal
167 }
168
169 #[inline(always)]
171 pub const fn get_placeholder(&self) -> &Style {
172 &self.placeholder
173 }
174
175 #[inline(always)]
177 pub const fn get_valid(&self) -> &Style {
178 &self.valid
179 }
180
181 #[inline(always)]
183 pub const fn get_invalid(&self) -> &Style {
184 &self.invalid
185 }
186
187 #[inline(always)]
191 pub const fn get_context(&self) -> &Style {
192 &self.context
193 }
194
195 #[inline(always)]
199 pub const fn get_context_value(&self) -> &Style {
200 match &self.context_value {
201 Some(s) => s,
202 None => &self.context,
203 }
204 }
205}
206
207impl super::AppExt for Styles {}
208
209impl Default for Styles {
210 fn default() -> Self {
211 Self::styled()
212 }
213}
214
215impl Default for &'_ Styles {
216 fn default() -> Self {
217 const STYLES: Styles = Styles::styled();
218 &STYLES
219 }
220}