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
use std::collections::HashMap;
use super::{Color, TextBuffer};
use regex::Regex;
use crate::text_processing::{OptTextStyle, Processable, ProcessedChar, TextProcessor};
#[derive(Default, Debug, Clone)]
pub struct Parser {
colors: HashMap<String, Color>,
}
impl Parser {
pub fn new() -> Parser {
Parser {
colors: HashMap::<String, Color>::new(),
}
}
pub fn add_color<T: Into<String>>(&mut self, color_str: T, color: Color) {
self.colors.insert(color_str.into(), color);
}
pub fn write<T: Into<String>>(&self, text_buffer: &mut TextBuffer, text: T) {
text_buffer.write_processed(&self.parse(text));
}
pub fn parse<T: Into<String>>(&self, text: T) -> Vec<ProcessedChar> {
let text = text.into();
self.process(vec![text.into()])
}
#[cfg(test)]
pub(crate) fn get_color(&self, color: &str) -> Option<&Color> {
self.colors.get(color)
}
}
impl TextProcessor for Parser {
fn process(&self, processables: Vec<Processable>) -> Vec<ProcessedChar> {
let mut fg_stack = Vec::new();
let mut bg_stack = Vec::new();
let mut shakiness_stack = Vec::new();
let mut current_style = OptTextStyle {
fg_color: None,
bg_color: None,
shakiness: None,
};
let regex = Regex::new(r"\[(/)?((fg|bg|shake)(=([A-z]+|\d+(\.\d+)?))?)\]").unwrap();
let mut parsed = Vec::new();
for processable in processables {
match processable {
Processable::ToProcess(text) => {
let mut parts = regex.split(&text);
for capture in regex.captures_iter(&text) {
parsed.push(ParsedText {
text: parts.next().unwrap().to_owned(),
style: current_style.clone(),
});
if let Some(target) = capture.get(3) {
if capture.get(1).is_some() {
if target.as_str() == "shake" {
current_style.shakiness = shakiness_stack.pop();
} else if target.as_str() == "fg" {
current_style.fg_color = fg_stack.pop();
} else if target.as_str() == "bg" {
current_style.bg_color = bg_stack.pop();
}
}
if let Some(value) = capture.get(5) {
if target.as_str() == "shake" {
let value = match value.as_str().parse::<f32>() {
Ok(val) => val,
Err(e) => panic!("Failed to parse shake-number: {}", e),
};
if let Some(shakiness) = current_style.shakiness {
shakiness_stack.push(shakiness);
}
current_style.shakiness = Some(value);
} else if let Some(color) = self.colors.get(value.as_str()) {
if target.as_str() == "fg" {
if let Some(fg) = current_style.fg_color {
fg_stack.push(fg);
}
current_style.fg_color = Some(*color);
} else {
if let Some(bg) = current_style.bg_color {
bg_stack.push(bg);
}
current_style.bg_color = Some(*color);
}
}
}
}
}
if let Some(last_part) = parts.next() {
parsed.push(ParsedText {
text: last_part.to_owned(),
style: current_style.clone(),
});
}
}
Processable::NoProcess(text) => {
parsed.push(ParsedText {
text: text,
style: current_style.clone(),
});
}
}
}
let mut list = Vec::new();
for text in parsed {
for character in text.text.chars() {
list.push(ProcessedChar {
character: character,
style: text.style.clone(),
});
}
}
list
}
}
#[derive(Clone, Debug)]
struct ParsedText {
pub text: String,
pub style: OptTextStyle,
}