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
extern crate pancurses;
extern crate rand;
extern crate structopt;
extern crate term_size;
pub mod config;
use config::Config;
use pancurses::*;
use rand::distributions::{Distribution, Standard};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use std::cell::RefCell;
thread_local! {
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
}
fn gen<T>() -> T
where
Standard: Distribution<T>,
{
RNG.with(|rng| (*rng).borrow_mut().gen::<T>())
}
fn rand_char() -> char {
let (randnum, randmin) = (93, 33);
RNG.with(|rng| (*rng).borrow_mut().gen::<u8>() % randnum + randmin) as char
}
fn coin_flip() -> bool {
RNG.with(|rng| (*rng).borrow_mut().gen())
}
pub struct Matrix {
m: Vec<Column>,
cols: usize,
lines: usize,
}
impl Default for Matrix {
fn default() -> Self {
let (lines, cols) = get_term_size();
Matrix {
m: (0..cols).map(|_| Column::new(lines)).collect(),
cols,
lines,
}
}
}
impl Matrix {
pub fn arrange(&mut self, config: &Config) {
let lines = self.lines;
self.m.iter_mut().for_each(|col| {
if col.head_is_empty() && col.spaces != 0 {
col.spaces -= 1;
} else if col.head_is_empty() && col.spaces == 0 {
col.new_rand_head(config);
col.length -= 1;
col.spaces = gen::<usize>() % lines + 1;
} else if col.length != 0 {
col.new_rand_char();
col.length -= 1;
} else {
col.col[0].val = ' ';
col.length = gen::<usize>() % (lines - 3) + 3;
}
});
if config.oldstyle {
self.old_style_move_down();
} else {
self.move_down();
}
}
fn move_down(&mut self) {
self.m.iter_mut().for_each(|col| {
let mut in_stream = false;
let mut last_was_white = false;
let mut running_color = COLOR_CYAN;
col.col.iter_mut().for_each(|block| {
if !in_stream {
if !block.is_space() {
block.val = ' ';
in_stream = true;
running_color = block.color;
}
} else if block.is_space() {
block.val = rand_char();
block.white = last_was_white;
in_stream = false;
}
std::mem::swap(&mut last_was_white, &mut block.white);
block.color = running_color;
})
})
}
fn old_style_move_down(&mut self) {
self.m.iter_mut().for_each(|col| {
let mut tmp = Block::default();
col.col.iter_mut().for_each(|block| {
std::mem::swap(&mut tmp, block);
})
});
}
pub fn draw(&self, window: &Window, config: &Config) {
for j in 1..self.lines {
for i in 0..self.cols {
window.mv(j as i32 - 1, 2 * i as i32);
let mcolour = if self[i][j].white {
COLOR_WHITE
} else {
self[i][j].color
};
window.attron(COLOR_PAIR(mcolour as chtype));
window.addch(self[i][j].val as chtype);
window.attroff(COLOR_PAIR(mcolour as chtype));
}
}
napms(config.update as i32 * 10);
}
}
use std::ops;
impl ops::Index<usize> for Matrix {
type Output = Column;
fn index(&self, i: usize) -> &Self::Output {
&self.m[i]
}
}
pub struct Column {
length: usize,
spaces: usize,
col: Vec<Block>,
}
impl Column {
fn new(lines: usize) -> Self {
Column {
length: gen::<usize>() % (lines - 3) + 3,
spaces: gen::<usize>() % lines + 1,
col: vec![Block::default(); lines],
}
}
fn head_is_empty(&self) -> bool {
self.col[1].val == ' '
}
fn new_rand_char(&mut self) {
self.col[0].val = rand_char();
self.col[0].color = self.col[1].color;
}
fn new_rand_head(&mut self, config: &Config) {
self.col[0].val = rand_char();
self.col[0].color = if config.rainbow {
match gen::<usize>() % 6 {
0 => COLOR_GREEN,
1 => COLOR_BLUE,
2 => COLOR_WHITE,
3 => COLOR_YELLOW,
4 => COLOR_CYAN,
5 => COLOR_MAGENTA,
_ => unreachable!(),
}
} else {
config.colour
};
self.col[0].white = coin_flip();
}
}
impl ops::Index<usize> for Column {
type Output = Block;
fn index(&self, i: usize) -> &Self::Output {
&self.col[i]
}
}
#[derive(Clone)]
pub struct Block {
val: char,
white: bool,
color: i16,
}
impl Block {
fn is_space(&self) -> bool {
self.val == ' '
}
}
impl Default for Block {
fn default() -> Self {
Block {
val: ' ',
white: false,
color: COLOR_RED,
}
}
}
pub fn finish() {
curs_set(1);
endwin();
std::process::exit(0);
}
pub fn ncurses_init() -> Window {
let window = initscr();
window.nodelay(true);
window.refresh();
noecho();
nonl();
cbreak();
curs_set(0);
if has_colors() {
start_color();
if use_default_colors() != ERR {
init_pair(COLOR_BLACK, -1, -1);
init_pair(COLOR_GREEN, COLOR_GREEN, -1);
init_pair(COLOR_WHITE, COLOR_WHITE, -1);
init_pair(COLOR_RED, COLOR_RED, -1);
init_pair(COLOR_CYAN, COLOR_CYAN, -1);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1);
init_pair(COLOR_BLUE, COLOR_BLUE, -1);
init_pair(COLOR_YELLOW, COLOR_YELLOW, -1);
} else {
init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
}
}
window
}
fn get_term_size() -> (usize, usize) {
match term_size::dimensions() {
Some((mut width, mut height)) => {
if width < 10 {
width = 10
}
if height < 10 {
height = 10
}
if width % 2 != 0 {
(height + 1, (width / 2) + 1)
} else {
(height + 1, width / 2)
}
}
None => (10, 10),
}
}
pub fn resize_window() {
endwin();
initscr();
}