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
pub mod colors {
use core::fmt;
pub struct ColoredString<'a> {
text_to_color: &'a str,
colors: Vec<String>,
// Not related to user text...
all_colors: Vec<&'a str>,
all_background_colors: Vec<&'a str>,
}
impl<'a> ColoredString<'a> {
pub fn new(text_to_color: &'a str) -> ColoredString<'a> {
ColoredString {
text_to_color,
colors: vec![],
all_colors: vec![
"0;30", "0;31", "0;32", "0;33", "0;34", "0;35", "0;36", "0;37",
],
all_background_colors: vec![
"0;40", "0;41", "0;42", "0;43", "0;44", "0;45", "0;46", "0;47",
],
}
}
/// Sets the color of the **text** based off of a index. This index refers to a color
/// in a vector of colors, either custom or default. If you set not custom colors
/// the list of colors is: black (0), red (1), green (2), yellow (3), blue (4),
/// purple (5), cyan (6), white (7).
pub fn text_from_index(&mut self, index: usize) -> &mut Self {
match self.all_colors.iter().nth(index) {
Some(color_code) => {
self.colors.push(color_code.to_string());
}
None => {
self.colors.push("0;37".to_string());
}
}
self
}
/// Sets the color of the **background** based off of a index. This index refers to a color
/// in a vector of colors, either custom or default. If you set not custom colors
/// the list of colors is: black (0), red (1), green (2), yellow (3), blue (4),
/// purple (5), cyan (6), white (7).
pub fn bg_from_index(&mut self, index: usize) -> &mut Self {
match self.all_background_colors.iter().nth(index) {
Some(color_code) => {
self.colors.push(color_code.to_string());
}
None => {
self.colors.push("0;37".to_string());
}
}
self
}
/// Sets the list of custom **text** colors. If you set custom colors, you must call
/// `text_from_index()` **after** you use this method to use custom colors, otherwise
/// you will use the preset colors (see `text_from_index` for the default colors).
pub fn set_custom_text(&mut self, colors: Vec<&'static str>) -> &mut Self {
self.all_colors = colors;
self
}
/// Sets the list of custom **background** colors. If you set custom colors, you must call
/// `bg_from_index()` **after** you use this method to use custom colors, otherwise
/// you will use the preset colors (see `bg_from_index` for the default colors).
pub fn set_custom_bg(&mut self, colors: Vec<&'static str>) -> &mut Self {
self.all_background_colors = colors;
self
}
/* Below is normal colors, not underlines or backgrounds */
/// Turns the text black
pub fn black(&mut self) -> &mut Self {
self.colors.push(String::from("0;30"));
self
}
/// Turns the text red
pub fn red(&mut self) -> &mut Self {
self.colors.push(String::from("0;31"));
self
}
/// Turns the text green
pub fn green(&mut self) -> &mut Self {
self.colors.push(String::from("0;32"));
self
}
/// Turns the text yellow
pub fn yellow(&mut self) -> &mut Self {
self.colors.push(String::from("0;33"));
self
}
/// Turns the text blue
pub fn blue(&mut self) -> &mut Self {
self.colors.push(String::from("0;34"));
self
}
/// Turns the text purple
pub fn purple(&mut self) -> &mut Self {
self.colors.push(String::from("0;35"));
self
}
/// Turns the text cyan
pub fn cyan(&mut self) -> &mut Self {
self.colors.push(String::from("0;36"));
self
}
/// Turns the text white
pub fn white(&mut self) -> &mut Self {
self.colors.push(String::from("0;37"));
self
}
/* Below is all background related colors */
/// Turns the background black
pub fn bg_black(&mut self) -> &mut Self {
self.colors.push(String::from("40"));
self
}
/// Turns the background red
pub fn bg_red(&mut self) -> &mut Self {
self.colors.push(String::from("41"));
self
}
/// Turns the background green
pub fn bg_green(&mut self) -> &mut Self {
self.colors.push(String::from("42"));
self
}
/// Turns the background yellow
pub fn bg_yellow(&mut self) -> &mut Self {
self.colors.push(String::from("43"));
self
}
/// Turns the background blue
pub fn bg_blue(&mut self) -> &mut Self {
self.colors.push(String::from("44"));
self
}
/// Turns the background purple
pub fn bg_purple(&mut self) -> &mut Self {
self.colors.push(String::from("45"));
self
}
/// Turns the background cyan
pub fn bg_cyan(&mut self) -> &mut Self {
self.colors.push(String::from("46"));
self
}
/// Turns the background white
pub fn bg_white(&mut self) -> &mut Self {
self.colors.push(String::from("47"));
self
}
}
impl fmt::Display for ColoredString<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let colors = self.colors.join(";");
let string = format!("{}m{}", colors, self.text_to_color).to_string();
let finish_stringed = format!("\u{001b}[{}\u{001b}[0m", string).to_string();
write!(f, "{}", finish_stringed)
}
}
}
#[cfg(test)]
mod tests {
use crate::colors::ColoredString;
#[test]
fn please_work() {
println!("{}", ColoredString::new("text?").bg_from_index(5));
println!("Some more text :)");
}
#[test]
fn lifetimes_are_dumb() {
let mut text = ColoredString::new("ewe?");
text.bg_from_index(5);
println!("{}", text);
println!("Some more text :)");
}
}