-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathformat_string.rs
More file actions
executable file
·124 lines (110 loc) · 4 KB
/
format_string.rs
File metadata and controls
executable file
·124 lines (110 loc) · 4 KB
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
use bracket_color::prelude::*;
#[derive(Debug)]
pub struct ColoredTextSpans {
pub length: usize,
pub spans: Vec<(RGBA, String)>,
}
fn find_color(col_name: &str) -> RGBA {
if let Some(palette) = palette_color(&col_name) {
palette
} else {
RGBA::from_u8(255, 255, 255, 255)
}
}
impl ColoredTextSpans {
pub fn new(text: &str) -> Self {
let mut result = Self {
length: 0,
spans: Vec::new(),
};
let mut color_stack = Vec::new();
let text: String = match text.starts_with("#[") {
true => text.to_owned(),
false => "#[white]".to_string() + text,
};
for color_span in text.split("#[") {
if color_span.is_empty() {
continue;
}
let mut col_text = color_span.splitn(2, ']');
let col_name = col_text.next().unwrap();
if let Some(text_span) = col_text.next() {
if !col_name.is_empty() {
color_stack.push(find_color(col_name));
} else {
color_stack.pop();
}
result.spans.push((
*color_stack
.last()
.unwrap_or(&RGBA::from_u8(255, 255, 255, 255)),
text_span.to_string(),
));
result.length += text_span.chars().count();
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
// use crate::prelude::*;
#[test]
fn no_colors() {
let text = "This is a simple message.";
let span = ColoredTextSpans::new(text);
assert_eq!(span.length, text.len());
assert_eq!(span.spans.len(), 1);
assert_eq!(span.spans[0].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[0].1, text);
}
#[test]
fn no_start_color() {
register_palette_color("blue", RGBA::from_u8(0, 0, 255, 255));
let text = "This is a #[blue]simple#[] message.";
let span = ColoredTextSpans::new(text);
assert_eq!(span.length, 25);
assert_eq!(span.spans.len(), 3);
assert_eq!(span.spans[0].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[0].1, "This is a ");
assert_eq!(span.spans[1].0, RGBA::from_u8(0, 0, 255, 255));
assert_eq!(span.spans[1].1, "simple");
assert_eq!(span.spans[2].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[2].1, " message.");
}
#[test]
fn start_color() {
let text = "#[white]This is a simple message.";
let span = ColoredTextSpans::new(text);
assert_eq!(span.length, text.len() - "$[white]".len());
assert_eq!(span.spans.len(), 1);
assert_eq!(span.spans[0].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[0].1, "This is a simple message.");
}
#[test]
fn color_with_pop() {
register_palette_color("blue", RGBA::from_u8(0, 0, 255, 255));
let text = "#[white]This is a #[blue]simple#[] message.";
let span = ColoredTextSpans::new(text);
assert_eq!(span.length, 25);
assert_eq!(span.spans.len(), 3);
assert_eq!(span.spans[0].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[0].1, "This is a ");
assert_eq!(span.spans[1].0, RGBA::from_u8(0, 0, 255, 255));
assert_eq!(span.spans[1].1, "simple");
assert_eq!(span.spans[2].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[2].1, " message.");
}
#[test]
fn pop_color() {
let text = "#[white]This#[] is a simple message.";
let span = ColoredTextSpans::new(text);
assert_eq!(span.length, 25);
assert_eq!(span.spans.len(), 2);
assert_eq!(span.spans[0].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[0].1, "This");
assert_eq!(span.spans[1].0, RGBA::from_u8(255, 255, 255, 255));
assert_eq!(span.spans[1].1, " is a simple message.");
}
}