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
mod colors;
use colors::*;
pub trait Colored {
fn blue(&self) -> String;
fn cyan(&self) -> String;
fn green(&self) -> String;
fn magenta(&self) -> String;
fn orange(&self) -> String;
fn red(&self) -> String;
fn yellow(&self) -> String;
fn rgb(&self, red: u8, green: u8, blue: u8) -> String;
}
fn wrap(color: &str, str: &str) -> String {
format!("{color}{str}{END}")
}
impl<T: AsRef<str>> Colored for T {
fn blue(&self) -> String {
wrap(BLUE, self.as_ref())
}
fn cyan(&self) -> String {
wrap(CYAN, self.as_ref())
}
fn green(&self) -> String {
wrap(GREEN, self.as_ref())
}
fn magenta(&self) -> String {
wrap(MAGENTA, self.as_ref())
}
fn orange(&self) -> String {
wrap(ORANGE, self.as_ref())
}
fn red(&self) -> String {
wrap(RED, self.as_ref())
}
fn yellow(&self) -> String {
wrap(YELLOW, self.as_ref())
}
fn rgb(&self, r: u8, g: u8, b: u8) -> String {
wrap(&format!("\x1b[38;2;{r};{g};{b}m"), self.as_ref())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn primary() {
let test = "Hello 12345";
assert_eq!("\x1b[38;2;225;50;50mHello 12345\x1b[0m", test.red());
assert_eq!(
"\x1b[38;2;255;255;255mHello 12345\x1b[0m",
test.rgb(255, 255, 255)
);
assert_eq!(
test.to_owned().red(),
"\x1b[38;2;225;50;50mHello 12345\x1b[0m"
)
}
#[test]
fn fail() {
let test = "Test 12345";
println!("{}", test.blue());
println!("{}", test.cyan());
println!("{}", test.green());
println!("{}", test.magenta());
println!("{}", test.orange());
println!("{}", test.red());
println!("{}", test.yellow());
println!("{}", test.rgb(0xff, 0xff, 0xff));
}
}