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
use super::*;
const SEPARATOR: char = ';';
pub fn categorise_text(text: &str) -> CategorisedSlices {
let matches = parse(text);
let mut sgr = SGR::default();
let mut lo = 0;
let mut slices: Vec<CategorisedSlice> = Vec::with_capacity(matches.len() + 1);
for m in matches {
if m.start != lo {
slices.push(CategorisedSlice::with_sgr(sgr, &text[lo..m.start]));
}
sgr = handle_seq(&m);
lo = m.end;
}
if lo != text.len() {
slices.push(CategorisedSlice::with_sgr(sgr, &text[lo..text.len()]));
}
slices
}
fn handle_seq(m: &Match) -> SGR {
let slice = &m.text[2..(m.text.len() - 1)];
let styles = slice.split(SEPARATOR);
let mut sgr = SGR::default();
for style in styles {
adjust_sgr(&mut sgr, style);
}
sgr
}
fn adjust_sgr(sgr: &mut SGR, seq: &str) {
match seq {
"0" => *sgr = SGR::default(),
"1" => sgr.intensity = Intensity::Bold,
"2" => sgr.intensity = Intensity::Faint,
"3" => sgr.italic = true,
"4" => sgr.underline = true,
"5" => sgr.blink = true,
"7" => sgr.reversed = true,
"8" => sgr.hidden = true,
"9" => sgr.strikethrough = true,
"22" => sgr.intensity = Intensity::Normal,
"23" => sgr.italic = false,
"24" => sgr.underline = false,
"25" => sgr.blink = false,
"27" => sgr.reversed = false,
"28" => sgr.hidden = false,
"29" => sgr.strikethrough = false,
"30" => sgr.fg_colour = Color::Black,
"31" => sgr.fg_colour = Color::Red,
"32" => sgr.fg_colour = Color::Green,
"33" => sgr.fg_colour = Color::Yellow,
"34" => sgr.fg_colour = Color::Blue,
"35" => sgr.fg_colour = Color::Magenta,
"36" => sgr.fg_colour = Color::Cyan,
"37" => sgr.fg_colour = Color::White,
"40" => sgr.bg_colour = Color::Black,
"41" => sgr.bg_colour = Color::Red,
"42" => sgr.bg_colour = Color::Green,
"43" => sgr.bg_colour = Color::Yellow,
"44" => sgr.bg_colour = Color::Blue,
"45" => sgr.bg_colour = Color::Magenta,
"46" => sgr.bg_colour = Color::Cyan,
"47" => sgr.bg_colour = Color::White,
"90" => sgr.fg_colour = Color::BrightBlack,
"91" => sgr.fg_colour = Color::BrightRed,
"92" => sgr.fg_colour = Color::BrightGreen,
"93" => sgr.fg_colour = Color::BrightYellow,
"94" => sgr.fg_colour = Color::BrightBlue,
"95" => sgr.fg_colour = Color::BrightMagenta,
"96" => sgr.fg_colour = Color::BrightCyan,
"97" => sgr.fg_colour = Color::BrightWhite,
"100" => sgr.bg_colour = Color::BrightBlack,
"101" => sgr.bg_colour = Color::BrightRed,
"102" => sgr.bg_colour = Color::BrightGreen,
"103" => sgr.bg_colour = Color::BrightYellow,
"104" => sgr.bg_colour = Color::BrightBlue,
"105" => sgr.bg_colour = Color::BrightMagenta,
"106" => sgr.bg_colour = Color::BrightCyan,
"107" => sgr.bg_colour = Color::BrightWhite,
_ => (),
}
}
#[cfg(test)]
mod tests {
use super::*;
use colored::Colorize;
#[test]
fn cat_and_matches_len() {
let txt = "hello";
let matches = parse(&txt);
let cat = categorise_text(&txt);
assert!(matches.len() + 1 >= cat.len());
let txt = "hello".bright_green();
let matches = parse(&txt);
let cat = categorise_text(&txt);
assert!(matches.len() + 1 >= cat.len());
let txt = format!("{}{}{}", "hello".bright_green(), "world".red(), "whatever");
let matches = parse(&txt);
let cat = categorise_text(&txt);
assert!(matches.len() + 1 >= cat.len());
}
}