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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
macro_rules! impl_attribute_methods {
($($(#[$meta:meta])* $set:ident $variant:ident)*) => {
$(
$(#[$meta])*
#[inline]
pub const fn $set(self) -> Self {
self.attributes(Attributes::$variant)
}
)*
};
}
pub(crate) use impl_attribute_methods;
macro_rules! impl_color_methods {
(
colors($($variant:ident)*)
fg($($(#[$fg_meta:meta])* $set_fg:ident)*)
bg($($(#[$bg_meta:meta])* $set_bg:ident)*)
underline($($(#[$underline_meta:meta])* $set_underline:ident)*)
) => {
$(
$(#[$fg_meta])*
#[inline]
pub const fn $set_fg(self) -> Self {
self.fg(Color::Basic(BasicColor::$variant))
}
$(#[$bg_meta])*
#[inline]
pub const fn $set_bg(self) -> Self {
self.bg(Color::Basic(BasicColor::$variant))
}
$(#[$underline_meta])*
#[inline]
pub const fn $set_underline(self) -> Self {
self.underline_colored(Color::Basic(BasicColor::$variant))
}
)*
};
}
pub(crate) use impl_color_methods;
macro_rules! builder_methods {
($s:ident => $style:expr) => {
/// Set the foreground color.
#[inline]
pub const fn fg(self, color: Color) -> Self {
let mut $s = self;
$style.fg = color;
$s
}
/// Set the background color.
#[inline]
pub const fn bg(self, color: Color) -> Self {
let mut $s = self;
$style.bg = color;
$s
}
/// Set the underline attribute and color.
#[inline]
pub const fn underline_colored(self, color: Color) -> Self {
let mut $s = self;
let style = &mut $style;
style.underline = color;
style.attributes = style.attributes.or(Attributes::UNDERLINED);
$s
}
/// Set the attributes.
///
/// This will not unset any existing attributes.
#[inline]
pub const fn attributes(self, attrs: Attributes) -> Self {
let mut $s = self;
let style = &mut $style;
style.attributes = style.attributes.or(attrs);
$s
}
$crate::builder::impl_attribute_methods!(
/// Set the bold attribute.
bold BOLD
/// Set the dim attribute.
dim DIM
/// Set the italic attribute.
italic ITALIC
/// Set the underline attribute.
underlined UNDERLINED
/// Set the blink attribute.
blink BLINK
/// Set the inverted attribute.
inverted INVERTED
/// Set the hidden attribute.
hidden HIDDEN
/// Set the strikethrough attribute.
strikethrough STRIKETHROUGH
);
$crate::builder::impl_color_methods!(
colors(
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
BrightBlack
BrightRed
BrightGreen
BrightYellow
BrightBlue
BrightMagenta
BrightCyan
BrightWhite
)
fg(
/// Set the foreground color to black.
black
/// Set the foreground color to red.
red
/// Set the foreground color to green.
green
/// Set the foreground color to yellow.
yellow
/// Set the foreground color to blue.
blue
/// Set the foreground color to magenta.
magenta
/// Set the foreground color to cyan.
cyan
/// Set the foreground color to white.
white
/// Set the foreground color to bright black.
bright_black
/// Set the foreground color to bright red.
bright_red
/// Set the foreground color to bright green.
bright_green
/// Set the foreground color to bright yellow.
bright_yellow
/// Set the foreground color to bright blue.
bright_blue
/// Set the foreground color to bright magenta.
bright_magenta
/// Set the foreground color to bright cyan.
bright_cyan
/// Set the foreground color to bright white.
bright_white
)
bg(
/// Set the background color to black.
on_black
/// Set the background color to red.
on_red
/// Set the background color to green.
on_green
/// Set the background color to yellow.
on_yellow
/// Set the background color to blue.
on_blue
/// Set the background color to magenta.
on_magenta
/// Set the background color to cyan.
on_cyan
/// Set the background color to white.
on_white
/// Set the background color to bright black.
on_bright_black
/// Set the background color to bright red.
on_bright_red
/// Set the background color to bright green.
on_bright_green
/// Set the background color to bright yellow.
on_bright_yellow
/// Set the background color to bright blue.
on_bright_blue
/// Set the background color to bright magenta.
on_bright_magenta
/// Set the background color to bright cyan.
on_bright_cyan
/// Set the background color to bright white.
on_bright_white
)
underline(
/// Set the underline attribute, and set the underline color to black.
underline_black
/// Set the underline attribute, and set the underline color to red.
underline_red
/// Set the underline attribute, and set the underline color to green.
underline_green
/// Set the underline attribute, and set the underline color to yellow.
underline_yellow
/// Set the underline attribute, and set the underline color to blue.
underline_blue
/// Set the underline attribute, and set the underline color to magenta.
underline_magenta
/// Set the underline attribute, and set the underline color to cyan.
underline_cyan
/// Set the underline attribute, and set the underline color to white.
underline_white
/// Set the underline attribute, and set the underline color to bright black.
underline_bright_black
/// Set the underline attribute, and set the underline color to bright red.
underline_bright_red
/// Set the underline attribute, and set the underline color to bright green.
underline_bright_green
/// Set the underline attribute, and set the underline color to bright yellow.
underline_bright_yellow
/// Set the underline attribute, and set the underline color to bright blue.
underline_bright_blue
/// Set the underline attribute, and set the underline color to bright magenta.
underline_bright_magenta
/// Set the underline attribute, and set the underline color to bright cyan.
underline_bright_cyan
/// Set the underline attribute, and set the underline color to bright white.
underline_bright_white
)
);
};
}
pub(crate) use builder_methods;