revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Shape types for braille canvas drawing

use super::super::grid::Grid;
use crate::style::Color;

/// A shape that can be drawn on a braille canvas
pub trait Shape {
    /// Draw the shape onto the braille grid
    fn draw(&self, grid: &mut dyn Grid);
}

/// A line segment
#[derive(Clone, Debug)]
pub struct Line {
    /// Start X coordinate
    pub x0: f64,
    /// Start Y coordinate
    pub y0: f64,
    /// End X coordinate
    pub x1: f64,
    /// End Y coordinate
    pub y1: f64,
    /// Line color
    pub color: Color,
}

impl Line {
    /// Create a new line
    pub fn new(x0: f64, y0: f64, x1: f64, y1: f64, color: Color) -> Self {
        Self {
            x0,
            y0,
            x1,
            y1,
            color,
        }
    }
}

impl Shape for Line {
    fn draw(&self, grid: &mut dyn Grid) {
        // Bresenham's line algorithm for floating point
        let dx = (self.x1 - self.x0).abs();
        let dy = (self.y1 - self.y0).abs();
        let sx = if self.x0 < self.x1 { 1.0 } else { -1.0 };
        let sy = if self.y0 < self.y1 { 1.0 } else { -1.0 };
        let mut err = dx - dy;

        let mut x = self.x0;
        let mut y = self.y0;

        loop {
            grid.set(x as usize, y as usize, self.color);

            if (x - self.x1).abs() < 0.5 && (y - self.y1).abs() < 0.5 {
                break;
            }

            let e2 = 2.0 * err;
            if e2 > -dy {
                err -= dy;
                x += sx;
            }
            if e2 < dx {
                err += dx;
                y += sy;
            }
        }
    }
}

/// A circle
#[derive(Clone, Debug)]
pub struct Circle {
    /// Center X coordinate
    pub x: f64,
    /// Center Y coordinate
    pub y: f64,
    /// Radius
    pub radius: f64,
    /// Circle color
    pub color: Color,
}

impl Circle {
    /// Create a new circle
    pub fn new(x: f64, y: f64, radius: f64, color: Color) -> Self {
        Self {
            x,
            y,
            radius,
            color,
        }
    }
}

impl Shape for Circle {
    fn draw(&self, grid: &mut dyn Grid) {
        // Midpoint circle algorithm
        let mut x = self.radius as i32;
        let mut y = 0i32;
        let mut err = 0i32;

        while x >= y {
            // Draw 8 octants
            let points = [
                (self.x as i32 + x, self.y as i32 + y),
                (self.x as i32 + y, self.y as i32 + x),
                (self.x as i32 - y, self.y as i32 + x),
                (self.x as i32 - x, self.y as i32 + y),
                (self.x as i32 - x, self.y as i32 - y),
                (self.x as i32 - y, self.y as i32 - x),
                (self.x as i32 + y, self.y as i32 - x),
                (self.x as i32 + x, self.y as i32 - y),
            ];

            for (px, py) in points {
                if px >= 0 && py >= 0 {
                    grid.set(px as usize, py as usize, self.color);
                }
            }

            y += 1;
            err += 1 + 2 * y;
            if 2 * (err - x) + 1 > 0 {
                x -= 1;
                err += 1 - 2 * x;
            }
        }
    }
}

/// A filled circle
#[derive(Clone, Debug)]
pub struct FilledCircle {
    /// Center X coordinate
    pub x: f64,
    /// Center Y coordinate
    pub y: f64,
    /// Radius
    pub radius: f64,
    /// Fill color
    pub color: Color,
}

impl FilledCircle {
    /// Create a new filled circle
    pub fn new(x: f64, y: f64, radius: f64, color: Color) -> Self {
        Self {
            x,
            y,
            radius,
            color,
        }
    }
}

impl Shape for FilledCircle {
    fn draw(&self, grid: &mut dyn Grid) {
        let r2 = self.radius * self.radius;
        let min_x = (self.x - self.radius).max(0.0) as usize;
        let max_x = (self.x + self.radius) as usize;
        let min_y = (self.y - self.radius).max(0.0) as usize;
        let max_y = (self.y + self.radius) as usize;

        for py in min_y..=max_y {
            for px in min_x..=max_x {
                let dx = px as f64 - self.x;
                let dy = py as f64 - self.y;
                if dx * dx + dy * dy <= r2 {
                    grid.set(px, py, self.color);
                }
            }
        }
    }
}

/// An arc (portion of a circle)
#[derive(Clone, Debug)]
pub struct Arc {
    /// Center X coordinate
    pub x: f64,
    /// Center Y coordinate
    pub y: f64,
    /// Radius
    pub radius: f64,
    /// Start angle in radians (0 = right, counter-clockwise)
    pub start_angle: f64,
    /// End angle in radians
    pub end_angle: f64,
    /// Arc color
    pub color: Color,
}

impl Arc {
    /// Create a new arc
    pub fn new(
        x: f64,
        y: f64,
        radius: f64,
        start_angle: f64,
        end_angle: f64,
        color: Color,
    ) -> Self {
        Self {
            x,
            y,
            radius,
            start_angle,
            end_angle,
            color,
        }
    }

    /// Create an arc from degrees
    pub fn from_degrees(
        x: f64,
        y: f64,
        radius: f64,
        start_deg: f64,
        end_deg: f64,
        color: Color,
    ) -> Self {
        Self {
            x,
            y,
            radius,
            start_angle: start_deg.to_radians(),
            end_angle: end_deg.to_radians(),
            color,
        }
    }
}

impl Shape for Arc {
    fn draw(&self, grid: &mut dyn Grid) {
        // Normalize angles
        let start = self.start_angle;
        let mut end = self.end_angle;

        // Ensure we draw in the correct direction
        while end < start {
            end += std::f64::consts::TAU;
        }

        // Calculate number of steps based on arc length
        let arc_length = self.radius * (end - start).abs();
        let steps = (arc_length * 2.0).max(20.0) as usize;

        let step_angle = (end - start) / steps as f64;

        for i in 0..=steps {
            let angle = start + step_angle * i as f64;
            let px = self.x + self.radius * angle.cos();
            let py = self.y + self.radius * angle.sin();

            if px >= 0.0 && py >= 0.0 {
                grid.set(px as usize, py as usize, self.color);
            }
        }
    }
}

/// A polygon (closed shape with multiple vertices)
#[derive(Clone, Debug)]
pub struct Polygon {
    /// Vertices as (x, y) coordinates
    pub vertices: Vec<(f64, f64)>,
    /// Polygon color
    pub color: Color,
}

impl Polygon {
    /// Create a new polygon
    pub fn new(vertices: Vec<(f64, f64)>, color: Color) -> Self {
        Self { vertices, color }
    }

    /// Create a regular polygon
    pub fn regular(x: f64, y: f64, radius: f64, sides: usize, color: Color) -> Self {
        let mut vertices = Vec::with_capacity(sides);
        let angle_step = std::f64::consts::TAU / sides as f64;

        for i in 0..sides {
            let angle = angle_step * i as f64 - std::f64::consts::FRAC_PI_2;
            vertices.push((x + radius * angle.cos(), y + radius * angle.sin()));
        }

        Self { vertices, color }
    }
}

impl Shape for Polygon {
    fn draw(&self, grid: &mut dyn Grid) {
        if self.vertices.len() < 2 {
            return;
        }

        // Draw edges connecting consecutive vertices
        for i in 0..self.vertices.len() {
            let p0 = self.vertices[i];
            let p1 = self.vertices[(i + 1) % self.vertices.len()];
            Line::new(p0.0, p0.1, p1.0, p1.1, self.color).draw(grid);
        }
    }
}

/// A filled polygon
#[derive(Clone, Debug)]
pub struct FilledPolygon {
    /// Vertices as (x, y) coordinates
    pub vertices: Vec<(f64, f64)>,
    /// Fill color
    pub color: Color,
}

impl FilledPolygon {
    /// Create a new filled polygon
    pub fn new(vertices: Vec<(f64, f64)>, color: Color) -> Self {
        Self { vertices, color }
    }
}

impl Shape for FilledPolygon {
    fn draw(&self, grid: &mut dyn Grid) {
        if self.vertices.len() < 3 {
            return;
        }

        // Find bounding box
        let min_x = self
            .vertices
            .iter()
            .map(|(x, _)| *x)
            .fold(f64::INFINITY, f64::min);
        let max_x = self
            .vertices
            .iter()
            .map(|(x, _)| *x)
            .fold(f64::NEG_INFINITY, f64::max);
        let min_y = self
            .vertices
            .iter()
            .map(|(_, y)| *y)
            .fold(f64::INFINITY, f64::min);
        let max_y = self
            .vertices
            .iter()
            .map(|(_, y)| *y)
            .fold(f64::NEG_INFINITY, f64::max);

        // Scanline fill using ray casting
        for py in min_y.max(0.0) as usize..=max_y as usize {
            for px in min_x.max(0.0) as usize..=max_x as usize {
                if self.point_in_polygon(px as f64, py as f64) {
                    grid.set(px, py, self.color);
                }
            }
        }
    }
}

impl FilledPolygon {
    /// Check if point is inside polygon using ray casting
    fn point_in_polygon(&self, x: f64, y: f64) -> bool {
        let mut inside = false;
        let n = self.vertices.len();

        let mut j = n - 1;
        for i in 0..n {
            let (xi, yi) = self.vertices[i];
            let (xj, yj) = self.vertices[j];

            if ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
                inside = !inside;
            }
            j = i;
        }

        inside
    }
}

/// A rectangle outline
#[derive(Clone, Debug)]
pub struct Rectangle {
    /// Top-left X coordinate
    pub x: f64,
    /// Top-left Y coordinate
    pub y: f64,
    /// Width
    pub width: f64,
    /// Height
    pub height: f64,
    /// Rectangle color
    pub color: Color,
}

impl Rectangle {
    /// Create a new rectangle
    pub fn new(x: f64, y: f64, width: f64, height: f64, color: Color) -> Self {
        Self {
            x,
            y,
            width,
            height,
            color,
        }
    }
}

impl Shape for Rectangle {
    fn draw(&self, grid: &mut dyn Grid) {
        let x0 = self.x;
        let y0 = self.y;
        let x1 = self.x + self.width;
        let y1 = self.y + self.height;

        // Top edge
        Line::new(x0, y0, x1, y0, self.color).draw(grid);
        // Bottom edge
        Line::new(x0, y1, x1, y1, self.color).draw(grid);
        // Left edge
        Line::new(x0, y0, x0, y1, self.color).draw(grid);
        // Right edge
        Line::new(x1, y0, x1, y1, self.color).draw(grid);
    }
}

/// A filled rectangle
#[derive(Clone, Debug)]
pub struct FilledRectangle {
    /// Top-left X coordinate
    pub x: f64,
    /// Top-left Y coordinate
    pub y: f64,
    /// Width
    pub width: f64,
    /// Height
    pub height: f64,
    /// Fill color
    pub color: Color,
}

impl FilledRectangle {
    /// Create a new filled rectangle
    pub fn new(x: f64, y: f64, width: f64, height: f64, color: Color) -> Self {
        Self {
            x,
            y,
            width,
            height,
            color,
        }
    }
}

impl Shape for FilledRectangle {
    fn draw(&self, grid: &mut dyn Grid) {
        let x0 = self.x.max(0.0) as usize;
        let y0 = self.y.max(0.0) as usize;
        let x1 = (self.x + self.width) as usize;
        let y1 = (self.y + self.height) as usize;

        for py in y0..=y1 {
            for px in x0..=x1 {
                grid.set(px, py, self.color);
            }
        }
    }
}

/// A series of connected points (polyline)
#[derive(Clone, Debug)]
pub struct Points {
    /// Points as (x, y) coordinates
    pub coords: Vec<(f64, f64)>,
    /// Line color
    pub color: Color,
}

impl Points {
    /// Create a new points series
    pub fn new(coords: Vec<(f64, f64)>, color: Color) -> Self {
        Self { coords, color }
    }

    /// Create from x and y slices
    pub fn from_slices(xs: &[f64], ys: &[f64], color: Color) -> Self {
        let coords = xs.iter().copied().zip(ys.iter().copied()).collect();
        Self { coords, color }
    }
}

impl Shape for Points {
    fn draw(&self, grid: &mut dyn Grid) {
        // Draw connected lines between consecutive points
        for window in self.coords.windows(2) {
            if let [p0, p1] = window {
                Line::new(p0.0, p0.1, p1.0, p1.1, self.color).draw(grid);
            }
        }
    }
}