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
use std::borrow::Cow;
use crate::style::Styleable;
macro_rules! generate_stylize_trait {
(
$($cname:ident => $color:ident),* $(,)?
;
$($mname:ident => $modifier:ident),* $(,)?
) => {
/// Trait enabling for better style applying to widgets and texts.
///
/// This trait allows using builder methods for more natural styling,
/// such as `.red()`, `.on_blue()` and `.bold()`.
///
/// [`Stylize`] is automatically implemented for types implementing
/// [`Styleable`] trait.
///
/// # Example
///
/// ```rust
/// use termint::prelude::*;
///
/// // Using the styles shorthands
/// let warning = "Important message".red().on_black().bold();
///
/// // You can still set color via setter
/// let dyn_color = Color::Green;
/// let success = "Success".fg(dyn_color).italic();
/// ```
pub trait Stylize: Sized {
/// The type returned by the styling methods.
///
/// Widgets usually have this as `Self`, but it allows string
/// to implement this trait with output as `Span`, for example.
type Output;
/// Sets the foreground color explicitly.
///
/// The `fg` can be any type convertible into `Option<Color>`. If
/// `None` is supplied, it keeps the original foreground color.
fn fg<T>(self, fg: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>;
/// Sets the background color explicitly.
///
/// The `bg` can be any type convertible into `Option<Color>`. If
/// `None` is supplied, the background is transparent.
fn bg<T>(self, bg: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>;
/// Adds a specific modifier to the existing set of modifiers.
fn add_modifier(
self,
modifier: crate::enums::Modifier
) -> Self::Output;
/// Removes a specific modifier if it is currently set.
fn remove_modifier(
self,
modifier: crate::enums::Modifier
) -> Self::Output;
$(
#[doc = concat!(
"Sets the foreground color to [`Color::",
stringify!($color),
"`](crate::enums::Color::",
stringify!($color), ")."
)]
fn $cname(self) -> Self::Output {
self.fg(crate::enums::Color::$color)
}
#[doc = concat!(
"Sets the background color to [`Color::",
stringify!($color),
"`](crate::enums::Color::",
stringify!($color), ")."
)]
paste::paste! {
fn [<on_ $cname>](self) -> Self::Output {
self.bg(crate::enums::Color::$color)
}
}
)*
$(
#[doc = concat!(
"Applies the [`Modifier::",
stringify!($modifier),
"`](crate::enums::Modifier::",
stringify!($modifier),
") modifier."
)]
fn $mname(self) -> Self::Output {
self.add_modifier(crate::enums::Modifier::$modifier)
}
)*
}
impl<W> Stylize for W
where
W: crate::style::Styleable,
{
type Output = W;
fn fg<T>(mut self, color: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>
{
self.style_mut().fg = color.into();
self
}
fn bg<T>(mut self, color: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>
{
self.style_mut().bg = color.into();
self
}
fn add_modifier(
mut self,
modifier: crate::enums::Modifier
) -> Self::Output
{
self.style_mut().modifier.insert(modifier);
self
}
fn remove_modifier(
mut self,
modifier: crate::enums::Modifier
) -> Self::Output
{
self.style_mut().modifier.remove(modifier);
self
}
}
};
}
macro_rules! impl_stylize {
(
$($from:ty => $to:ty),* $(,)?
) => {
$(
impl Stylize for $from {
type Output = $to;
fn fg<T>(self, color: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>
{
let mut widget: $to = self.into();
widget.style_mut().fg = color.into();
widget
}
fn bg<T>(self, color: T) -> Self::Output
where
T: Into<Option<crate::enums::Color>>
{
let mut widget: $to = self.into();
widget.style_mut().bg = color.into();
widget
}
fn add_modifier(
self,
modifier: crate::enums::Modifier
) -> Self::Output
{
let mut widget: $to = self.into();
widget.style_mut().modifier.insert(modifier);
widget
}
fn remove_modifier(
self,
modifier: crate::enums::Modifier
) -> Self::Output
{
let mut widget: $to = self.into();
widget.style_mut().modifier.remove(modifier);
widget
}
}
)*
};
}
generate_stylize_trait! {
black => Black,
dark_red => DarkRed,
dark_green => DarkGreen,
dark_yellow => DarkYellow,
dark_blue => DarkBlue,
dark_magenta => DarkMagenta,
dark_cyan => DarkCyan,
light_gray => LightGray,
gray => Gray,
red => Red,
green => Green,
yellow => Yellow,
blue => Blue,
magenta => Magenta,
cyan => Cyan,
white => White,
;
bold => BOLD,
dim => DIM,
italic => ITALIC,
underline => UNDERLINED,
blink => BLINK,
inverse => INVERSED,
hidden => HIDDEN,
strike => STRIKED,
}
impl_stylize! {
&str => crate::widgets::Span,
String => crate::widgets::Span,
Cow<'_, str> => crate::widgets::Span,
}