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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::{fmt::Display,
io::{stdout, Write},
thread::sleep,
time::Duration};
use rand::{thread_rng, Rng};
use crate::*;
#[macro_export]
macro_rules! colorize_using_lolcat {
($lolcat: expr, $($arg:tt)*) => {
format!("{}", std::format_args!($($arg)*)).color_with($lolcat);
};
}
pub type OutputCollectorType = Vec<String>;
#[derive(Debug, Clone)]
pub struct OutputCollector {
pub output_vec: OutputCollectorType,
}
impl OutputCollector {
pub fn from(output_vec: OutputCollectorType) -> OutputCollector { OutputCollector { output_vec } }
}
impl Display for OutputCollector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", String::from_iter(self.output_vec.clone()))
}
}
pub trait LolcatStringExt {
fn color_with(&self, lolcat: &mut Lolcat) -> String;
}
impl LolcatStringExt for String {
fn color_with(&self, lolcat: &mut Lolcat) -> String { lolcat.format_str(self).to_string() }
}
pub trait SupportsColor {
fn get_lolcat(&mut self) -> &mut Lolcat;
}
#[macro_export]
macro_rules! my_print {
($output_collector: expr, $($arg:tt)*) => {
_print($output_collector, std::format_args!($($arg)*))
};
}
fn _print(output_vec: &mut OutputCollectorType, args: std::fmt::Arguments) {
let content = format!("{}", args);
output_vec.push(content);
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Lolcat {
color_wheel_control: ColorWheelControl,
}
impl Lolcat {
pub fn new() -> Self {
let control = ColorWheelControl::default();
Self {
color_wheel_control: control,
}
}
pub fn format_str(&mut self, input_str: &str) -> OutputCollector {
let chars_iter: std::str::Chars = input_str.chars();
self.format_iter(chars_iter, true)
}
pub fn to_string(output_vec: OutputCollectorType) -> String { String::from_iter(output_vec) }
fn format_iter<I: Iterator<Item = char>>(
&mut self, mut iter: I, constantly_flush: bool,
) -> OutputCollector {
let mut original_seed = self.color_wheel_control.seed;
let mut ignore_whitespace = self.color_wheel_control.background_mode;
let mut output_vec: OutputCollectorType = vec![];
if !self.color_wheel_control.print_color {
for character in iter {
my_print!(&mut output_vec, "{}", character);
}
return OutputCollector::from(output_vec);
}
while let Some(character) = iter.next() {
match character {
'\x1b' => {
my_print!(&mut output_vec, "\x1b");
let mut escape_sequence_character = iter
.next()
.expect("Escape character with no escape sequence after it");
my_print!(&mut output_vec, "{}", escape_sequence_character);
match escape_sequence_character {
'[' => loop {
escape_sequence_character =
iter.next().expect("CSI escape sequence did not terminate");
my_print!(&mut output_vec, "{}", escape_sequence_character);
match escape_sequence_character {
'\x30'..='\x3F' => continue,
'\x20'..='\x2F' => {
loop {
escape_sequence_character =
iter.next().expect("CSI escape sequence did not terminate");
my_print!(&mut output_vec, "{}", escape_sequence_character);
match escape_sequence_character {
'\x20'..='\x2F' => continue,
'\x40'..='\x7E' => break,
_ => {
panic!("CSI escape sequence terminated with an incorrect value")
}
}
}
break;
}
'\x40'..='\x7E' => break,
_ => panic!("CSI escape sequence terminated with an incorrect value"),
}
},
'\x20'..='\x2F' => loop {
escape_sequence_character =
iter.next().expect("nF escape sequence did not terminate");
my_print!(&mut output_vec, "{}", escape_sequence_character);
match escape_sequence_character {
'\x20'..='\x2F' => continue,
'\x30'..='\x7E' => break,
_ => panic!("nF escape sequence terminated with an incorrect value"),
}
},
_ => (),
}
}
'\n' => {
if self.color_wheel_control.print_color {
if self.color_wheel_control.background_mode {
my_print!(&mut output_vec, "\x1b[49m");
}
}
println!();
if self.color_wheel_control.dialup_mode {
let mut rng = thread_rng();
let rand_value: u64 = rng.gen_range(30..700);
let stall = Duration::from_millis(rand_value);
sleep(stall);
}
original_seed += f64::from(self.color_wheel_control.color_change_speed);
self.color_wheel_control.seed = original_seed;
ignore_whitespace = self.color_wheel_control.background_mode;
}
_ => {
if ignore_whitespace && character.is_whitespace() {
my_print!(&mut output_vec, "{}", character);
continue;
} else {
ignore_whitespace = false;
}
self.colored_print(&mut output_vec, character);
self.color_wheel_control.seed += f64::from(self.color_wheel_control.color_change_speed);
}
}
if constantly_flush {
self.reset_colors(&mut output_vec);
stdout().flush().unwrap();
}
}
OutputCollector::from(output_vec)
}
fn reset_colors(&self, output_vec: &mut OutputCollectorType) {
if self.color_wheel_control.print_color {
if self.color_wheel_control.background_mode {
my_print!(output_vec, "\x1b[49m");
}
my_print!(output_vec, "\x1b[39m");
}
}
fn colored_print(&self, output_vec: &mut OutputCollectorType, character: char) {
if self.color_wheel_control.background_mode {
let bg = ColorUtils::get_color_tuple(&self.color_wheel_control);
let fg = ColorUtils::calc_fg_color(bg);
my_print!(
output_vec,
"\x1b[38;2;{};{};{};48;2;{};{};{}m{}",
fg.0,
fg.1,
fg.2,
bg.0,
bg.1,
bg.2,
character
);
} else {
let fg = ColorUtils::get_color_tuple(&self.color_wheel_control);
my_print!(
output_vec,
"\x1b[38;2;{};{};{}m{}",
fg.0,
fg.1,
fg.2,
character
);
}
}
}