envision 0.15.1

A ratatui framework for collaborative TUI development with headless testing support
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
//! Rendering functions for the BoxPlot component.
//!
//! Renders box-and-whisker plots in vertical or horizontal orientation.
//! Uses block characters to draw whiskers, boxes, and median lines.

use ratatui::prelude::*;
use ratatui::widgets::Paragraph;

use super::BoxPlotState;
use crate::theme::Theme;

/// Maps a data value to a position within a pixel range.
///
/// Returns the position (0-based) within `length` pixels that corresponds
/// to `value` in the range `[data_min, data_max]`.
fn value_to_position(value: f64, data_min: f64, data_max: f64, length: u16) -> u16 {
    if length == 0 {
        return 0;
    }
    let range = data_max - data_min;
    if range <= 0.0 {
        return length / 2;
    }
    let ratio = (value - data_min) / range;
    let pos = (ratio * (length.saturating_sub(1) as f64)).round() as u16;
    pos.min(length.saturating_sub(1))
}

/// Renders vertical box plots (values on Y axis, datasets on X axis).
pub(super) fn render_vertical(
    state: &BoxPlotState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
    disabled: bool,
) {
    let dataset_count = state.datasets().len() as u16;
    if dataset_count == 0 || area.height < 3 || area.width < 3 {
        return;
    }

    // Reserve bottom row for labels
    let label_height = 1u16;
    let chart_height = area.height.saturating_sub(label_height);
    if chart_height < 3 {
        return;
    }

    let chart_area = Rect::new(area.x, area.y, area.width, chart_height);
    let label_area = Rect::new(area.x, area.y + chart_height, area.width, label_height);

    // Calculate column width for each dataset
    let col_width = area.width / dataset_count.max(1);
    if col_width < 3 {
        // Not enough space for meaningful rendering
        return;
    }

    let data_min = state.global_min();
    let data_max = state.global_max();

    let style = if disabled {
        theme.disabled_style()
    } else {
        theme.normal_style()
    };

    // Render each box plot
    for (i, dataset) in state.datasets().iter().enumerate() {
        let col_x = area.x + (i as u16) * col_width;
        let center_x = col_x + col_width / 2;

        let box_color = if disabled {
            Color::DarkGray
        } else {
            dataset.color()
        };
        let box_style = Style::default().fg(box_color);

        let selected_indicator = if i == state.selected() && focused {
            Style::default().fg(box_color).add_modifier(Modifier::BOLD)
        } else {
            box_style
        };

        // Map values to vertical positions (inverted: top = max, bottom = min)
        let min_y = value_to_y(dataset.min(), data_min, data_max, chart_area);
        let q1_y = value_to_y(dataset.q1(), data_min, data_max, chart_area);
        let median_y = value_to_y(dataset.median(), data_min, data_max, chart_area);
        let q3_y = value_to_y(dataset.q3(), data_min, data_max, chart_area);
        let max_y = value_to_y(dataset.max(), data_min, data_max, chart_area);

        // Draw upper whisker (from max to q3)
        if max_y < q3_y {
            for y in max_y..q3_y {
                if center_x < area.x + area.width && y < chart_area.y + chart_area.height {
                    frame
                        .buffer_mut()
                        .set_string(center_x, y, "|", selected_indicator);
                }
            }
            // Whisker cap at max
            if center_x > col_x && center_x + 1 < col_x + col_width {
                frame.buffer_mut().set_string(
                    center_x.saturating_sub(1),
                    max_y,
                    "---",
                    selected_indicator,
                );
            }
        }

        // Draw the box (from q3 to q1)
        let box_left = center_x.saturating_sub(1);
        let box_right = (center_x + 1).min(col_x + col_width - 1);
        let box_width = box_right.saturating_sub(box_left) + 1;

        // Top of box (Q3 line)
        if q3_y >= chart_area.y && q3_y < chart_area.y + chart_area.height {
            let top_str: String = "-".repeat(box_width as usize);
            frame
                .buffer_mut()
                .set_string(box_left, q3_y, &top_str, selected_indicator);
        }

        // Box sides
        let box_top = q3_y + 1;
        let box_bottom = q1_y;
        for y in box_top..box_bottom {
            if y >= chart_area.y && y < chart_area.y + chart_area.height {
                if box_left >= area.x && box_left < area.x + area.width {
                    frame
                        .buffer_mut()
                        .set_string(box_left, y, "|", selected_indicator);
                }
                if box_right >= area.x && box_right < area.x + area.width {
                    frame
                        .buffer_mut()
                        .set_string(box_right, y, "|", selected_indicator);
                }
            }
        }

        // Median line
        if median_y >= chart_area.y && median_y < chart_area.y + chart_area.height {
            let median_str: String = "=".repeat(box_width as usize);
            frame
                .buffer_mut()
                .set_string(box_left, median_y, &median_str, selected_indicator);
        }

        // Bottom of box (Q1 line)
        if q1_y >= chart_area.y && q1_y < chart_area.y + chart_area.height {
            let bottom_str: String = "-".repeat(box_width as usize);
            frame
                .buffer_mut()
                .set_string(box_left, q1_y, &bottom_str, selected_indicator);
        }

        // Draw lower whisker (from q1 to min)
        if q1_y < min_y {
            for y in (q1_y + 1)..=min_y {
                if center_x < area.x + area.width && y < chart_area.y + chart_area.height {
                    frame
                        .buffer_mut()
                        .set_string(center_x, y, "|", selected_indicator);
                }
            }
            // Whisker cap at min
            if center_x > col_x
                && center_x + 1 < col_x + col_width
                && min_y < chart_area.y + chart_area.height
            {
                frame.buffer_mut().set_string(
                    center_x.saturating_sub(1),
                    min_y,
                    "---",
                    selected_indicator,
                );
            }
        }

        // Draw outliers
        if state.show_outliers() {
            for &outlier in dataset.outliers() {
                let oy = value_to_y(outlier, data_min, data_max, chart_area);
                if oy >= chart_area.y
                    && oy < chart_area.y + chart_area.height
                    && center_x < area.x + area.width
                {
                    frame.buffer_mut().set_string(center_x, oy, "*", box_style);
                }
            }
        }

        // Render label below the chart
        let label = dataset.label();
        let max_label_width = col_width as usize;
        let truncated_label = if label.len() > max_label_width {
            &label[..max_label_width]
        } else {
            label
        };
        let label_x = col_x + col_width.saturating_sub(truncated_label.len() as u16) / 2;
        if label_area.y < area.y + area.height {
            frame
                .buffer_mut()
                .set_string(label_x, label_area.y, truncated_label, style);
        }
    }
}

/// Renders horizontal box plots (values on X axis, datasets on Y axis).
pub(super) fn render_horizontal(
    state: &BoxPlotState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
    disabled: bool,
) {
    let dataset_count = state.datasets().len() as u16;
    if dataset_count == 0 || area.height < 3 || area.width < 3 {
        return;
    }

    // Reserve left column for labels
    let max_label_len = state
        .datasets()
        .iter()
        .map(|d| d.label().len())
        .max()
        .unwrap_or(0) as u16;
    let label_width = max_label_len.min(area.width / 3).max(1);
    let chart_width = area.width.saturating_sub(label_width + 1);
    if chart_width < 3 {
        return;
    }

    let chart_x = area.x + label_width + 1;

    // Calculate row height for each dataset
    let row_height = area.height / dataset_count.max(1);
    if row_height < 1 {
        return;
    }

    let data_min = state.global_min();
    let data_max = state.global_max();

    let style = if disabled {
        theme.disabled_style()
    } else {
        theme.normal_style()
    };

    for (i, dataset) in state.datasets().iter().enumerate() {
        let row_y = area.y + (i as u16) * row_height;
        let center_y = row_y + row_height / 2;

        let box_color = if disabled {
            Color::DarkGray
        } else {
            dataset.color()
        };
        let box_style = Style::default().fg(box_color);

        let selected_indicator = if i == state.selected() && focused {
            Style::default().fg(box_color).add_modifier(Modifier::BOLD)
        } else {
            box_style
        };

        // Map values to horizontal positions
        let min_x = value_to_position(dataset.min(), data_min, data_max, chart_width) + chart_x;
        let q1_x = value_to_position(dataset.q1(), data_min, data_max, chart_width) + chart_x;
        let median_x =
            value_to_position(dataset.median(), data_min, data_max, chart_width) + chart_x;
        let q3_x = value_to_position(dataset.q3(), data_min, data_max, chart_width) + chart_x;
        let max_x = value_to_position(dataset.max(), data_min, data_max, chart_width) + chart_x;

        // Draw left whisker (min to q1)
        if min_x < q1_x && center_y < area.y + area.height {
            let whisker_len = q1_x.saturating_sub(min_x);
            let whisker_str: String = "-".repeat(whisker_len as usize);
            frame
                .buffer_mut()
                .set_string(min_x, center_y, &whisker_str, selected_indicator);
            // Whisker cap at min
            if center_y > row_y && center_y < row_y + row_height {
                frame
                    .buffer_mut()
                    .set_string(min_x, center_y, "|", selected_indicator);
            }
        }

        // Draw the box (q1 to q3)
        let box_top = center_y.saturating_sub(row_height.min(3) / 2);
        let box_bottom = (center_y + row_height.min(3) / 2).min(row_y + row_height - 1);

        // Top and bottom borders of box
        if box_top >= area.y && box_top < area.y + area.height {
            let box_len = q3_x.saturating_sub(q1_x) + 1;
            let border_str: String = "-".repeat(box_len as usize);
            frame
                .buffer_mut()
                .set_string(q1_x, box_top, &border_str, selected_indicator);
        }
        if box_bottom > box_top && box_bottom >= area.y && box_bottom < area.y + area.height {
            let box_len = q3_x.saturating_sub(q1_x) + 1;
            let border_str: String = "-".repeat(box_len as usize);
            frame
                .buffer_mut()
                .set_string(q1_x, box_bottom, &border_str, selected_indicator);
        }

        // Left and right sides of box
        for y in box_top..=box_bottom {
            if y >= area.y && y < area.y + area.height {
                if q1_x < area.x + area.width {
                    frame
                        .buffer_mut()
                        .set_string(q1_x, y, "|", selected_indicator);
                }
                if q3_x < area.x + area.width {
                    frame
                        .buffer_mut()
                        .set_string(q3_x, y, "|", selected_indicator);
                }
            }
        }

        // Median line (vertical within the box)
        for y in box_top..=box_bottom {
            if y >= area.y
                && y < area.y + area.height
                && median_x < area.x + area.width
                && median_x >= q1_x
                && median_x <= q3_x
            {
                frame
                    .buffer_mut()
                    .set_string(median_x, y, "|", selected_indicator);
            }
        }

        // Draw right whisker (q3 to max)
        if q3_x < max_x && center_y < area.y + area.height {
            let whisker_start = q3_x + 1;
            let whisker_len = max_x.saturating_sub(whisker_start);
            if whisker_len > 0 {
                let whisker_str: String = "-".repeat(whisker_len as usize);
                frame.buffer_mut().set_string(
                    whisker_start,
                    center_y,
                    &whisker_str,
                    selected_indicator,
                );
            }
            // Whisker cap at max
            if max_x < area.x + area.width {
                frame
                    .buffer_mut()
                    .set_string(max_x, center_y, "|", selected_indicator);
            }
        }

        // Draw outliers
        if state.show_outliers() {
            for &outlier in dataset.outliers() {
                let ox = value_to_position(outlier, data_min, data_max, chart_width) + chart_x;
                if ox < area.x + area.width && center_y < area.y + area.height {
                    frame.buffer_mut().set_string(ox, center_y, "*", box_style);
                }
            }
        }

        // Render label on the left
        let label = dataset.label();
        let label_width_usize = label_width as usize;
        let truncated_label = if label.len() > label_width_usize {
            &label[..label_width_usize]
        } else {
            label
        };
        if center_y < area.y + area.height {
            let p = Paragraph::new(truncated_label).style(style);
            let label_rect = Rect::new(area.x, center_y, label_width, 1);
            frame.render_widget(p, label_rect);
        }
    }
}

/// Converts a data value to a Y coordinate (inverted: higher values at top).
fn value_to_y(value: f64, data_min: f64, data_max: f64, area: Rect) -> u16 {
    let height = area.height;
    if height == 0 {
        return area.y;
    }
    let range = data_max - data_min;
    if range <= 0.0 {
        return area.y + height / 2;
    }
    let ratio = (value - data_min) / range;
    // Invert: top of area = data_max, bottom = data_min
    let y_offset = ((1.0 - ratio) * (height.saturating_sub(1) as f64)).round() as u16;
    area.y + y_offset.min(height.saturating_sub(1))
}

#[cfg(test)]
mod render_tests {
    use super::*;

    #[test]
    fn test_value_to_position_basic() {
        assert_eq!(value_to_position(0.0, 0.0, 100.0, 100), 0);
        assert_eq!(value_to_position(50.0, 0.0, 100.0, 100), 50);
        assert_eq!(value_to_position(100.0, 0.0, 100.0, 100), 99);
    }

    #[test]
    fn test_value_to_position_zero_length() {
        assert_eq!(value_to_position(50.0, 0.0, 100.0, 0), 0);
    }

    #[test]
    fn test_value_to_position_zero_range() {
        assert_eq!(value_to_position(50.0, 50.0, 50.0, 100), 50);
    }

    #[test]
    fn test_value_to_y_basic() {
        let area = Rect::new(0, 0, 10, 100);
        // Max value should be at top (y = 0)
        assert_eq!(value_to_y(100.0, 0.0, 100.0, area), 0);
        // Min value should be at bottom (y = 99)
        assert_eq!(value_to_y(0.0, 0.0, 100.0, area), 99);
        // Middle value should be around middle
        let mid = value_to_y(50.0, 0.0, 100.0, area);
        assert!((49..=50).contains(&mid));
    }

    #[test]
    fn test_value_to_y_zero_height() {
        let area = Rect::new(5, 10, 10, 0);
        assert_eq!(value_to_y(50.0, 0.0, 100.0, area), 10);
    }

    #[test]
    fn test_value_to_y_zero_range() {
        let area = Rect::new(0, 0, 10, 20);
        assert_eq!(value_to_y(5.0, 5.0, 5.0, area), 10);
    }
}