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
mod ansi;
pub mod color;
pub mod colorize;
pub mod gradient;
pub mod style;
use ansi::*;
pub use color::*;
pub use colorize::*;
pub use gradient::*;
pub use style::*;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StyledText {
foreground_color_style: ColorStyle,
background_color_style: ColorStyle,
style: Style,
pub text: String,
}
impl StyledText {
#[inline]
pub fn new(text: &str) -> Self {
Self {
foreground_color_style: ColorStyle::Colorless,
background_color_style: ColorStyle::Colorless,
style: Style::new(),
text: text.to_string(),
}
}
#[inline]
pub fn foreground(&self) -> ColorStyle {
self.foreground_color_style
}
#[inline]
pub fn background(&self) -> ColorStyle {
self.background_color_style
}
pub fn styles(&self) -> Vec<StyleMode> {
self.style.styles()
}
#[inline]
fn with_foreground_color<C: Into<ColorStyle>>(&self, color: C) -> Self {
Self {
foreground_color_style: color.into(),
background_color_style: self.background_color_style,
style: self.style,
text: self.text.clone(),
}
}
#[inline]
fn with_background_color<C: Into<ColorStyle>>(&self, color: C) -> Self {
Self {
foreground_color_style: self.foreground_color_style,
background_color_style: color.into(),
style: self.style,
text: self.text.clone(),
}
}
fn with_style_mode(&self, mode: StyleMode) -> Self {
Self {
foreground_color_style: self.foreground_color_style,
background_color_style: self.background_color_style,
style: self.style.add_style_mode(mode),
text: self.text.clone(),
}
}
}
impl From<String> for StyledText {
fn from(s: String) -> Self {
StyledText::new(&s)
}
}
impl From<&str> for StyledText {
fn from(s: &str) -> Self {
StyledText::new(s)
}
}
impl fmt::Display for StyledText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\x1B[{}m", self.style.ansi_code())?;
match (self.foreground_color_style, self.background_color_style) {
(ColorStyle::Single(fg), ColorStyle::Single(bg)) => {
write!(
f,
"\x1B[{};{}m{}\x1B[0m",
fg.ansi_color_code(TargetGround::Foreground),
bg.ansi_color_code(TargetGround::Background),
self.text
)
}
(ColorStyle::Single(fg), ColorStyle::Gradient(bg)) => {
write!(
f,
"\x1B[{}m{}",
fg.ansi_color_code(TargetGround::Foreground),
bg.build(&self.text, TargetGround::Background)
)
}
(ColorStyle::Single(fg), ColorStyle::Colorless) => {
write!(
f,
"\x1B[{}m{}\x1B[0m",
fg.ansi_color_code(TargetGround::Foreground),
self.text
)
}
(ColorStyle::Gradient(fg), ColorStyle::Single(bg)) => {
write!(
f,
"\x1B[{}m{}",
bg.ansi_color_code(TargetGround::Background),
fg.build(&self.text, TargetGround::Foreground)
)
}
(ColorStyle::Gradient(fg), ColorStyle::Gradient(bg)) => {
write!(f, "{}", build_all_gradient_text(&self.text, fg, bg))
}
(ColorStyle::Gradient(fg), ColorStyle::Colorless) => {
write!(f, "{}", fg.build(&self.text, TargetGround::Foreground))
}
(ColorStyle::Colorless, ColorStyle::Single(bg)) => {
write!(
f,
"\x1B[{}m{}\x1B[0m",
bg.ansi_color_code(TargetGround::Background),
self.text
)
}
(ColorStyle::Colorless, ColorStyle::Gradient(bg)) => {
write!(f, "{}", bg.build(&self.text, TargetGround::Background))
}
(ColorStyle::Colorless, ColorStyle::Colorless) => {
write!(f, "{}\x1B[0m", self.text)
}
}
}
}
pub trait ColorApply: Into<ColorStyle> {
fn to<C>(self, c: C) -> StyledText
where
C: Colorize + ?Sized,
{
c.fg::<Self>(self)
}
fn under<C>(self, c: C) -> StyledText
where
C: Colorize + ?Sized,
{
c.bg::<Self>(self)
}
}
impl ColorApply for RGB {}
impl ColorApply for HSL {}
impl ColorApply for Color {}
impl ColorApply for Gradient {}