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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use log::info;
use rand::{thread_rng, Rng};
use std::io::Result;
use ratatui::{
layout::Rect, prelude::{CrosstermBackend, Terminal}, style::Style, text::{Line, Span, Text}, widgets::Paragraph, Frame
};
use std::io::Stdout;
pub struct State {
pub color: String,
pub speed: u64,
pub direction: Direction,
}
// Keep track of the state of each column individually
#[derive(Clone, Debug)]
pub struct LineState {
//Whether the stream is on or off
pub stream: Stream,
// The state of the line
pub line: Vec<Cell>,
// How many random chars to write
pub chars: usize,
// How many white spaces to write
pub whitespace: usize,
}
impl LineState {
// Create anew line with random number of chars and whitespace to create
pub fn new(height: usize) -> Self {
let mut rng = thread_rng();
let stream = match rng.gen_bool(0.02) {
true => Stream::On,
false => Stream::Off,
};
Self {
stream,
line: vec![Cell::Whitespace; height],
chars: rng.gen_range(5..height / 2),
whitespace: rng.gen_range(10..height),
}
}
// Update the line each tick
pub fn update_line(&mut self) {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(}{][*&^%$#@!~";
let mut rng = thread_rng();
let mut updated = false;
match self.stream {
Stream::Off => {
let line_len = self.line.len() - 1;
let mut iter = self.line.iter_mut();
loop {
let next = iter.next();
match next {
Some(cell) => match cell {
Cell::Whitespace => {
updated = false;
}
Cell::Sym(sym) => match sym.white {
true => {
let idx = thread_rng().gen_range(0..CHARSET.len());
let rand_char = CHARSET[idx] as char;
sym.white = false;
let next_cell = iter.next();
if let Some(cell) = next_cell {
*cell = Cell::Sym(Sym {
value: rand_char.to_string(),
white: true,
});
}
updated = true;
}
false => {
if !updated {
*cell = Cell::Whitespace;
updated = true;
}
}
},
},
None => {
break;
}
}
}
self.whitespace -= 1;
if self.whitespace == 0 {
self.stream = Stream::On;
self.whitespace = rng.gen_range(10..line_len);
}
}
Stream::On => {
let line_len = self.line.len() - 1;
let mut iter = self.line.iter_mut();
loop {
let next = iter.next();
match next {
Some(cell) => match cell {
Cell::Whitespace => {
if !updated {
let idx = thread_rng().gen_range(0..CHARSET.len());
let rand_char = CHARSET[idx] as char;
*cell = Cell::Sym(Sym {
value: rand_char.to_string(),
white: true,
});
updated = true;
}
}
Cell::Sym(sym) => match sym.white {
true => {
let idx = thread_rng().gen_range(0..CHARSET.len());
let rand_char = CHARSET[idx] as char;
sym.white = false;
let next_cell = iter.next();
if let Some(cell) = next_cell {
*cell = Cell::Sym(Sym {
value: rand_char.to_string(),
white: true,
});
}
updated = true;
}
false => {
if updated {
*cell = Cell::Whitespace;
updated = false;
}
}
},
},
None => {
break;
}
}
}
self.chars -= 1;
if self.chars == 0 {
self.stream = Stream::Off;
self.chars = rng.gen_range(5..line_len);
}
}
}
}
}
// A symbol has a character value and either is white (first of stream) or not
#[derive(Clone, Debug)]
pub struct Sym {
pub value: String,
pub white: bool,
}
// A cell either is a symbol or a whitespace
#[derive(Clone, Debug)]
pub enum Cell {
Sym(Sym),
Whitespace,
}
// The stream is either on (printing chars) or off (printing whitespace)
#[derive(Clone, Debug)]
pub enum Stream {
On,
Off,
}
//
// The stream is either on (printing chars) or off (printing whitespace)
#[derive(Clone, Debug, PartialEq)]
pub enum Direction {
Down,
Up,
Left,
Right,
}
pub fn process_matrix_cols(i: usize, line: Rect, frame: &mut Frame, matrix: &mut [LineState], state: &State) {
if i / 2 >= matrix.len() {
return
}
let line_state = matrix.get_mut(i / 2).unwrap();
if state.direction == Direction::Up || state.direction == Direction::Left {
line_state.line.reverse();
}
let new_line = line_state.line.clone();
let lines: Vec<Line> = new_line
.into_iter()
.map(|cell| {
// Determine how to print each line
match cell {
Cell::Sym(sym) => match sym.white {
true => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::White),
)),
false => match state.color.as_str() {
"blue" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Blue),
)),
"cyan" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Cyan),
)),
"red" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Red),
)),
"purple" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Magenta),
)),
"yellow" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Yellow),
)),
"rainbow" => {
let mut rng = thread_rng();
let colors = ["blue", "cyan", "red", "purple", "yellow", "green"];
let index = rng.gen_range(0..=colors.len() - 1);
let color = colors[index];
match color {
"blue" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Blue),
)),
"cyan" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Cyan),
)),
"red" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Red),
)),
"purple" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Magenta),
)),
"yellow" => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Yellow),
)),
_ => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Green),
)),
}
}
_ => Line::from(Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Green),
)),
},
},
Cell::Whitespace => Line::from(String::from(" ")),
}
})
.collect();
// Render the line as a paragraph
frame.render_widget(Paragraph::new(Text::from(lines)), line);
if state.direction == Direction::Up || state.direction == Direction::Left {
line_state.line.reverse();
}
}
pub fn process_matrix_rows(i: usize, line: Rect, frame: &mut Frame, matrix: &mut [LineState], state: &State) {
if i >= matrix.len() {
return
}
let line_state = matrix.get_mut(i).unwrap();
if state.direction == Direction::Up || state.direction == Direction::Left {
line_state.line.reverse();
}
let new_line = line_state.line.clone();
let lines: Vec<Span> = new_line
.into_iter()
.map(|cell| {
// Determine how to print each line
match cell {
Cell::Sym(sym) => match sym.white {
true => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::White),
),
false => match state.color.as_str() {
"blue" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Blue),
),
"cyan" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Cyan),
),
"red" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Red),
),
"purple" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Magenta),
),
"yellow" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Yellow),
),
"rainbow" => {
let mut rng = thread_rng();
let colors = ["blue", "cyan", "red", "purple", "yellow", "green"];
let index = rng.gen_range(0..=colors.len() - 1);
let color = colors[index];
match color {
"blue" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Blue),
),
"cyan" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Cyan),
),
"red" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Red),
),
"purple" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Magenta),
),
"yellow" => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Yellow),
),
_ => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Green),
),
}
}
_ => Span::styled(
sym.value,
Style::default().fg(ratatui::style::Color::Green),
),
},
},
Cell::Whitespace => Span::from(String::from(" ")),
}
})
.collect();
// Render the line
frame.render_widget(Line::from(lines), line);
if state.direction == Direction::Up || state.direction == Direction::Left {
line_state.line.reverse();
}
}
pub fn create_matrix(matrix: &mut Vec<LineState>, terminal: &mut Terminal<CrosstermBackend<Stdout>>, state: &State) -> Result<()> {
let terminal_size = terminal.size().unwrap();
let t_height = terminal_size.height;
let t_width = terminal_size.width;
terminal.clear()?;
// Create new matrix where each column has its own state
// Only need half the columns because using all looks cluttered
*matrix = Vec::new();
if state.direction == Direction::Up || state.direction == Direction::Down {
for _ in 0..t_width / 2 + 1 {
matrix.push(LineState::new(t_height.into()));
}
} else {
for _ in 0..t_height - 1 {
matrix.push(LineState::new(t_width.into()));
}
}
info!("Matrix len: {}", matrix.len());
Ok(())
}