owo_colors/
styles.rs

1//! Different display styles (strikethrough, bold, etc.)
2use core::fmt;
3
4#[allow(unused_imports)]
5use crate::OwoColorize;
6
7macro_rules! impl_fmt_for_style {
8    ($(($ty:ident, $trait:path, $ansi:literal)),* $(,)?) => {
9        $(
10            impl<'a, T: $trait> $trait for $ty<'a, T> {
11                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12                    f.write_str($ansi)?;
13                    <_ as $trait>::fmt(&self.0, f)?;
14                    f.write_str("\x1b[0m")
15                }
16            }
17        )*
18    };
19}
20
21/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
22/// with the addition of boldening it. Recommended to be constructed using
23/// [`OwoColorize`](OwoColorize::bold).
24#[repr(transparent)]
25pub struct BoldDisplay<'a, T>(pub &'a T);
26
27/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
28/// with the addition of dimming it. Recommended to be constructed using
29/// [`OwoColorize`](OwoColorize::dimmed).
30#[repr(transparent)]
31pub struct DimDisplay<'a, T>(pub &'a T);
32
33/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
34/// with the addition of italicizing it. Recommended to be constructed using
35/// [`OwoColorize`](OwoColorize::italic).
36#[repr(transparent)]
37pub struct ItalicDisplay<'a, T>(pub &'a T);
38
39/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
40/// with the addition of underlining it. Recommended to be constructed using
41/// [`OwoColorize`](OwoColorize::underline).
42#[repr(transparent)]
43pub struct UnderlineDisplay<'a, T>(pub &'a T);
44
45/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
46/// with the addition of making it blink. Recommended to be constructed using
47/// [`OwoColorize`](OwoColorize::blink).
48#[repr(transparent)]
49pub struct BlinkDisplay<'a, T>(pub &'a T);
50
51/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
52/// with the addition of making it blink fast. Recommended to be constructed using
53/// [`OwoColorize`](OwoColorize::blink_fast).
54#[repr(transparent)]
55pub struct BlinkFastDisplay<'a, T>(pub &'a T);
56
57/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
58/// with the addition of swapping foreground and background colors. Recommended to be constructed
59/// using [`OwoColorize`](OwoColorize::reversed).
60#[repr(transparent)]
61pub struct ReversedDisplay<'a, T>(pub &'a T);
62
63/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
64/// with the addition of hiding the text. Recommended to be constructed
65/// using [`OwoColorize`](OwoColorize::reversed).
66#[repr(transparent)]
67pub struct HiddenDisplay<'a, T>(pub &'a T);
68
69/// Transparent wrapper around a type which implements all the formatters the wrapped type does,
70/// with the addition of crossing out the given text. Recommended to be constructed using
71/// [`OwoColorize`](OwoColorize::strikethrough).
72#[repr(transparent)]
73pub struct StrikeThroughDisplay<'a, T>(pub &'a T);
74
75impl_fmt_for_style! {
76    // Bold
77    (BoldDisplay, fmt::Display,  "\x1b[1m"),
78    (BoldDisplay, fmt::Debug,    "\x1b[1m"),
79    (BoldDisplay, fmt::UpperHex, "\x1b[1m"),
80    (BoldDisplay, fmt::LowerHex, "\x1b[1m"),
81    (BoldDisplay, fmt::Binary,   "\x1b[1m"),
82    (BoldDisplay, fmt::UpperExp, "\x1b[1m"),
83    (BoldDisplay, fmt::LowerExp, "\x1b[1m"),
84    (BoldDisplay, fmt::Octal,    "\x1b[1m"),
85    (BoldDisplay, fmt::Pointer,  "\x1b[1m"),
86
87    // Dim
88    (DimDisplay, fmt::Display,  "\x1b[2m"),
89    (DimDisplay, fmt::Debug,    "\x1b[2m"),
90    (DimDisplay, fmt::UpperHex, "\x1b[2m"),
91    (DimDisplay, fmt::LowerHex, "\x1b[2m"),
92    (DimDisplay, fmt::Binary,   "\x1b[2m"),
93    (DimDisplay, fmt::UpperExp, "\x1b[2m"),
94    (DimDisplay, fmt::LowerExp, "\x1b[2m"),
95    (DimDisplay, fmt::Octal,    "\x1b[2m"),
96    (DimDisplay, fmt::Pointer,  "\x1b[2m"),
97
98    // Italic
99    (ItalicDisplay, fmt::Display,  "\x1b[3m"),
100    (ItalicDisplay, fmt::Debug,    "\x1b[3m"),
101    (ItalicDisplay, fmt::UpperHex, "\x1b[3m"),
102    (ItalicDisplay, fmt::LowerHex, "\x1b[3m"),
103    (ItalicDisplay, fmt::Binary,   "\x1b[3m"),
104    (ItalicDisplay, fmt::UpperExp, "\x1b[3m"),
105    (ItalicDisplay, fmt::LowerExp, "\x1b[3m"),
106    (ItalicDisplay, fmt::Octal,    "\x1b[3m"),
107    (ItalicDisplay, fmt::Pointer,  "\x1b[3m"),
108
109    // Underline
110    (UnderlineDisplay, fmt::Display,  "\x1b[4m"),
111    (UnderlineDisplay, fmt::Debug,    "\x1b[4m"),
112    (UnderlineDisplay, fmt::UpperHex, "\x1b[4m"),
113    (UnderlineDisplay, fmt::LowerHex, "\x1b[4m"),
114    (UnderlineDisplay, fmt::Binary,   "\x1b[4m"),
115    (UnderlineDisplay, fmt::UpperExp, "\x1b[4m"),
116    (UnderlineDisplay, fmt::LowerExp, "\x1b[4m"),
117    (UnderlineDisplay, fmt::Octal,    "\x1b[4m"),
118    (UnderlineDisplay, fmt::Pointer,  "\x1b[4m"),
119
120    // Blink
121    (BlinkDisplay, fmt::Display,  "\x1b[5m"),
122    (BlinkDisplay, fmt::Debug,    "\x1b[5m"),
123    (BlinkDisplay, fmt::UpperHex, "\x1b[5m"),
124    (BlinkDisplay, fmt::LowerHex, "\x1b[5m"),
125    (BlinkDisplay, fmt::Binary,   "\x1b[5m"),
126    (BlinkDisplay, fmt::UpperExp, "\x1b[5m"),
127    (BlinkDisplay, fmt::LowerExp, "\x1b[5m"),
128    (BlinkDisplay, fmt::Octal,    "\x1b[5m"),
129    (BlinkDisplay, fmt::Pointer,  "\x1b[5m"),
130
131    // Blink fast
132    (BlinkFastDisplay, fmt::Display,  "\x1b[6m"),
133    (BlinkFastDisplay, fmt::Debug,    "\x1b[6m"),
134    (BlinkFastDisplay, fmt::UpperHex, "\x1b[6m"),
135    (BlinkFastDisplay, fmt::LowerHex, "\x1b[6m"),
136    (BlinkFastDisplay, fmt::Binary,   "\x1b[6m"),
137    (BlinkFastDisplay, fmt::UpperExp, "\x1b[6m"),
138    (BlinkFastDisplay, fmt::LowerExp, "\x1b[6m"),
139    (BlinkFastDisplay, fmt::Octal,    "\x1b[6m"),
140    (BlinkFastDisplay, fmt::Pointer,  "\x1b[6m"),
141
142    // Reverse video
143    (ReversedDisplay, fmt::Display,  "\x1b[7m"),
144    (ReversedDisplay, fmt::Debug,    "\x1b[7m"),
145    (ReversedDisplay, fmt::UpperHex, "\x1b[7m"),
146    (ReversedDisplay, fmt::LowerHex, "\x1b[7m"),
147    (ReversedDisplay, fmt::Binary,   "\x1b[7m"),
148    (ReversedDisplay, fmt::UpperExp, "\x1b[7m"),
149    (ReversedDisplay, fmt::LowerExp, "\x1b[7m"),
150    (ReversedDisplay, fmt::Octal,    "\x1b[7m"),
151    (ReversedDisplay, fmt::Pointer,  "\x1b[7m"),
152
153    // Hide the text
154    (HiddenDisplay, fmt::Display,  "\x1b[8m"),
155    (HiddenDisplay, fmt::Debug,    "\x1b[8m"),
156    (HiddenDisplay, fmt::UpperHex, "\x1b[8m"),
157    (HiddenDisplay, fmt::LowerHex, "\x1b[8m"),
158    (HiddenDisplay, fmt::Binary,   "\x1b[8m"),
159    (HiddenDisplay, fmt::UpperExp, "\x1b[8m"),
160    (HiddenDisplay, fmt::LowerExp, "\x1b[8m"),
161    (HiddenDisplay, fmt::Octal,    "\x1b[8m"),
162    (HiddenDisplay, fmt::Pointer,  "\x1b[8m"),
163
164    // StrikeThrough
165    (StrikeThroughDisplay, fmt::Display,  "\x1b[9m"),
166    (StrikeThroughDisplay, fmt::Debug,    "\x1b[9m"),
167    (StrikeThroughDisplay, fmt::UpperHex, "\x1b[9m"),
168    (StrikeThroughDisplay, fmt::LowerHex, "\x1b[9m"),
169    (StrikeThroughDisplay, fmt::Binary,   "\x1b[9m"),
170    (StrikeThroughDisplay, fmt::UpperExp, "\x1b[9m"),
171    (StrikeThroughDisplay, fmt::LowerExp, "\x1b[9m"),
172    (StrikeThroughDisplay, fmt::Octal,    "\x1b[9m"),
173    (StrikeThroughDisplay, fmt::Pointer,  "\x1b[9m"),
174}