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
use std::fmt::Display;
pub trait Color {
fn rgb(&self, r: u8, g: u8, b: u8, ctype: ColorType) -> ColorString;
fn style(&self, effect: Effect) -> ColorString;
}
pub trait ColorExt {
fn rgb_fg(&self, r: u8, g: u8, b: u8) -> ColorString;
fn rgb_bg(&self, r: u8, g: u8, b: u8) -> ColorString;
fn red(&self) -> ColorString;
fn red_bg(&self) -> ColorString;
fn green(&self) -> ColorString;
fn green_bg(&self) -> ColorString;
fn yellow(&self) -> ColorString;
fn yellow_bg(&self) -> ColorString;
fn blue(&self) -> ColorString;
fn blue_bg(&self) -> ColorString;
fn light_blue(&self) -> ColorString;
fn light_blue_bg(&self) -> ColorString;
fn italic(&self) -> ColorString;
fn bold(&self) -> ColorString;
fn underline(&self) -> ColorString;
fn crossed_out(&self) -> ColorString;
}
impl<T> Color for T
where
T: Display,
{
fn rgb(&self, r: u8, g: u8, b: u8, ctype: ColorType) -> ColorString {
ColorString {
string: self,
color: Some(Rgb { r, g, b, ctype }),
effect: None,
}
}
fn style(&self, effect: Effect) -> ColorString {
ColorString {
string: self,
color: None,
effect: Some(effect),
}
}
}
impl<T> ColorExt for T
where
T: Color,
{
fn rgb_fg(&self, r: u8, g: u8, b: u8) -> ColorString {
self.rgb(r, g, b, ColorType::Fg)
}
fn rgb_bg(&self, r: u8, g: u8, b: u8) -> ColorString {
self.rgb(r, g, b, ColorType::Bg)
}
fn red(&self) -> ColorString {
self.rgb_fg(255, 0, 0)
}
fn red_bg(&self) -> ColorString {
self.rgb_bg(255, 0, 0)
}
fn green(&self) -> ColorString {
self.rgb_fg(0, 255, 0)
}
fn green_bg(&self) -> ColorString {
self.rgb_bg(0, 255, 0)
}
fn yellow(&self) -> ColorString {
self.rgb_fg(255, 255, 0)
}
fn yellow_bg(&self) -> ColorString {
self.rgb_bg(255, 255, 0)
}
fn blue(&self) -> ColorString {
self.rgb_fg(0, 0, 255)
}
fn blue_bg(&self) -> ColorString {
self.rgb_bg(0, 0, 255)
}
fn light_blue(&self) -> ColorString {
self.rgb_fg(0, 150, 255)
}
fn light_blue_bg(&self) -> ColorString {
self.rgb_bg(0, 150, 255)
}
fn italic(&self) -> ColorString {
self.style(Effect::Italic)
}
fn bold(&self) -> ColorString {
self.style(Effect::Bold)
}
fn underline(&self) -> ColorString {
self.style(Effect::Underline)
}
fn crossed_out(&self) -> ColorString {
self.style(Effect::CrossedOut)
}
}
pub enum Effect {
Bold,
Faint,
Italic,
Underline,
ReverseVideo,
CrossedOut,
}
impl std::fmt::Display for Effect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Effect::Bold => write!(f, "1"),
Effect::Faint => write!(f, "2"),
Effect::Italic => write!(f, "3"),
Effect::Underline => write!(f, "4"),
Effect::ReverseVideo => write!(f, "7"),
Effect::CrossedOut => write!(f, "9"),
}
}
}
pub enum ColorType {
Fg,
Bg,
}
struct Rgb {
r: u8,
g: u8,
b: u8,
ctype: ColorType,
}
impl std::fmt::Display for Rgb {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const FG_DEL: &str = "38;2";
const BG_DEL: &str = "48;2";
let del = match self.ctype {
ColorType::Fg => FG_DEL,
ColorType::Bg => BG_DEL,
};
write!(f, "{};{};{};{}", del, self.r, self.g, self.b)
}
}
#[doc(hidden)]
pub struct ColorString<'a> {
string: &'a dyn Display,
color: Option<Rgb>,
effect: Option<Effect>,
}
impl Display for ColorString<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const START_DEL: &str = "\x1b[";
const COMPONENT_DEL: &str = ";";
const COLOR_END_DEL: &str = "m";
const END_DEL: &str = "\x1b[0m";
write!(f, "{}", START_DEL)?;
if let Some(ref color) = self.color {
write!(f, "{}", color)?;
}
if let Some(ref effect) = self.effect {
if self.color.is_some() {
write!(f, "{}", COMPONENT_DEL)?;
}
write!(f, "{}", effect)?;
}
write!(f, "{}", COLOR_END_DEL)?;
write!(f, "{}", self.string)?;
write!(f, "{}", END_DEL)
}
}