terminal_graphics_interactive 0.2.0

Keyboard and mouse input for terminal_graphics: build interactive terminal games, art and animations. Almost no unsafe.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Formula art: colour every cell from a formula, every frame.

use super::palette::Palette;
use super::stage::{run_loop, Pointer};
use crate::Display;
use std::io;

/// What a formula is told about the cell it is colouring.
///
/// `x` is the column. `y` is the row stretched by two, so one unit looks as
/// tall as one unit of `x` is wide (terminal cells are about twice as tall as
/// they are wide) and circles come out round. The mouse position and the
/// window size use the same units.
///
/// When a sketch is drawn into part of the screen (see
/// `Screen::backdrop_in`), `x` and `y` count from that part's top left, and
/// the window size and mouse position are measured from it too.
#[derive(Clone, Debug, PartialEq)]
pub struct Cell {
    /// Column of this cell.
    pub x: f32,
    /// Row of this cell, stretched so it matches `x`.
    pub y: f32,
    /// Seconds since the sketch started.
    pub t: f32,
    /// Column the mouse is over.
    pub mouse_x: f32,
    /// Row the mouse is over, stretched like `y`.
    pub mouse_y: f32,
    /// Whether the left mouse button is held.
    pub mouse_down: bool,
    /// Width of the window.
    pub width: f32,
    /// Height of the window, stretched like `y`.
    pub height: f32,
}

impl Cell {
    /// Distance from this cell to the mouse.
    pub fn dist_to_mouse(&self) -> f32 {
        ((self.x - self.mouse_x).powi(2) + (self.y - self.mouse_y).powi(2)).sqrt()
    }
}

/// Draws one frame of a sketch into `display`, with no terminal: every cell is
/// coloured from `formula`, which returns a number from -1.0 to 1.0.
///
/// [`sketch`] calls this for you every frame. Call it yourself to test a
/// formula, to draw into a `Display` you print some other way, or to measure
/// how fast a formula is.
///
/// ```
/// use terminal_graphics_interactive::{sketch_frame, Display, Palette, Pointer};
///
/// let mut display = Display::new(20, 6);
/// sketch_frame(&mut display, &Palette::ocean(), 0.0, &Pointer::default(), |cell| {
///     (cell.x * 0.3).sin()
/// });
/// assert_ne!(display.pixel(0, 0), display.pixel(5, 0));
/// ```
pub fn sketch_frame(
    display: &mut Display,
    palette: &Palette,
    t: f32,
    pointer: &Pointer,
    formula: impl FnMut(&Cell) -> f32,
) {
    let (width, height) = display.size();
    sketch_region(display, (0, 0, width, height), palette, t, pointer, formula);
}

/// Draws a sketch into the part of `display` given by `region` (`x`, `y`,
/// `width`, `height`). Parts of the region that are off the display are not
/// drawn, and a region of no size draws nothing.
pub(crate) fn sketch_region(
    display: &mut Display,
    region: (i32, i32, u32, u32),
    palette: &Palette,
    t: f32,
    pointer: &Pointer,
    mut formula: impl FnMut(&Cell) -> f32,
) {
    let (origin_x, origin_y, region_width, region_height) = region;
    let (width, height) = display.size();
    let mut cell = Cell {
        x: 0.0,
        y: 0.0,
        t,
        mouse_x: pointer.x - origin_x as f32,
        mouse_y: (pointer.y - origin_y as f32) * 2.0,
        mouse_down: pointer.left,
        width: region_width as f32,
        height: region_height as f32 * 2.0,
    };
    let left = i64::from(origin_x).max(0);
    let top = i64::from(origin_y).max(0);
    let right = (i64::from(origin_x) + i64::from(region_width)).min(i64::from(width));
    let bottom = (i64::from(origin_y) + i64::from(region_height)).min(i64::from(height));
    for row in top..bottom {
        for column in left..right {
            cell.x = (column - i64::from(origin_x)) as f32;
            cell.y = (row - i64::from(origin_y)) as f32 * 2.0;
            let (block, foreground, background) = palette.shade_signed(formula(&cell));
            display.set_pixel(column as isize, row as isize, block, foreground, background);
        }
    }
}

/// Fills the window every frame from a formula, until the user presses `q`.
pub fn sketch(formula: impl FnMut(&Cell) -> f32) -> io::Result<()> {
    sketch_with(Palette::ocean(), formula)
}

/// Like [`sketch`], with a choice of [`Palette`].
pub fn sketch_with(palette: Palette, mut formula: impl FnMut(&Cell) -> f32) -> io::Result<()> {
    run_loop(|display, t, pointer| sketch_frame(display, &palette, t, pointer, &mut formula))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ops::Range;
    use terminal_graphics::Colour;

    fn pointer(x: f32, y: f32, left: bool) -> Pointer {
        Pointer {
            x,
            y,
            left,
            ..Pointer::default()
        }
    }

    #[test]
    fn distance_to_the_mouse_is_the_straight_line_distance() {
        let cell = Cell {
            x: 3.0,
            y: 4.0,
            t: 0.0,
            mouse_x: 0.0,
            mouse_y: 0.0,
            mouse_down: false,
            width: 10.0,
            height: 10.0,
        };
        assert_eq!(cell.dist_to_mouse(), 5.0);
    }

    #[test]
    fn the_formula_runs_once_for_every_cell() {
        let mut display = Display::new(7, 3);
        let mut calls = 0;
        sketch_frame(
            &mut display,
            &Palette::ocean(),
            0.0,
            &Pointer::default(),
            |_| {
                calls += 1;
                0.0
            },
        );
        assert_eq!(calls, 21);
    }

    #[test]
    fn x_is_the_column_and_y_is_the_row_stretched_by_two() {
        let mut display = Display::new(4, 3);
        let mut seen = Vec::new();
        sketch_frame(
            &mut display,
            &Palette::ocean(),
            0.0,
            &Pointer::default(),
            |cell| {
                seen.push((cell.x, cell.y));
                0.0
            },
        );
        assert_eq!(seen.first(), Some(&(0.0, 0.0)));
        assert_eq!(seen[1], (1.0, 0.0), "columns run along a row");
        assert_eq!(seen[4], (0.0, 2.0), "the next row is two units down");
        assert_eq!(seen.last(), Some(&(3.0, 4.0)));
    }

    #[test]
    fn the_formula_is_told_the_time_the_mouse_and_the_window_size() {
        let mut display = Display::new(10, 5);
        let mut told = None;
        sketch_frame(
            &mut display,
            &Palette::ocean(),
            1.5,
            &pointer(6.0, 3.0, true),
            |cell| {
                told = Some(cell.clone());
                0.0
            },
        );
        let cell = told.unwrap();
        assert_eq!(cell.t, 1.5);
        assert_eq!(
            (cell.mouse_x, cell.mouse_y),
            (6.0, 6.0),
            "mouse y is stretched too"
        );
        assert!(cell.mouse_down);
        assert_eq!((cell.width, cell.height), (10.0, 10.0));
    }

    #[test]
    fn what_the_formula_returns_becomes_the_cell_colour() {
        let palette = Palette::fire();
        let mut display = Display::new(2, 1);
        let mut value = -1.0;
        sketch_frame(&mut display, &palette, 0.0, &Pointer::default(), |_| {
            let this = value;
            value = 1.0;
            this
        });
        for (x, expected) in [
            (0, palette.shade_signed(-1.0)),
            (1, palette.shade_signed(1.0)),
        ] {
            let pixel = display.pixel(x, 0).unwrap();
            assert_eq!(
                (pixel.character(), pixel.colour(), pixel.background()),
                expected
            );
        }
    }

    #[test]
    fn nan_and_infinite_results_are_clamped_not_a_crash() {
        let palette = Palette::ocean();
        let mut display = Display::new(3, 1);
        let mut results = [f32::NAN, f32::INFINITY, f32::NEG_INFINITY].into_iter();
        sketch_frame(&mut display, &palette, 0.0, &Pointer::default(), |_| {
            results.next().unwrap()
        });
        let shades: Vec<_> = (0..3)
            .map(|x| {
                let p = display.pixel(x, 0).unwrap();
                (p.character(), p.colour(), p.background())
            })
            .collect();
        assert_eq!(shades[0], palette.shade_signed(-1.0));
        assert_eq!(shades[1], palette.shade_signed(1.0));
        assert_eq!(shades[2], palette.shade_signed(-1.0));
    }

    #[test]
    fn a_window_with_no_cells_never_calls_the_formula() {
        let mut calls = 0;
        for (w, h) in [(0, 0), (0, 4), (4, 0)] {
            let mut display = Display::new(w, h);
            sketch_frame(
                &mut display,
                &Palette::mono(),
                0.0,
                &Pointer::default(),
                |_| {
                    calls += 1;
                    0.0
                },
            );
        }
        assert_eq!(calls, 0);
    }

    #[test]
    fn it_paints_with_the_palette_colours_only() {
        let palette = Palette::new(&[Colour::Blue, Colour::Red]);
        let mut display = Display::new(5, 5);
        sketch_frame(&mut display, &palette, 2.0, &Pointer::default(), |cell| {
            (cell.x + cell.y).sin()
        });
        for y in 0..5 {
            for x in 0..5 {
                let pixel = display.pixel(x, y).unwrap();
                assert!(palette.colours().contains(&pixel.colour()));
                assert!(palette.colours().contains(&pixel.background()));
            }
        }
    }

    /// Marks every cell with `q`, so a cell the sketch changed is one that is not `q`.
    fn mark_all(display: &mut Display) {
        let (width, height) = display.size();
        for y in 0..height as isize {
            for x in 0..width as isize {
                display.set_pixel(x, y, 'q', Colour::Red, Colour::Red);
            }
        }
    }

    /// The cells that no longer show the `q` mark, in row order.
    fn changed_cells(display: &Display) -> Vec<(isize, isize)> {
        let (width, height) = display.size();
        let mut cells = Vec::new();
        for y in 0..height as isize {
            for x in 0..width as isize {
                if display.pixel(x, y).unwrap().character() != 'q' {
                    cells.push((x, y));
                }
            }
        }
        cells
    }

    #[test]
    fn a_region_only_changes_the_cells_inside_it() {
        let mut display = Display::new(6, 4);
        mark_all(&mut display);
        sketch_region(
            &mut display,
            (1, 1, 3, 2),
            &Palette::mono(),
            0.0,
            &Pointer::default(),
            |_| 0.0,
        );
        assert_eq!(
            changed_cells(&display),
            vec![(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2)]
        );
    }

    #[test]
    fn inside_a_region_x_and_y_count_from_its_top_left() {
        let palette = Palette::fire();
        let mut display = Display::new(8, 4);
        let mut seen = Vec::new();
        sketch_region(
            &mut display,
            (2, 1, 4, 2),
            &palette,
            0.0,
            &Pointer::default(),
            |cell| {
                seen.push((cell.x, cell.y, cell.width, cell.height));
                0.0
            },
        );
        assert_eq!(seen.first(), Some(&(0.0, 0.0, 4.0, 4.0)));
        assert_eq!(seen.last(), Some(&(3.0, 2.0, 4.0, 4.0)));
    }

    #[test]
    fn the_left_column_of_a_region_is_the_darkest_shade() {
        let palette = Palette::fire();
        let mut display = Display::new(8, 3);
        sketch_region(
            &mut display,
            (4, 0, 4, 3),
            &palette,
            0.0,
            &Pointer::default(),
            |cell| cell.x / cell.width * 2.0 - 1.0,
        );
        let darkest = palette.shade_signed(-1.0);
        for row in 0..3 {
            let pixel = display.pixel(4, row).unwrap();
            assert_eq!(
                (pixel.character(), pixel.colour(), pixel.background()),
                darkest
            );
        }
    }

    #[test]
    fn a_region_hanging_off_any_edge_is_clipped_to_the_display() {
        // (display size, region, visible columns, visible rows)
        type Case = ((u32, u32), (i32, i32, u32, u32), Range<isize>, Range<isize>);
        let cases: [Case; 4] = [
            ((4, 3), (-2, -1, 5, 3), 0..3, 0..2),
            ((4, 3), (2, 0, 5, 3), 2..4, 0..3),
            ((3, 3), (0, 1, 2, 10), 0..2, 1..3),
            ((3, 3), (1, -5, 1, 7), 1..2, 0..2),
        ];
        for ((width, height), region, columns, rows) in cases {
            let mut display = Display::new(width, height);
            mark_all(&mut display);
            sketch_region(
                &mut display,
                region,
                &Palette::mono(),
                0.0,
                &Pointer::default(),
                |_| 0.0,
            );
            let expected: Vec<(isize, isize)> = rows
                .flat_map(|y| columns.clone().map(move |x| (x, y)))
                .collect();
            assert_eq!(changed_cells(&display), expected, "region {region:?}");
        }
    }

    #[test]
    fn an_empty_or_off_screen_region_never_calls_the_formula() {
        let regions = [
            (1, 1, 0, 3),
            (1, 1, 3, 0),
            (50, 0, 3, 3),
            (0, 50, 3, 3),
            (-50, -50, 3, 3),
            (i32::MAX, i32::MAX, u32::MAX, u32::MAX),
        ];
        for region in regions {
            let mut display = Display::new(3, 3);
            let mut calls = 0;
            sketch_region(
                &mut display,
                region,
                &Palette::mono(),
                0.0,
                &Pointer::default(),
                |_| {
                    calls += 1;
                    0.0
                },
            );
            assert_eq!(calls, 0, "region {region:?}");
        }
    }

    #[test]
    fn a_region_with_extreme_coordinates_clips_without_overflow() {
        // from i32::MIN, u32::MAX wide reaches past the far edge of a 3 by 3 display
        let mut display = Display::new(3, 3);
        mark_all(&mut display);
        sketch_region(
            &mut display,
            (i32::MIN, i32::MIN, u32::MAX, u32::MAX),
            &Palette::mono(),
            0.0,
            &Pointer::default(),
            |_| 0.0,
        );
        assert_eq!(changed_cells(&display).len(), 9);
    }

    #[test]
    fn the_pointer_is_measured_from_the_regions_top_left() {
        let mut display = Display::new(10, 6);
        let mut mouse = None;
        sketch_region(
            &mut display,
            (2, 1, 5, 4),
            &Palette::ocean(),
            0.0,
            &pointer(6.0, 3.0, false),
            |cell| {
                mouse.get_or_insert((cell.mouse_x, cell.mouse_y));
                0.0
            },
        );
        assert_eq!(
            mouse,
            Some((4.0, 4.0)),
            "pointer (6, 3) is (4, 2) in a region at (2, 1), and y is stretched"
        );
    }

    /// The whole-display loop as it was before regions existed, kept here so the
    /// refactor is checked against the old output, not against itself.
    fn legacy_full_frame(
        display: &mut Display,
        palette: &Palette,
        t: f32,
        pointer: &Pointer,
        mut formula: impl FnMut(&Cell) -> f32,
    ) {
        let (width, height) = display.size();
        let mut cell = Cell {
            x: 0.0,
            y: 0.0,
            t,
            mouse_x: pointer.x,
            mouse_y: pointer.y * 2.0,
            mouse_down: pointer.left,
            width: width as f32,
            height: height as f32 * 2.0,
        };
        for row in 0..height {
            for column in 0..width {
                cell.x = column as f32;
                cell.y = row as f32 * 2.0;
                let (block, foreground, background) = palette.shade_signed(formula(&cell));
                display.set_pixel(column as isize, row as isize, block, foreground, background);
            }
        }
    }

    #[test]
    fn sketch_frame_draws_exactly_what_it_drew_before_regions() {
        let palette = Palette::fire();
        let pointer = pointer(4.0, 2.0, true);
        let formula = |cell: &Cell| {
            (cell.x * 0.7 + cell.y * 0.3 + cell.t).sin() * (1.0 - cell.dist_to_mouse() * 0.05)
        };
        let mut old = Display::new(13, 7);
        legacy_full_frame(&mut old, &palette, 1.25, &pointer, formula);
        let mut frame = Display::new(13, 7);
        sketch_frame(&mut frame, &palette, 1.25, &pointer, formula);
        let mut region = Display::new(13, 7);
        sketch_region(
            &mut region,
            (0, 0, 13, 7),
            &palette,
            1.25,
            &pointer,
            formula,
        );
        assert_eq!(frame.render(), old.render());
        assert_eq!(region.render(), old.render());
    }
}