retroglyph-widgets 0.2.1

Immediate-mode drawing helpers for retroglyph (panels, gauges, tables, sparklines)
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
//! [`Table`]: a fixed-column, scrollable table with a highlighted row.
use retroglyph_core::{Backend, Color, Rect, Style, Terminal};

use super::StatefulWidget;
use super::window::visible_window;
use crate::ListState;
use crate::Theme;
use crate::draw::fill_rect;
use crate::text::truncate as truncate_to_cols;

/// A fixed-column, scrollable table with a [`ListState`]-driven highlighted
/// row.
///
/// `headers` render on the first row of the area it's rendered into;
/// `rows` follow, one per line, clipped to that area. `widths` gives each
/// column's cell width; columns are space-separated and truncated to fit.
///
/// `state.offset()` is the index of the first row drawn below the header --
/// rendering draws whatever window `offset` names and does not clamp or
/// auto-scroll it, matching [`ListState`]'s existing "only the caller knows
/// the viewport height" design. Call
/// [`state.ensure_visible(visible_row_count)`](ListState::ensure_visible)
/// before rendering to keep `state.selected()` on-screen. If `selected()` is
/// `Some` and its row falls within the visible window, that row is drawn
/// with an inverted highlight background; if it has scrolled out of view,
/// no row is highlighted.
///
/// `header_style`, `row_style`, and `selected_style` each default to a fixed
/// palette (a light blue-gray header, a dim gray-blue for unselected rows,
/// and a bright-white-on-dark-blue highlight for the selected row); set them
/// with [`Table::header_style`], [`Table::row_style`], and
/// [`Table::selected_style`]. `column_spacing` defaults to `1` (a single
/// blank column between cells); set it with [`Table::column_spacing`].
#[derive(Clone, Copy, Debug)]
pub struct Table<'a> {
    headers: &'a [&'a str],
    widths: &'a [u16],
    rows: &'a [Vec<String>],
    header_style: Style,
    row_style: Style,
    selected_style: Style,
    column_spacing: u16,
}

impl<'a> Table<'a> {
    /// A table with the given header labels, column widths, and rows, in the
    /// default style.
    #[must_use]
    pub fn new(headers: &'a [&'a str], widths: &'a [u16], rows: &'a [Vec<String>]) -> Self {
        Self {
            headers,
            widths,
            rows,
            header_style: Style::new().fg(Color::Rgb {
                r: 210,
                g: 210,
                b: 230,
            }),
            row_style: Style::new().fg(Color::Rgb {
                r: 170,
                g: 175,
                b: 190,
            }),
            selected_style: Style::new().fg(Color::BRIGHT_WHITE).bg(Color::Rgb {
                r: 40,
                g: 60,
                b: 90,
            }),
            column_spacing: 1,
        }
    }

    /// Set the header row's style.
    #[must_use]
    pub const fn header_style(mut self, style: Style) -> Self {
        self.header_style = style;
        self
    }

    /// Set the style of unselected rows.
    #[must_use]
    pub const fn row_style(mut self, style: Style) -> Self {
        self.row_style = style;
        self
    }

    /// Set the style of the selected row, including its background fill.
    #[must_use]
    pub const fn selected_style(mut self, style: Style) -> Self {
        self.selected_style = style;
        self
    }

    /// Set the number of blank columns between cells.
    #[must_use]
    pub const fn column_spacing(mut self, spacing: u16) -> Self {
        self.column_spacing = spacing;
        self
    }

    /// Applies `theme`'s named roles to this table's row styles: `header_style` becomes
    /// `theme.fg` (brighter, matching the header's original brighter-than-row default) on
    /// `theme.panel_bg`, `row_style` becomes `theme.dim` (the same de-emphasized role a plain
    /// body row already reads as) on `theme.panel_bg`, and `selected_style` becomes `theme.bg`
    /// on `theme.accent` -- the same bright-on-accent highlight [`super::List::theme`] and
    /// [`super::Button::theme`] use.
    ///
    /// `header_style`/`row_style` always set an explicit background rather than leaving it at
    /// [`Style::new()`]'s default: an unset background isn't "transparent" once a real backend
    /// draws it (a bare `Color::Default` cell paints as solid black behind the glyph, not
    /// whatever was there before -- see `retroglyph-software`'s `DEFAULT_BG`), so this widget
    /// assumes it's drawn on `theme.panel_bg` -- true when composed with a themed
    /// [`super::Panel`]/[`super::Modal`], the common case -- rather than risk a black box behind
    /// every row on a light [`Theme`]. Drawing this table directly on the raw screen background
    /// instead of inside a themed panel needs a manual `.header_style(...)`/`.row_style(...)`
    /// override afterwards.
    ///
    /// Call before any manual [`Table::header_style`]/[`Table::row_style`]/
    /// [`Table::selected_style`] override you want to keep.
    #[must_use]
    pub fn theme(mut self, theme: Theme) -> Self {
        self.header_style = Style::new().fg(theme.fg).bg(theme.panel_bg);
        self.row_style = Style::new().fg(theme.dim).bg(theme.panel_bg);
        self.selected_style = Style::new().fg(theme.bg).bg(theme.accent);
        self
    }
}

impl<B: Backend> StatefulWidget<B> for Table<'_> {
    type State = ListState;

    fn render(self, area: Rect, term: &mut Terminal<B>, state: &mut Self::State) {
        if area.width() == 0 || area.height() == 0 {
            return;
        }
        draw_row(
            term,
            area,
            area.top(),
            self.headers,
            self.widths,
            RowStyle {
                style: self.header_style,
                bg: None,
                column_spacing: self.column_spacing,
            },
        );

        let visible_rows = area.height_usize().saturating_sub(1);
        let selected = state.selected();
        for (row_index, row) in visible_window(self.rows, state.offset(), visible_rows) {
            let y = area.top() + 1 + (row_index - state.offset()) as u16;
            let (style, bg) = if Some(row_index) == selected {
                (self.selected_style, Some(self.selected_style.background()))
            } else {
                (self.row_style, None)
            };
            let cells: Vec<&str> = row.iter().map(String::as_str).collect();
            draw_row(
                term,
                area,
                y,
                &cells,
                self.widths,
                RowStyle {
                    style,
                    bg,
                    column_spacing: self.column_spacing,
                },
            );
        }
        term.reset_style();
    }
}

/// The style and layout options for drawing one [`Table`] row, grouped to keep [`draw_row`]'s
/// argument count within clippy's limit.
#[derive(Clone, Copy)]
struct RowStyle {
    /// The text (and, for the selected row, background) style.
    style: Style,
    /// When set, the whole row width is filled with this background first.
    bg: Option<Color>,
    /// The number of blank columns between cells.
    column_spacing: u16,
}

/// Draw one table row of `column_spacing`-separated, per-column-clipped cells at row `y`.
fn draw_row<B: Backend>(
    term: &mut Terminal<B>,
    area: Rect,
    y: u16,
    cells: &[&str],
    widths: &[u16],
    row_style: RowStyle,
) {
    let RowStyle {
        style,
        bg,
        column_spacing,
    } = row_style;
    if let Some(bg) = bg {
        fill_rect(
            term,
            Rect::new(area.left(), y, area.width(), 1),
            ' ',
            Style::new().bg(bg),
        );
    }
    let mut x = area.left();
    for (cell, &w) in cells.iter().zip(widths) {
        if x >= area.right() {
            break;
        }
        let avail = (area.right() - x).min(w) as usize;
        let text = truncate_to_cols(cell, avail);
        term.reset_style()
            .fg(style.foreground())
            .bg(style.background());
        term.print(x, y, &text);
        x = x.saturating_add(w + column_spacing);
    }
    term.reset_style();
}

#[cfg(test)]
mod tests {
    use retroglyph_core::Headless;

    use super::*;

    #[test]
    fn table_widget_highlights_the_selected_row() {
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = vec![vec!["Alpha".to_string()], vec!["Bravo".to_string()]];
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.select(Some(1));
        table.render(area, &mut term, &mut state);

        // Row 1 ("Bravo") is highlighted; row 0 ("Alpha") is not.
        let highlighted_bg = term.grid().get(0, 2).style().background();
        let plain_bg = term.grid().get(0, 1).style().background();
        assert_ne!(highlighted_bg, plain_bg);
    }

    #[test]
    fn table_widget_highlights_nothing_when_unselected() {
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = vec![vec!["Alpha".to_string()], vec!["Bravo".to_string()]];
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new(); // nothing selected
        table.render(area, &mut term, &mut state);

        let row0_bg = term.grid().get(0, 1).style().background();
        let row1_bg = term.grid().get(0, 2).style().background();
        assert_eq!(row0_bg, row1_bg);
    }

    fn rows(names: &[&str]) -> Vec<Vec<String>> {
        names.iter().map(|n| vec![(*n).to_string()]).collect()
    }

    #[test]
    fn scroll_offset_renders_the_window_starting_at_offset() {
        // 2 visible rows (area height 3, minus the header row).
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = rows(&["Alpha", "Bravo", "Charlie", "Delta"]);
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.set_offset(2); // window is [Charlie, Delta]
        table.render(area, &mut term, &mut state);

        // Row 1 is "Charlie", row 2 is "Delta"; neither "Alpha" nor "Bravo"
        // (offset 0/1) are drawn anywhere.
        assert_eq!(term.grid().get(0, 1).glyph(), 'C');
        assert_eq!(term.grid().get(0, 2).glyph(), 'D');
    }

    #[test]
    fn selection_scrolled_out_of_view_highlights_nothing() {
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = rows(&["Alpha", "Bravo", "Charlie", "Delta"]);
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.select(Some(0)); // "Alpha"
        state.set_offset(2); // but the window starts at "Charlie"
        table.render(area, &mut term, &mut state);

        let row0_bg = term.grid().get(0, 1).style().background();
        let row1_bg = term.grid().get(0, 2).style().background();
        assert_eq!(row0_bg, row1_bg); // neither visible row is highlighted
    }

    #[test]
    fn default_header_style_matches_previous_hardcoded_color() {
        let area = Rect::new(0, 0, 20, 2);
        let headers = ["Name"];
        let widths = [10u16];
        let rows: Vec<Vec<String>> = vec![];
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 2));
        let mut state = ListState::new();
        table.render(area, &mut term, &mut state);

        let expected = Color::Rgb {
            r: 210,
            g: 210,
            b: 230,
        };
        assert_eq!(term.grid().get(0, 0).style().foreground(), expected);
    }

    #[test]
    fn header_style_can_be_overridden() {
        let area = Rect::new(0, 0, 20, 2);
        let headers = ["Name"];
        let widths = [10u16];
        let rows: Vec<Vec<String>> = vec![];
        let custom = Style::new().fg(Color::RED);
        let table = Table::new(&headers, &widths, &rows).header_style(custom);

        let mut term = Terminal::new(Headless::new(20, 2));
        let mut state = ListState::new();
        table.render(area, &mut term, &mut state);

        assert_eq!(term.grid().get(0, 0).style().foreground(), Color::RED);
    }

    #[test]
    fn selected_style_can_be_overridden() {
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = vec![vec!["Alpha".to_string()], vec!["Bravo".to_string()]];
        let custom = Style::new().fg(Color::GREEN).bg(Color::BLUE);
        let table = Table::new(&headers, &widths, &rows).selected_style(custom);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.select(Some(1));
        table.render(area, &mut term, &mut state);

        assert_eq!(term.grid().get(0, 2).style().foreground(), Color::GREEN);
        assert_eq!(term.grid().get(0, 2).style().background(), Color::BLUE);
    }

    #[test]
    fn theme_maps_named_roles_onto_header_row_and_selected_styles() {
        let area = Rect::new(0, 0, 20, 3);
        let headers = ["Name"];
        let widths = [10u16];
        let rows = vec![vec!["Alpha".to_string()], vec!["Bravo".to_string()]];
        let table = Table::new(&headers, &widths, &rows).theme(Theme::DARK);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.select(Some(1));
        table.render(area, &mut term, &mut state);

        assert_eq!(term.grid().get(0, 0).style().foreground(), Theme::DARK.fg);
        assert_eq!(
            term.grid().get(0, 0).style().background(),
            Theme::DARK.panel_bg
        );
        assert_eq!(term.grid().get(0, 1).style().foreground(), Theme::DARK.dim);
        assert_eq!(
            term.grid().get(0, 1).style().background(),
            Theme::DARK.panel_bg
        );
        assert_eq!(term.grid().get(0, 2).style().foreground(), Theme::DARK.bg);
        assert_eq!(
            term.grid().get(0, 2).style().background(),
            Theme::DARK.accent
        );
    }

    #[test]
    fn column_spacing_can_be_overridden() {
        let area = Rect::new(0, 0, 20, 1);
        let headers = ["A", "B"];
        let widths = [1u16, 1u16];
        let rows: Vec<Vec<String>> = vec![];
        let table = Table::new(&headers, &widths, &rows).column_spacing(3);

        let mut term = Terminal::new(Headless::new(20, 1));
        let mut state = ListState::new();
        table.render(area, &mut term, &mut state);

        // Default spacing (1) would put "B" at column 2; spacing 3 pushes
        // it out to column 4.
        assert_eq!(term.grid().get(0, 0).glyph(), 'A');
        assert_eq!(term.grid().get(4, 0).glyph(), 'B');
    }

    #[test]
    fn ensure_visible_before_render_keeps_selection_on_screen() {
        let area = Rect::new(0, 0, 20, 3); // 2 visible rows
        let headers = ["Name"];
        let widths = [10u16];
        let rows = rows(&["Alpha", "Bravo", "Charlie", "Delta"]);
        let table = Table::new(&headers, &widths, &rows);

        let mut term = Terminal::new(Headless::new(20, 3));
        let mut state = ListState::new();
        state.select(Some(3)); // "Delta", off the front of the default window
        state.ensure_visible(2);
        table.render(area, &mut term, &mut state);

        // ensure_visible moved the window to [2, 4): "Charlie" then "Delta",
        // with "Delta" (the selection) highlighted on the last visible row.
        assert_eq!(term.grid().get(0, 2).glyph(), 'D');
        let highlighted_bg = term.grid().get(0, 2).style().background();
        let plain_bg = term.grid().get(0, 1).style().background();
        assert_ne!(highlighted_bg, plain_bg);
    }
}