facet_ansi/
lib.rs

1use std::fmt::{self, Debug, Display, Formatter};
2
3// Re-export Style from anstyle
4pub use anstyle::Style;
5
6/// Provides easy access to common styles
7pub mod styles {
8    use super::*;
9
10    /// Get a red style
11    pub fn red() -> Style {
12        Style::new().with_red()
13    }
14
15    /// Get a green style
16    pub fn green() -> Style {
17        Style::new().with_green()
18    }
19
20    /// Get a blue style
21    pub fn blue() -> Style {
22        Style::new().with_blue()
23    }
24
25    /// Get a yellow style
26    pub fn yellow() -> Style {
27        Style::new().with_yellow()
28    }
29
30    /// Get a magenta style
31    pub fn magenta() -> Style {
32        Style::new().with_magenta()
33    }
34
35    /// Get a cyan style
36    pub fn cyan() -> Style {
37        Style::new().with_cyan()
38    }
39
40    /// Get a white style
41    pub fn white() -> Style {
42        Style::new().with_white()
43    }
44
45    /// Get a black style
46    pub fn black() -> Style {
47        Style::new().with_black()
48    }
49
50    /// Get a bright red style
51    pub fn bright_red() -> Style {
52        Style::new().with_bright_red()
53    }
54
55    /// Get a bright green style
56    pub fn bright_green() -> Style {
57        Style::new().with_bright_green()
58    }
59
60    /// Get a bright blue style
61    pub fn bright_blue() -> Style {
62        Style::new().with_bright_blue()
63    }
64
65    /// Get a bright yellow style
66    pub fn bright_yellow() -> Style {
67        Style::new().with_bright_yellow()
68    }
69
70    /// Get a bright magenta style
71    pub fn bright_magenta() -> Style {
72        Style::new().with_bright_magenta()
73    }
74
75    /// Get a bright cyan style
76    pub fn bright_cyan() -> Style {
77        Style::new().with_bright_cyan()
78    }
79
80    /// Get a bright white style
81    pub fn bright_white() -> Style {
82        Style::new().with_bright_white()
83    }
84
85    /// Get a bold style
86    pub fn bold() -> Style {
87        Style::new().bold()
88    }
89
90    /// Get an underlined style
91    pub fn underline() -> Style {
92        Style::new().underline()
93    }
94
95    /// Get a dimmed style
96    pub fn dimmed() -> Style {
97        Style::new().dimmed()
98    }
99}
100
101/// Extensions for creating styles with common colors
102pub trait ColorStyle {
103    /// Create a new style with red foreground color
104    fn with_red(self) -> Style;
105    /// Create a new style with green foreground color
106    fn with_green(self) -> Style;
107    /// Create a new style with blue foreground color
108    fn with_blue(self) -> Style;
109    /// Create a new style with yellow foreground color
110    fn with_yellow(self) -> Style;
111    /// Create a new style with magenta foreground color
112    fn with_magenta(self) -> Style;
113    /// Create a new style with cyan foreground color
114    fn with_cyan(self) -> Style;
115    /// Create a new style with white foreground color
116    fn with_white(self) -> Style;
117    /// Create a new style with black foreground color
118    fn with_black(self) -> Style;
119    /// Create a new style with bright red foreground color
120    fn with_bright_red(self) -> Style;
121    /// Create a new style with bright green foreground color
122    fn with_bright_green(self) -> Style;
123    /// Create a new style with bright blue foreground color
124    fn with_bright_blue(self) -> Style;
125    /// Create a new style with bright yellow foreground color
126    fn with_bright_yellow(self) -> Style;
127    /// Create a new style with bright magenta foreground color
128    fn with_bright_magenta(self) -> Style;
129    /// Create a new style with bright cyan foreground color
130    fn with_bright_cyan(self) -> Style;
131    /// Create a new style with bright white foreground color
132    fn with_bright_white(self) -> Style;
133}
134
135impl ColorStyle for Style {
136    fn with_red(self) -> Style {
137        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
138    }
139
140    fn with_green(self) -> Style {
141        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green)))
142    }
143
144    fn with_blue(self) -> Style {
145        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Blue)))
146    }
147
148    fn with_yellow(self) -> Style {
149        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow)))
150    }
151
152    fn with_magenta(self) -> Style {
153        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Magenta)))
154    }
155
156    fn with_cyan(self) -> Style {
157        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Cyan)))
158    }
159
160    fn with_white(self) -> Style {
161        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::White)))
162    }
163
164    fn with_black(self) -> Style {
165        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Black)))
166    }
167
168    fn with_bright_red(self) -> Style {
169        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightRed)))
170    }
171
172    fn with_bright_green(self) -> Style {
173        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightGreen)))
174    }
175
176    fn with_bright_blue(self) -> Style {
177        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightBlue)))
178    }
179
180    fn with_bright_yellow(self) -> Style {
181        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightYellow)))
182    }
183
184    fn with_bright_magenta(self) -> Style {
185        self.fg_color(Some(anstyle::Color::Ansi(
186            anstyle::AnsiColor::BrightMagenta,
187        )))
188    }
189
190    fn with_bright_cyan(self) -> Style {
191        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightCyan)))
192    }
193
194    fn with_bright_white(self) -> Style {
195        self.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::BrightWhite)))
196    }
197}
198
199/// A struct that wraps a value and its style.
200pub struct Styled<T> {
201    value: T,
202    style: Style,
203}
204
205impl<T: Display> Display for Styled<T> {
206    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
207        if self.style == Style::new() {
208            write!(f, "{}", self.value)
209        } else {
210            // anstyle's Style implements Display which handles all the formatting
211            write!(f, "{}{}{}", self.style, self.value, anstyle::Reset)
212        }
213    }
214}
215
216impl<T: Debug> Debug for Styled<T> {
217    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
218        if self.style == Style::new() {
219            write!(f, "{:?}", self.value)
220        } else {
221            write!(f, "{}{:?}{}", self.style, self.value, anstyle::Reset)
222        }
223    }
224}
225
226/// Extension trait for styling any Display value.
227pub trait Stylize {
228    /// Apply a style to a value.
229    fn style(self, style: Style) -> Styled<Self>
230    where
231        Self: Sized;
232
233    /// Apply red color style to a value.
234    fn red(self) -> Styled<Self>
235    where
236        Self: Sized;
237
238    /// Apply green color style to a value.
239    fn green(self) -> Styled<Self>
240    where
241        Self: Sized;
242
243    /// Apply blue color style to a value.
244    fn blue(self) -> Styled<Self>
245    where
246        Self: Sized;
247
248    /// Apply yellow color style to a value.
249    fn yellow(self) -> Styled<Self>
250    where
251        Self: Sized;
252
253    /// Apply magenta color style to a value.
254    fn magenta(self) -> Styled<Self>
255    where
256        Self: Sized;
257
258    /// Apply cyan color style to a value.
259    fn cyan(self) -> Styled<Self>
260    where
261        Self: Sized;
262
263    /// Apply white color style to a value.
264    fn white(self) -> Styled<Self>
265    where
266        Self: Sized;
267
268    /// Apply black color style to a value.
269    fn black(self) -> Styled<Self>
270    where
271        Self: Sized;
272
273    /// Apply bright red color style to a value.
274    fn bright_red(self) -> Styled<Self>
275    where
276        Self: Sized;
277
278    /// Apply bright green color style to a value.
279    fn bright_green(self) -> Styled<Self>
280    where
281        Self: Sized;
282
283    /// Apply bright blue color style to a value.
284    fn bright_blue(self) -> Styled<Self>
285    where
286        Self: Sized;
287
288    /// Apply bright yellow color style to a value.
289    fn bright_yellow(self) -> Styled<Self>
290    where
291        Self: Sized;
292
293    /// Apply bright magenta color style to a value.
294    fn bright_magenta(self) -> Styled<Self>
295    where
296        Self: Sized;
297
298    /// Apply bright cyan color style to a value.
299    fn bright_cyan(self) -> Styled<Self>
300    where
301        Self: Sized;
302
303    /// Apply bright white color style to a value.
304    fn bright_white(self) -> Styled<Self>
305    where
306        Self: Sized;
307
308    /// Apply bold style to a value.
309    fn bold(self) -> Styled<Self>
310    where
311        Self: Sized;
312
313    /// Apply underline style to a value.
314    fn underline(self) -> Styled<Self>
315    where
316        Self: Sized;
317
318    /// Apply dimmed style to a value.
319    fn dimmed(self) -> Styled<Self>
320    where
321        Self: Sized;
322}
323
324impl<T: Display> Stylize for T {
325    fn style(self, style: Style) -> Styled<Self> {
326        Styled { value: self, style }
327    }
328
329    fn red(self) -> Styled<Self> {
330        self.style(Style::new().with_red())
331    }
332
333    fn green(self) -> Styled<Self> {
334        self.style(Style::new().with_green())
335    }
336
337    fn blue(self) -> Styled<Self> {
338        self.style(Style::new().with_blue())
339    }
340
341    fn yellow(self) -> Styled<Self> {
342        self.style(Style::new().with_yellow())
343    }
344
345    fn magenta(self) -> Styled<Self> {
346        self.style(Style::new().with_magenta())
347    }
348
349    fn cyan(self) -> Styled<Self> {
350        self.style(Style::new().with_cyan())
351    }
352
353    fn white(self) -> Styled<Self> {
354        self.style(Style::new().with_white())
355    }
356
357    fn black(self) -> Styled<Self> {
358        self.style(Style::new().with_black())
359    }
360
361    fn bright_red(self) -> Styled<Self> {
362        self.style(Style::new().with_bright_red())
363    }
364
365    fn bright_green(self) -> Styled<Self> {
366        self.style(Style::new().with_bright_green())
367    }
368
369    fn bright_blue(self) -> Styled<Self> {
370        self.style(Style::new().with_bright_blue())
371    }
372
373    fn bright_yellow(self) -> Styled<Self> {
374        self.style(Style::new().with_bright_yellow())
375    }
376
377    fn bright_magenta(self) -> Styled<Self> {
378        self.style(Style::new().with_bright_magenta())
379    }
380
381    fn bright_cyan(self) -> Styled<Self> {
382        self.style(Style::new().with_bright_cyan())
383    }
384
385    fn bright_white(self) -> Styled<Self> {
386        self.style(Style::new().with_bright_white())
387    }
388
389    fn bold(self) -> Styled<Self> {
390        self.style(Style::new().bold())
391    }
392
393    fn underline(self) -> Styled<Self> {
394        self.style(Style::new().underline())
395    }
396
397    fn dimmed(self) -> Styled<Self> {
398        self.style(Style::new().dimmed())
399    }
400}