rsille 2.3.1

A full feature braille code art lib
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
// IMPORTANT: the algorithm is fixed, be very careful of changing the code
// there isn't a good way to debug

use std::io::Write;
use std::{cmp, collections::HashMap};

use crossterm::{cursor::MoveToNextLine, queue, style::Print};

use crate::braille;
use crate::utils::get_pos;
use crate::{
    braille::PixelOp,
    term::is_raw_mode,
    utils::{round, RsilleErr},
};

use crate::color::{Color, Colored, ColoredChar};

/// Implement this for painting on [`Canvas`](struct.Canvas.html)
pub trait Paint: Send + 'static {
    /// Paint the object on the canvas
    fn paint<T>(&self, canvas: &mut Canvas, x: T, y: T) -> Result<(), RsilleErr>
    where
        T: Into<f64>;
}

// this is just for err: "Box<T> not impl Paint" xd
impl<T: Paint + ?Sized> Paint for Box<T> {
    fn paint<N>(&self, canvas: &mut Canvas, x: N, y: N) -> Result<(), RsilleErr>
    where
        N: Into<f64>,
    {
        canvas.paint(self, x, y)
    }
}

/// The basic canvas
///
/// Paint anything on the canvas anywhere you want.
/// Don't worry about the (x, y), the size of canvas will auto increase,
/// and it support the negative number.
///
/// ## Example
///
/// Draw the `y = 1.5*sin(x)` and `y = cos(x)`
/// ```
/// use rsille::Canvas;
/// let mut c = Canvas::new();
/// for x in 0..1000 {
///     let x = x as f64;
///     c.set(x / 10.0, x.to_radians().sin() * 15.0);
///     c.set(x / 10.0, x.to_radians().cos() * 10.0);
/// }
/// c.print();
/// ```
///
/// ## NOTE
///
/// Take a look at the [`extra`](extra/index.html) module, there are some useful things can paint on the canvas
///

#[derive(Debug, Clone)]
pub struct Canvas {
    minx: f64,                              // <= 0
    miny: f64,                              // <= 0
    width: i32,                             // >= 0
    height: i32,                            // >= 0
    pixels: HashMap<(i32, i32), Colored>,   // (col, row) -> colored
    text: HashMap<(i32, i32), ColoredChar>, // (col, row) -> colored char
}

impl Canvas {
    /// Make a new empty canvas
    ///
    /// The size of the canvas will auto increase
    pub fn new() -> Self {
        let pixels = HashMap::new();
        let text = HashMap::new();
        let (width, height) = (0, 0);
        let (minx, miny) = (0.0, 0.0);
        Self {
            minx,
            miny,
            width,
            height,
            pixels,
            text,
        }
    }

    /// Paint those [`Paint`]() object on the location (x, y)
    pub fn paint<T, N>(&mut self, target: &T, x: N, y: N) -> Result<(), RsilleErr>
    where
        T: Paint,
        N: Into<f64>,
    {
        let (x, y) = (x.into(), y.into());
        target.paint(self, x, y)?;
        Ok(())
    }

    /// Print the canvas to the terminal
    ///
    /// If you want to print the canvas to a buffer, use the [`print_on`](struct.Canvas.html#method.print_on)
    pub fn print(&self) {
        let is_raw = is_raw_mode();
        let mut stdout = std::io::stdout();
        self.print_on(&mut stdout, is_raw).unwrap();
    }

    /// Print the canvas to the buffer
    ///
    /// If you want to print the canvas to the terminal, use the [`print`](struct.Canvas.html#method.print)
    pub fn print_on<W>(&self, w: &mut W, is_raw: bool) -> Result<(), RsilleErr>
    where
        W: Write,
    {
        self.print_impl(w, is_raw).map_err(RsilleErr::to_rsille_err)
    }

    fn print_impl<W>(&self, w: &mut W, is_raw: bool) -> std::io::Result<()>
    where
        W: Write,
    {
        let (start_col, start_row) = get_pos(self.minx, self.miny);
        for row in (start_row..self.height).rev() {
            for col in start_col..self.width {
                if let Some(text) = self.text.get(&(col, row)) {
                    text.queue(w)?;
                    continue;
                }
                if let Some(pixel) = self.pixels.get(&(col, row)) {
                    pixel.queue(w)?;
                } else {
                    queue!(w, Print(braille::SPACE))?;
                }
            }
            if is_raw {
                queue!(w, MoveToNextLine(1))?;
            } else {
                queue!(w, Print("\n"))?;
            }
        }
        w.flush()?;
        Ok(())
    }

    /// Clear the canvas
    ///
    /// This method only clear those dots on the canvas, the size of the canvas will not change
    /// If you want to clear the size too, use the [`reset`](struct.Canvas.html#method.reset)
    pub fn clear(&mut self) {
        self.pixels = HashMap::new();
    }

    /// Reset the canvas to a new empty canvas
    pub fn reset(&mut self) {
        self.minx = 0.0;
        self.miny = 0.0;
        self.width = 0;
        self.height = 0;
        self.pixels = HashMap::new();
    }

    /// Set the size of the canvas
    ///
    /// This method can't fix the size of the canvas, it's just set the canvas size.
    /// When the size isn't enough, the canvas will auto increase.
    /// And the (width, height) isn't the size of the terminal, it's the size of the canvas!
    /// For example, an object `x` from -30 to 30, then it's 60 in width.
    /// On the terminal, it's 30 in width(because braille code), but you should set the width to 60 not 30.
    pub fn set_size<T>(&mut self, width: T, height: T)
    where
        T: Into<f64>,
    {
        // start_col, start_row < 0
        let (max_col, max_row) = get_pos(width.into(), height.into());
        let (start_col, start_row) = get_pos(self.minx, self.miny);
        if max_col > self.width - start_col {
            self.width = max_col + start_col;
        }
        if max_row > self.height - start_row {
            self.height = max_row + start_row;
        }
    }

    /// Set the min `x` of th canvas
    ///
    /// In most time, no need to call this, only when the animation is moved when running
    pub fn set_minx<T>(&mut self, minx: T)
    where
        T: Into<f64>,
    {
        let minx = minx.into();
        if minx < self.minx {
            self.minx = minx;
        }
    }

    /// Set the max `y` of the canvas
    ///
    /// In most time, no need to call this, only when the animation is moved when running
    pub fn set_maxy<T>(&mut self, maxy: T)
    where
        T: Into<f64> + Copy,
    {
        let maxy = maxy.into();
        let (_, max_row) = get_pos(0.0, maxy);
        if max_row > self.height {
            self.height = max_row;
        }
    }

    /// Draw a dot on (x, y)
    ///
    /// Just use the (x, y) in your object, the algorithm will find the right location
    pub fn set<T>(&mut self, x: T, y: T)
    where
        T: Into<f64> + Copy,
    {
        self.set_at(x, y, None);
    }

    /// Similar to [`set`](struct.Canvas.html#method.set)
    ///
    /// But it's support color
    pub fn set_colorful<T>(&mut self, x: T, y: T, color: Color)
    where
        T: Into<f64> + Copy,
    {
        self.set_at(x, y, Some(color));
    }

    /// If the (x, y) is already set, then unset it
    ///
    /// If the (x, y) is unset, then set it
    pub fn toggle<T>(&mut self, x: T, y: T)
    where
        T: Into<f64> + Copy,
    {
        self.toggle_at(x, y);
    }

    /// Draw a line on the canvas
    /// * `xy1` - the start location
    /// * `xy2` - the end location
    pub fn line<T>(&mut self, xy1: (T, T), xy2: (T, T))
    where
        T: Into<f64>,
    {
        let (x1, y1) = (round(xy1.0), round(xy1.1));
        let (x2, y2) = (round(xy2.0), round(xy2.1));
        let d = |v1, v2| {
            if v1 <= v2 {
                (v2 - v1, 1.0)
            } else {
                (v1 - v2, -1.0)
            }
        };

        let (xdiff, xdir) = d(x1, x2);
        let (ydiff, ydif) = d(y1, y2);
        let r = cmp::max(xdiff, ydiff);

        for i in 0..=r {
            let r = r as f64;
            let i = i as f64;
            let (xd, yd) = (xdiff as f64, ydiff as f64);
            let x = x1 as f64 + i * xd / r * xdir;
            let y = y1 as f64 + i * yd / r * ydif;
            self.set(x, y);
        }
    }

    /// Draw a line on the canvas
    /// * `xy1` - the start location
    /// * `xy2` - the end location
    /// * `c` - the char used in line
    /// * `color` - optional, the color
    ///
    /// It can draw any character on canvas,
    /// and when there are both a braille code and any char on *(x, y)*,
    /// the character will cover the braille code!
    pub fn line_any<T>(&mut self, xy1: (T, T), xy2: (T, T), c: char, color: Option<Color>)
    where
        T: Into<f64>,
    {
        let (x1, y1) = (round(xy1.0), round(xy1.1));
        let (x2, y2) = (round(xy2.0), round(xy2.1));
        let d = |v1, v2| {
            if v1 <= v2 {
                (v2 - v1, 1.0)
            } else {
                (v1 - v2, -1.0)
            }
        };

        let (xdiff, xdir) = d(x1, x2);
        let (ydiff, ydif) = d(y1, y2);
        let r = cmp::max(xdiff, ydiff);

        for i in 0..=r {
            let r = r as f64;
            let i = i as f64;
            let (xd, yd) = (xdiff as f64, ydiff as f64);
            let x = x1 as f64 + i * xd / r * xdir;
            let y = y1 as f64 + i * yd / r * ydif;
            self.put(x, y, c, color);
        }
    }

    /// Draw a line on the canvas with the color
    /// * `xy1` - the start location
    /// * `xy2` - the end location
    pub fn line_colorful<T>(&mut self, xy1: (T, T), xy2: (T, T), color: Color)
    where
        T: Into<f64> + Copy,
    {
        let (x1, y1) = (round(xy1.0), round(xy1.1));
        let (x2, y2) = (round(xy2.0), round(xy2.1));
        let d = |v1, v2| {
            if v1 <= v2 {
                (v2 - v1, 1.0)
            } else {
                (v1 - v2, -1.0)
            }
        };

        let (xdiff, xdir) = d(x1, x2);
        let (ydiff, ydif) = d(y1, y2);
        let r = cmp::max(xdiff, ydiff);

        for i in 0..=r {
            let r = r as f64;
            let i = i as f64;
            let (xd, yd) = (xdiff as f64, ydiff as f64);
            let x = x1 as f64 + i * xd / r * xdir;
            let y = y1 as f64 + i * yd / r * ydif;
            self.set_colorful(x, y, color);
        }
    }

    /// Put text on canvas
    ///
    /// It can draw any character on canvas,
    /// and when there are both a braille code and any char on *(x, y)*,
    /// the character will cover the braille code!
    pub fn put_text<T>(&mut self, x: T, y: T, text: &str, color: Option<Color>)
    where
        T: Into<f64>,
    {
        let (col, row) = self.get_pos(x, y);
        if let Some(color) = color {
            for (i, c) in text.chars().enumerate() {
                let mut c = ColoredChar::new(c);
                c.set_foregound_color(color);
                self.text.insert((col + i as i32, row), c);
            }
        } else {
            for (i, c) in text.chars().enumerate() {
                self.text.insert((col + i as i32, row), ColoredChar::new(c));
            }
        }
    }

    /// Put char on canvas
    ///
    /// It can draw any character on canvas,
    /// and when there are both a braille code and any char on *(x, y)*,
    /// the character will cover the braille code!
    pub fn put<T>(&mut self, x: T, y: T, c: char, color: Option<Color>)
    where
        T: Into<f64>,
    {
        let (col, row) = self.get_pos(x, y);
        let c = if let Some(color) = color {
            let mut c = ColoredChar::new(c);
            c.set_foregound_color(color);
            c
        } else {
            ColoredChar::new(c)
        };
        self.text.insert((col, row), c);
    }

    fn set_at<T>(&mut self, x: T, y: T, color: Option<Color>)
    where
        T: Into<f64> + Copy,
    {
        let (col, row) = self.get_pos(x, y);
        if let Some(pixel) = self.pixels.get_mut(&(col, row)) {
            pixel.set(x, y);
        } else {
            self.pixels.insert((col, row), Colored::new());
            self.pixels.get_mut(&(col, row)).unwrap().set(x, y);
        }
        if let Some(color) = color {
            self.pixels
                .get_mut(&(col, row))
                .unwrap()
                .set_foregound_color(color);
        }
    }

    fn toggle_at<T>(&mut self, x: T, y: T)
    where
        T: Into<f64> + Copy,
    {
        let (col, row) = self.get_pos(x, y);
        if let Some(pixel) = self.pixels.get_mut(&(col, row)) {
            pixel.toggle(x, y);
        } else {
            self.pixels.insert((col, row), Colored::new());
            self.pixels.get_mut(&(col, row)).unwrap().toggle(x, y);
        }
    }

    fn get_pos<T>(&mut self, x: T, y: T) -> (i32, i32)
    where
        T: Into<f64>,
    {
        let (x, y) = (x.into(), y.into());
        if x < self.minx {
            self.minx = x;
        }
        if y < self.miny {
            self.miny = y;
        }
        let (col, row) = get_pos(x, y);
        if row >= self.height {
            self.height = row.abs() + 1;
        }
        if col >= self.width {
            self.width = col.abs() + 1;
        }
        (col, row)
    }
}