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
//! Time Series widget rendering

use super::types::{TimeLineStyle, TimeSeriesData};
use super::TimeSeries;
use crate::render::Cell;
use crate::utils::{char_width, display_width};
use crate::widget::traits::{RenderContext, View};

impl View for TimeSeries {
    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let height = self.height.unwrap_or(area.height);

        if area.width < 10 || height < 5 {
            return;
        }

        let mut current_y = 0u16;

        // Background
        if let Some(bg) = self.bg_color {
            for y in 0..height.min(area.height) {
                for x in 0..area.width {
                    let mut cell = Cell::new(' ');
                    cell.bg = Some(bg);
                    ctx.set(x, y, cell);
                }
            }
        }

        // Title
        if let Some(ref title) = self.title {
            let title_x = (area.width.saturating_sub(display_width(title) as u16)) / 2;
            let mut dx: u16 = 0;
            for ch in title.chars() {
                let x = title_x + dx;
                if x < area.width {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(crate::style::Color::WHITE);
                    cell.bg = self.bg_color;
                    ctx.set(x, current_y, cell);
                }
                dx += char_width(ch) as u16;
            }
            current_y += 1;
        }

        // Legend
        if self.show_legend && !self.series.is_empty() {
            let mut x = 2u16;
            for series in &self.series {
                let marker = match series.line_style {
                    TimeLineStyle::Solid => "",
                    TimeLineStyle::Dashed => "",
                    TimeLineStyle::Dotted => "·",
                    TimeLineStyle::Step => "",
                };
                for ch in marker.chars() {
                    if x < area.width {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(series.color);
                        cell.bg = self.bg_color;
                        ctx.set(x, current_y, cell);
                    }
                    x += 1;
                }
                x += 1;
                for ch in series.name.chars() {
                    if x < area.width {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(crate::style::Color::WHITE);
                        cell.bg = self.bg_color;
                        ctx.set(x, current_y, cell);
                    }
                    x += 1;
                }
                x += 3;
            }
            current_y += 1;
        }

        let y_label_width = 8u16;
        let plot_x = y_label_width;
        let plot_width = area.width.saturating_sub(y_label_width + 1);
        let plot_y = current_y;
        let plot_height = height.saturating_sub(current_y + 2);

        if plot_width < 5 || plot_height < 3 {
            return;
        }

        let (time_min, time_max) = self.get_time_bounds();
        let (val_min, val_max) = self.get_value_bounds();
        let time_range = time_max.saturating_sub(time_min);
        let val_range = val_max - val_min;

        // Draw grid
        if self.show_grid {
            let grid_rows = 4.min(plot_height as usize);
            for i in 0..=grid_rows {
                let y = plot_y + (i * plot_height as usize / grid_rows) as u16;
                for x in plot_x..plot_x + plot_width {
                    let ch = if i == grid_rows { '' } else { '' };
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.grid_color);
                    ctx.set(x, y, cell);
                }

                // Y-axis labels
                let val = val_max - (i as f64 * val_range / grid_rows as f64);
                let label = if val.abs() >= 1000.0 {
                    format!("{:.1}k", val / 1000.0)
                } else if val.abs() >= 1.0 {
                    format!("{:.1}", val)
                } else {
                    format!("{:.2}", val)
                };
                let label_x = y_label_width.saturating_sub(display_width(&label) as u16 + 1);
                let mut dx: u16 = 0;
                for ch in label.chars() {
                    let x = label_x + dx;
                    if x < area.width {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(crate::style::Color::WHITE);
                        cell.bg = self.bg_color;
                        ctx.set(x, y, cell);
                    }
                    dx += char_width(ch) as u16;
                }
            }
        }

        // Draw markers
        for marker in &self.markers {
            if marker.timestamp >= time_min && marker.timestamp <= time_max && time_range > 0 {
                let x_pos = ((marker.timestamp - time_min) as f64 / time_range as f64
                    * (plot_width - 1) as f64) as u16;
                let x = plot_x + x_pos;

                match marker.style {
                    super::types::MarkerStyle::Line => {
                        for y in plot_y..plot_y + plot_height {
                            let mut cell = Cell::new('');
                            cell.fg = Some(marker.color);
                            ctx.set(x, y, cell);
                        }
                    }
                    super::types::MarkerStyle::Point | super::types::MarkerStyle::Region => {
                        let mut cell = Cell::new('');
                        cell.fg = Some(marker.color);
                        ctx.set(x, plot_y, cell);
                    }
                }

                if !marker.label.is_empty() {
                    let label_x = x.saturating_sub(display_width(&marker.label) as u16 / 2);
                    let mut dx: u16 = 0;
                    for ch in marker.label.chars() {
                        let lx = label_x + dx;
                        if lx < area.width {
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(marker.color);
                            cell.bg = self.bg_color;
                            ctx.set(lx, plot_y + plot_height + 1, cell);
                        }
                        dx += char_width(ch) as u16;
                    }
                }
            }
        }

        // Draw series
        self.render_series(
            ctx,
            plot_x,
            plot_y,
            plot_width,
            plot_height,
            time_min,
            time_max,
            time_range,
            val_min,
            val_range,
        );

        // X-axis time labels
        let x_label_y = plot_y + plot_height + 1;
        if x_label_y < height {
            let num_labels = (plot_width / 12).max(2) as usize;
            for i in 0..num_labels {
                let ratio = i as f64 / (num_labels - 1) as f64;
                let ts = time_min + (ratio * time_range as f64) as u64;
                let label = self.format_time(ts, time_range);
                let x = plot_x + (ratio * (plot_width - 1) as f64) as u16;
                let label_x = x.saturating_sub(display_width(&label) as u16 / 2);
                let mut dx: u16 = 0;
                for ch in label.chars() {
                    let lx = label_x + dx;
                    if lx < area.width {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(crate::style::Color::WHITE);
                        cell.bg = self.bg_color;
                        ctx.set(lx, x_label_y, cell);
                    }
                    dx += char_width(ch) as u16;
                }
            }
        }
    }

    crate::impl_view_meta!("TimeSeries");
}

impl TimeSeries {
    #[allow(clippy::too_many_arguments)]
    /// Render data series
    fn render_series(
        &self,
        ctx: &mut RenderContext,
        plot_x: u16,
        plot_y: u16,
        plot_width: u16,
        plot_height: u16,
        time_min: u64,
        time_max: u64,
        time_range: u64,
        val_min: f64,
        val_range: f64,
    ) {
        for series in &self.series {
            let filtered_points: Vec<_> = series
                .points
                .iter()
                .filter(|p| p.timestamp >= time_min && p.timestamp <= time_max)
                .collect();

            if filtered_points.is_empty() {
                continue;
            }

            // Map points to screen coordinates
            let screen_points: Vec<(u16, u16)> = filtered_points
                .iter()
                .map(|p| {
                    let x_ratio = if time_range > 0 {
                        (p.timestamp - time_min) as f64 / time_range as f64
                    } else {
                        0.5
                    };
                    let y_ratio = if val_range > 0.0 {
                        (p.value - val_min) / val_range
                    } else {
                        0.5
                    };

                    let x = plot_x + (x_ratio * (plot_width - 1) as f64) as u16;
                    let y = plot_y + plot_height - 1 - (y_ratio * (plot_height - 1) as f64) as u16;
                    (x, y)
                })
                .collect();

            // Draw lines between points
            for i in 0..screen_points.len().saturating_sub(1) {
                let (x1, y1) = screen_points[i];
                let (x2, y2) = screen_points[i + 1];

                match series.line_style {
                    TimeLineStyle::Step => {
                        self.draw_step_line(ctx, x1, y1, x2, y2, series.color);
                    }
                    _ => {
                        self.draw_line(ctx, x1, y1, x2, y2, series);
                    }
                }

                // Fill area if enabled
                if series.fill {
                    self.fill_area(ctx, x1, y1, x2, y2, plot_y, plot_height, series.color);
                }
            }

            // Draw points
            for &(x, y) in &screen_points {
                let mut cell = Cell::new('');
                cell.fg = Some(series.color);
                ctx.set(x, y, cell);
            }
        }
    }

    /// Draw a step line between two points
    fn draw_step_line(
        &self,
        ctx: &mut RenderContext,
        x1: u16,
        y1: u16,
        x2: u16,
        y2: u16,
        color: crate::style::Color,
    ) {
        for x in x1..=x2 {
            let mut cell = Cell::new('');
            cell.fg = Some(color);
            ctx.set(x, y1, cell);
        }
        let (start_y, end_y) = if y1 <= y2 { (y1, y2) } else { (y2, y1) };
        for y in start_y..=end_y {
            let mut cell = Cell::new('');
            cell.fg = Some(color);
            ctx.set(x2, y, cell);
        }
    }

    /// Draw a line between two points using Bresenham's algorithm
    fn draw_line(
        &self,
        ctx: &mut RenderContext,
        x1: u16,
        y1: u16,
        x2: u16,
        y2: u16,
        series: &TimeSeriesData,
    ) {
        let dx = (x2 as i32 - x1 as i32).abs();
        let dy = (y2 as i32 - y1 as i32).abs();
        let sx = if x1 < x2 { 1i32 } else { -1i32 };
        let sy = if y1 < y2 { 1i32 } else { -1i32 };
        let mut err = dx - dy;
        let mut x = x1 as i32;
        let mut y = y1 as i32;
        let mut step = 0;

        loop {
            let ch = match series.line_style {
                TimeLineStyle::Solid => {
                    if dx > dy {
                        ''
                    } else {
                        ''
                    }
                }
                TimeLineStyle::Dashed => {
                    if step % 2 == 0 {
                        if dx > dy {
                            ''
                        } else {
                            ''
                        }
                    } else {
                        ' '
                    }
                }
                TimeLineStyle::Dotted => {
                    if step % 2 == 0 {
                        '·'
                    } else {
                        ' '
                    }
                }
                TimeLineStyle::Step => '',
            };

            if ch != ' ' {
                let mut cell = Cell::new(ch);
                cell.fg = Some(series.color);
                ctx.set(x as u16, y as u16, cell);
            }

            if x == x2 as i32 && y == y2 as i32 {
                break;
            }

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

    #[allow(clippy::too_many_arguments)]
    /// Fill area under a line segment
    fn fill_area(
        &self,
        ctx: &mut RenderContext,
        x1: u16,
        y1: u16,
        x2: u16,
        y2: u16,
        plot_y: u16,
        plot_height: u16,
        color: crate::style::Color,
    ) {
        let bottom_y = plot_y + plot_height - 1;
        for x in x1..=x2 {
            let y_at_x = if x2 != x1 {
                let t = (x - x1) as f64 / (x2 - x1) as f64;
                (y1 as f64 + t * (y2 as f64 - y1 as f64)) as u16
            } else {
                y1
            };
            for y in y_at_x..=bottom_y {
                let fill_color = crate::style::Color::rgb(
                    (color.r as u16 * 3 / 10) as u8,
                    (color.g as u16 * 3 / 10) as u8,
                    (color.b as u16 * 3 / 10) as u8,
                );
                let mut cell = Cell::new(' ');
                cell.bg = Some(fill_color);
                ctx.set(x, y, cell);
            }
        }
    }
}