rmatrix 0.2.0

Matrix like animation running in terminal
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::{
    mem,
    ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
    path::PathBuf,
};

use app::Speed;
use bon::bon;
use clap::Parser;
use crossterm::style::Color;
use rand::{
    Rng,
    distr::{Alphanumeric, Distribution},
};

pub mod app;
pub mod term;

#[derive(Debug)]
pub struct Matrix<R: Rng> {
    descriptor: MatrixDescriptor,
    gap: u16,
    rng: R,
    cols: Vec<Column>,
}

#[bon]
impl<R: Rng> Matrix<R> {
    #[builder]
    pub fn new(
        mut rng: R,
        #[builder(default = 80)] width: u16,
        #[builder(default = 24)] height: u16,
        #[builder(default = 1)] gap: u16,
        #[builder(into, default = SequenceHeightBounds::RangeFull)]
        sequence_height_bounds: SequenceHeightBounds,
        #[builder(default = 0.05)] sequence_probability: f64,
    ) -> Self {
        let descriptor = MatrixDescriptor {
            width,
            height,
            sequence_height_bounds,
            sequence_probability,
        };

        let cols: Vec<_> = (0..width)
            .step_by(usize::from(gap) + 1)
            .map(|x| Column::new(&mut rng, x, &descriptor))
            .collect();

        Self {
            descriptor,
            gap,
            rng,
            cols,
        }
    }

    pub fn update(&mut self) {
        for col in &mut self.cols {
            col.update(&mut self.rng, &self.descriptor);
        }
    }

    pub fn chars(&self) -> impl Iterator<Item = ([u16; 2], CharType, char)> {
        self.cols.iter().flat_map(|col| col.chars())
    }

    pub fn resize(&mut self, width: u16, height: u16) {
        let prev_width = self.descriptor.height;

        self.descriptor.width = width;
        self.descriptor.height = height;

        self.cols = mem::take(&mut self.cols)
            .into_iter()
            .filter(|col| col.x < width)
            .chain(
                ((prev_width + 1)..width)
                    .step_by(usize::from(self.gap))
                    .map(|x| Column::new(&mut self.rng, x, &self.descriptor)),
            )
            .collect();
    }
}

#[derive(Debug)]
struct MatrixDescriptor {
    #[allow(unused)]
    width: u16,
    height: u16,
    sequence_height_bounds: SequenceHeightBounds,
    sequence_probability: f64,
}

#[derive(Debug)]
struct Column {
    x: u16,
    seqs: Vec<Sequence>,
}

impl Column {
    fn new(mut rng: impl Rng, x: u16, descriptor: &MatrixDescriptor) -> Self {
        Self {
            x,
            seqs: rng
                .random_bool(descriptor.sequence_probability)
                .then(|| Sequence::new(rng, descriptor))
                .into_iter()
                .collect(),
        }
    }

    fn update(&mut self, mut rng: impl Rng, descriptor: &MatrixDescriptor) {
        self.seqs = mem::take(&mut self.seqs)
            .into_iter()
            .map(|mut seq| {
                seq.update();
                seq
            })
            .filter(|seq| seq.offset < i32::from(descriptor.height))
            .collect();

        if self.seqs.iter().all(|seq| seq.offset > 1)
            && rng.random_bool(descriptor.sequence_probability)
        {
            self.seqs.push(Sequence::new(rng, descriptor));
        }
    }

    fn chars(&self) -> impl Iterator<Item = ([u16; 2], CharType, char)> {
        self.seqs
            .iter()
            .flat_map(|seq| seq.chars().map(|(y, ty, ch)| ([self.x, y], ty, ch)))
    }
}

#[derive(Debug)]
struct Sequence {
    offset: i32,
    height: u16,
    chars: Vec<char>,
}

impl Sequence {
    fn new(mut rng: impl Rng, descriptor: &MatrixDescriptor) -> Self {
        let chars = Alphanumeric
            .sample_iter(&mut rng)
            .take(usize::from(descriptor.height))
            .map(char::from)
            .collect();

        let height = descriptor
            .sequence_height_bounds
            .sample(&mut rng, descriptor.height);

        Sequence {
            offset: -i32::from(height),
            height,
            chars,
        }
    }

    fn update(&mut self) {
        self.offset += 1;
    }

    fn chars(&self) -> impl Iterator<Item = (u16, CharType, char)> {
        let last_visible_y = self.offset + i32::from(self.height) - 1;

        (self.offset..(self.offset + i32::from(self.height)))
            .filter(|&y| y >= 0 && (y as usize) < self.chars.len())
            .map(move |y| {
                (
                    y as u16,
                    if y == last_visible_y {
                        CharType::Head
                    } else {
                        CharType::Tail
                    },
                    self.chars[y as usize],
                )
            })
    }
}

#[derive(Debug)]
pub enum SequenceHeightBounds {
    Range(Range<u16>),
    RangeInclusive(RangeInclusive<u16>),
    RangeFrom(RangeFrom<u16>),
    RangeTo(RangeTo<u16>),
    RangeToInclusive(RangeToInclusive<u16>),
    RangeFull,
}

impl SequenceHeightBounds {
    fn sample(&self, mut rng: impl Rng, max_height: u16) -> u16 {
        match self {
            Self::Range(range) => rng.random_range(range.clone()),
            Self::RangeInclusive(range) => rng.random_range(range.clone()),
            Self::RangeFrom(range) => rng.random_range(range.start..max_height),
            Self::RangeTo(range) => rng.random_range(0..range.end),
            Self::RangeToInclusive(range) => rng.random_range(0..=range.end),
            Self::RangeFull => rng.random_range(0..max_height),
        }
    }
}

impl From<Range<u16>> for SequenceHeightBounds {
    fn from(range: Range<u16>) -> Self {
        Self::Range(range)
    }
}

impl From<RangeInclusive<u16>> for SequenceHeightBounds {
    fn from(range: RangeInclusive<u16>) -> Self {
        Self::RangeInclusive(range)
    }
}

impl From<RangeFrom<u16>> for SequenceHeightBounds {
    fn from(range: RangeFrom<u16>) -> Self {
        Self::RangeFrom(range)
    }
}

impl From<RangeTo<u16>> for SequenceHeightBounds {
    fn from(range: RangeTo<u16>) -> Self {
        Self::RangeTo(range)
    }
}

impl From<RangeToInclusive<u16>> for SequenceHeightBounds {
    fn from(range: RangeToInclusive<u16>) -> Self {
        Self::RangeToInclusive(range)
    }
}

impl From<RangeFull> for SequenceHeightBounds {
    fn from(_: RangeFull) -> Self {
        Self::RangeFull
    }
}

/// Describes where the character is in the sequence.
#[derive(Debug, Hash, PartialEq, Eq)]
pub enum CharType {
    /// Character the most bottom character in the sequence.
    Head,

    /// Character isn't [`CharType::Head`].
    Tail,
}

#[derive(Debug, Parser)]
#[clap(about, version, author)]
pub struct Cli {
    #[arg(
        long,
        value_parser = parse_color,
        help = "Color of the first character in a falling sequence"
    )]
    pub head_color: Option<Color>,

    #[arg(
        long,
        value_parser = parse_color,
        help = "Color of the tail characters in a falling sequence"
    )]
    pub tail_color: Option<Color>,

    #[arg(short, long, help = "Speed of the falling sequences")]
    pub speed: Option<Speed>,

    #[arg(short, long, help = "Path to a log file")]
    pub log_file: Option<PathBuf>,
}

fn parse_color(s: &str) -> Result<Color, serde_plain::Error> {
    serde_plain::from_str(s)
}

#[cfg(test)]
mod tests {
    mod sequence {
        use crate::{CharType, MatrixDescriptor, Sequence, SequenceHeightBounds};

        #[test]
        fn test_samples_correct_number_of_chars() {
            const HEIGHTS: &[u16] = &[10, 16, 24, u16::MAX];

            let mut rng = rand::rng();
            for height in HEIGHTS.iter().copied() {
                let sequence = Sequence::new(
                    &mut rng,
                    &MatrixDescriptor {
                        width: 80,
                        height,
                        sequence_height_bounds: SequenceHeightBounds::RangeFull,
                        sequence_probability: 1.0,
                    },
                );
                assert_eq!(sequence.chars.len(), usize::from(height));
            }
        }

        #[test]
        fn test_starts_above_the_matrix() {
            let mut rng = rand::rng();

            let sequence = Sequence::new(
                &mut rng,
                &MatrixDescriptor {
                    width: 80,
                    height: 24,
                    sequence_height_bounds: (5..6).into(),
                    sequence_probability: 1.0,
                },
            );

            assert_eq!(sequence.offset, -5);
        }

        #[test]
        fn test_has_no_chars_when_above_the_matrix() {
            let mut rng = rand::rng();

            let descriptor = MatrixDescriptor {
                width: 80,
                height: 24,
                sequence_height_bounds: (5..16).into(),
                sequence_probability: 1.0,
            };

            let sequence = Sequence::new(&mut rng, &descriptor);

            assert_eq!(sequence.chars().count(), 0);
        }

        #[test]
        fn test_only_last_visible_char_is_head() {
            let mut rng = rand::rng();
            let descriptor = MatrixDescriptor {
                width: 80,
                height: 24,
                sequence_height_bounds: (5..6).into(),
                sequence_probability: 1.0,
            };

            let mut sequence = Sequence::new(&mut rng, &descriptor);

            // Move sequence partially into view
            sequence.offset = -2;

            let chars: Vec<_> = sequence.chars().collect();

            // Verify we have the expected number of visible characters
            assert_eq!(chars.len(), 3);

            // Check that only the last character is Head
            assert!(
                chars
                    .iter()
                    .take(2)
                    .all(|(_, ty, _)| matches!(ty, CharType::Tail)),
                "All characters except last should be Tail"
            );
            assert!(
                matches!(chars.last().unwrap().1, CharType::Head),
                "Last character should be Head"
            );
        }
    }

    mod column {
        use std::collections::HashSet;

        use crate::{Column, MatrixDescriptor};
        use itertools::Itertools;
        use rand::rngs::mock::StepRng;

        #[test]
        fn test_sequences_never_overlap() {
            let mut rng = StepRng::new(0, u64::MAX);
            let descriptor = MatrixDescriptor {
                width: 80,
                height: 24,
                sequence_height_bounds: (5..6).into(),
                sequence_probability: 1.0,
            };

            let mut col = Column::new(&mut rng, 0, &descriptor);
            for _ in 0..10 {
                col.update(&mut rng, &descriptor);
            }

            let no_sequences_intersect = col
                .seqs
                .iter()
                .map(|seq| (seq.offset..i32::from(seq.height)).collect::<HashSet<_>>())
                .tuple_combinations()
                .all(|(a, b)| a.intersection(&b).count() == 0);

            assert!(no_sequences_intersect)
        }
    }
}