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
mod message;
use crate::message::MessageQueue;
pub use crate::message::{Message, ColorString, ColorChar};
mod streak;
use crate::streak::Streak;
use ncurses::*;
extern crate rand;
use rand::Rng;
use std::sync::mpsc::{self, TryRecvError};
use std::sync::{Arc, Weak};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
pub fn init(){
initscr();
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE);
timeout(0);
start_color();
}
struct ThreadMsg {
title: String,
content: Option<Message>,
}
impl ThreadMsg {
pub fn new(title: &str, content: Option<Message>) -> Self {
Self{title: title.to_string(), content}
}
}
struct ForkedScene {
columns: Vec<Column>,
height: i32,
queue: MessageQueue,
background: attr_t,
max_padding: i32,
rx: Option<std::sync::mpsc::Receiver<ThreadMsg>>,
started: bool,
}
impl ForkedScene {
pub fn new(max_padding: i32, background: i16, is_closed: bool, rx: std::sync::mpsc::Receiver<ThreadMsg>) -> Self {
let bkg_attr = Self::init(background);
let mut height : i32 = 0;
let mut width : i32 = 0;
getmaxyx(stdscr(), &mut height, &mut width);
if width == -1 {
panic!("Could not get screen size!");
}
let mut columns = Vec::new();
for _ in 0..width {
columns.push(Column::new());
}
Self{columns, height, queue: MessageQueue::new(width as usize, is_closed), background: bkg_attr, max_padding, rx: Some(rx), started: false}
}
fn init(background: i16) -> attr_t {
let background_attr = 99;
init_pair(background_attr, COLOR_BLACK, background);
bkgd(' ' as chtype | COLOR_PAIR(background_attr) as chtype);
COLOR_PAIR(background_attr)
}
pub fn kill(&self){
endwin();
}
pub fn update(&mut self) -> bool {
if self.rx.is_none() {
return false;
}
let rx = self.rx.as_ref().unwrap();
match rx.try_recv() {
Err(TryRecvError::Disconnected) => {
self.kill();
return false;
}
Ok(thread_msg) => {
match thread_msg.title.as_str() {
"start" => {
if self.started == true {
panic!("Tried to start screen twice!");
} else {
self.started = true;
}
},
"push" => self.push(thread_msg.content.unwrap()),
"push_update" => self.push_update(thread_msg.content.unwrap()),
"kill" => {
self.kill();
return false;
},
&_ => panic!("Invalid command {:}"),
}
}
Err(TryRecvError::Empty) => {}
}
if self.started {
self.advance();
refresh();
}
true
}
pub fn push(&mut self, message: Message){
self.queue.push(message);
}
pub fn push_update(&mut self, message: Message){
self.queue.push_update(message);
}
pub fn advance(&mut self){
let mut rng = rand::thread_rng();
let untouched = self.columns.iter().fold(0, |sum, column| sum + if column.touched {0} else {1});
for (i, column) in self.columns.iter_mut().enumerate() {
for streak in &mut column.streaks {
streak.derender(self.background);
streak.advance();
}
let height = self.height;
column.streaks.retain(|streak| !streak.finished(height));
if column.streaks.len()==0 || column.streaks.iter().all(|streak| streak.top_space() > 5) {
if (!column.touched && rng.gen_range(0, untouched) == 0) || column.touched {
column.add_streak(Streak::new_with_queue(&mut self.queue, i as i32, rng.gen_range(5, self.height*2), self.height, self.max_padding));
}
}
for streak in &mut column.streaks {
streak.render(self.height);
}
}
}
}
pub struct Scene {
tx: Option<std::sync::mpsc::Sender<ThreadMsg>>,
join_handle: Option<JoinHandle<()>>,
thread_control: Option<Weak<AtomicBool>>,
}
impl Scene {
pub fn new(max_padding: i32, background: i16, is_closed: bool) -> Self {
let (tx, rx) = mpsc::channel();
let mut background = ForkedScene::new(max_padding, background, is_closed, rx);
let working = Arc::new(AtomicBool::new(true));
let control = Arc::downgrade(&working);
let join_handle = thread::spawn(move ||
while (*working).load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(25));
if !background.update() {
break;
}
if getch() == 113 {
background.kill();
break;
}
});
Self{tx: Some(tx), join_handle: Some(join_handle), thread_control: Some(control)}
}
pub fn push(&mut self, message: Message){
if !self.alive() {
return;
}
let _ = self.tx.as_ref().unwrap().send(ThreadMsg::new("push", Some(message)));
}
pub fn push_update(&mut self, message: Message){
if !self.alive() {
return;
}
let _ = self.tx.as_ref().unwrap().send(ThreadMsg::new("push_update", Some(message)));
}
pub fn append(&mut self, messages: Vec<Message>){
if !self.alive() {
return;
}
for message in messages {
self.push(message);
}
}
pub fn append_update(&mut self, messages: Vec<Message>){
if !self.alive() {
return;
}
for message in messages {
self.push_update(message);
}
}
pub fn start(&mut self){
refresh();
let _ = self.tx.as_ref().unwrap().send(ThreadMsg::new("start", None));
}
pub fn alive(&self) -> bool {
self.thread_control.is_some() && self.thread_control.as_ref().unwrap().upgrade().is_some()
}
pub fn kill(&mut self){
if !self.alive() {
return;
}
let _ = self.tx.as_ref().unwrap().send(ThreadMsg::new("kill", None));
let _ = self.join_handle.take().unwrap().join();
self.join_handle = None;
self.tx = None;
}
pub fn join(&self) {
while self.alive() {}
}
}
struct Column {
streaks: Vec<Streak>,
touched: bool,
}
impl Column {
fn new() -> Self {
Self{streaks: Vec::new(), touched: false}
}
fn add_streak(&mut self, streak: Streak){
self.streaks.push(streak);
self.touched = true;
}
}
#[cfg(test)]
mod tests;