datui-lib 0.4.5

Data Exploration in the Terminal (library)
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
//! Datatable main view: table content, sidebars (sort/filter, views, pivot/melt), export modal.

use crate::render::context::RenderContext;
use crate::render::datatable_view::{ActiveSidebar, DatatableLayout};
use crate::widgets::info::{DataTableInfo, InfoContext};
use crate::widgets::table::DataTable;
use crate::widgets::ui::{HintBar, Working};
use crate::widgets::{copy, export, pivot_melt};
use datui_cli::keys::Context;
use ratatui::layout::Rect;
use ratatui::prelude::StatefulWidget;
use ratatui::style::{Modifier, Style};
use ratatui::widgets::{Clear, Paragraph, Widget};

/// Renders the datatable main view: layout, table content, input strip, sidebars, export modal.
pub fn render(
    area: Rect,
    main_area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    app: &mut crate::App,
    ctx: &RenderContext,
) {
    let active_sidebar = ActiveSidebar::from_modals(
        app.overlay.shows(&crate::Overlay::Info),
        app.overlay == crate::Overlay::SortFilter,
        app.overlay == crate::Overlay::View,
    );

    let datatable_layout = DatatableLayout::compute(
        main_area,
        active_sidebar,
        app.app_config.display.sidebar_width,
    );
    let data_area = datatable_layout.content_area;
    let sort_area = datatable_layout.sidebar_area.unwrap_or_default();

    let evidence_label = app.quality.evidence_label.clone();
    // Asked before the table is borrowed: the job records are the app's.
    let facts_reading = app.file_facts_reading();
    let facts_tab = app.info_facts_tab();
    let footer_expected = app.info_facts().is_some_and(|(_, facts)| facts.footer);
    let declared_types = app
        .opened_format()
        .is_some_and(|f| f.descriptor().declares_types);
    let find_cell = app.find_hit();
    let match_cells = app.live_cells();
    let hex = app.hex_target().is_some();
    let query_reading = app.query_reading().map(str::to_string);
    let frame = app.throbber_frame as usize;
    let header_toggle = app.header_toggle_offered();
    let estimate = app.row_estimate();
    match &mut app.data_table_state {
        Some(state) => {
            let mut table_area = data_area;
            let breadcrumb_text = if state.is_drilled_down() {
                state.drilled_group_key().map(|(key_columns, key_values)| {
                    let g = crate::glyphs::get();
                    // A tab or line break in a key would cut the line: marked, as
                    // in a cell.
                    let parts: Vec<String> = key_columns
                        .iter()
                        .zip(key_values.iter())
                        .map(|(col, val)| {
                            format!(
                                "{col}={}",
                                crate::exact::cell_preview(val, g, main_area.width)
                            )
                        })
                        .collect();
                    format!(
                        "{} Group: {}",
                        g.arrow_left,
                        parts.join(&format!(" {} ", g.middot))
                    )
                })
            } else {
                evidence_label.map(|label| format!("{} {label}", crate::glyphs::get().arrow_left))
            };
            if let Some(text) = breadcrumb_text {
                render_breadcrumb(
                    Rect {
                        height: 1.min(data_area.height),
                        ..data_area
                    },
                    buf,
                    &text,
                    ctx,
                );
                table_area = Rect {
                    y: data_area.y + 1.min(data_area.height),
                    height: data_area.height.saturating_sub(1),
                    ..data_area
                };
            }

            Clear.render(table_area, buf);
            let mut dt = DataTable::new()
                .with_colors(
                    ctx.table_header_bg,
                    ctx.table_header,
                    ctx.row_numbers,
                    ctx.column_separator,
                )
                .with_cell_padding(ctx.table_cell_padding)
                .with_screen_width(main_area.width)
                .with_alternate_row_bg(ctx.alternate_row_color)
                .with_stripes_follow_rows(ctx.stripes_follow_rows)
                .with_binary_col(ctx.binary_col)
                .with_binary_columns(state.binary_column_names())
                .with_number_format(ctx.number_format.clone())
                .with_dtype_row(ctx.dtype_row)
                .with_selection_colors(
                    ctx.highlight_style(),
                    ctx.table_selected,
                    ctx.accent,
                    ctx.dimmed,
                )
                .with_cursor_styles(ctx.column_cursor_style(), ctx.cell_cursor_style())
                .with_drift(
                    state.display_drift(table_area.height as usize),
                    state.drift_groups(),
                )
                .with_find_cell(find_cell, ctx.find_match)
                .with_match_cells(match_cells);
            if ctx.column_colors {
                dt = dt.with_column_type_colors(
                    ctx.str_col,
                    ctx.int_col,
                    ctx.float_col,
                    ctx.bool_col,
                    ctx.temporal_col,
                );
            }
            StatefulWidget::render(dt, table_area, buf, state);
            if let Some(status) = &query_reading {
                // Drawn still, so the table keeps its size: the rows are the view the
                // query replaces, under columns it may have changed. The footer's
                // words, so the two say one thing.
                Working {
                    text: status,
                    frame,
                }
                .render_centered(table_area, buf, ctx);
            }
            if app.overlay.shows(&crate::Overlay::Info) {
                let facts = crate::App::facts_shown(
                    &app.info.file_facts,
                    app.dataset_generation,
                    facts_reading,
                );
                let info_ctx = InfoContext {
                    format: app.source.original_file_format,
                    declared_types,
                    facts,
                    facts_tab,
                    footer_expected,
                };
                let mut info_widget = DataTableInfo::new(state, info_ctx, &mut app.info_modal, ctx);
                info_widget.hex = hex;
                info_widget.header_toggle = header_toggle;
                info_widget.codebook = app.info.codebook.as_deref();
                info_widget.estimate = estimate;
                info_widget.documentation = app
                    .info
                    .info_documentation
                    .is_open()
                    .then_some(&mut app.info.info_documentation);
                info_widget.render(sort_area, buf);
            }
        }
        None => {
            // No dataset at all: a first load that failed, leaving its error modal over
            // an empty screen. A load still in flight never reaches here — it draws
            // `loading_view` instead.
        }
    }

    if app.overlay == crate::Overlay::SortFilter {
        crate::render::sort_filter_sidebar::render(sort_area, buf, &mut app.sort_filter_modal, ctx);
    }

    if app.overlay == crate::Overlay::View {
        crate::render::view_sidebar::render(
            sort_area,
            buf,
            &mut app.view_modal,
            app.views.active_id.as_deref(),
            ctx,
        );
    }

    // A takeover: the form beside a preview of the reshaped rows.
    if app.overlay == crate::Overlay::PivotMelt {
        pivot_melt::render(main_area, buf, &mut app.pivot_melt_modal, ctx);
    }

    if matches!(app.overlay, crate::Overlay::Export { .. }) {
        // A commitment, so a compact centered dialog that never scales with
        // the terminal.
        let modal_area = export::dialog_area(area);
        export::render_export_modal(modal_area, buf, &mut app.export_modal, ctx);
    }

    if app.overlay == crate::Overlay::Inspect
        && let Some(state) = app.data_table_state.as_ref()
    {
        // A takeover: the row's fields want the width a long value reads at, and
        // the table under it holds the cursor the inspector moves.
        crate::widgets::inspector::render(
            main_area,
            buf,
            &mut app.inspector_modal,
            state,
            app.info.codebook.as_deref(),
            ctx,
        );
    }

    if app.overlay == crate::Overlay::GoToColumn {
        render_go_to_column(data_area, buf, &app.pickers.go_to_column, ctx);
    }

    if app.overlay == crate::Overlay::PickFormat {
        render_picker(
            data_area,
            buf,
            &app.pickers.format_picker,
            ("Format", Context::FormatPicker, "No format matches"),
            ctx,
        );
    }

    if matches!(app.overlay, crate::Overlay::Retype { .. })
        && let Some(modal) = &app.column_forms.retype
    {
        crate::widgets::retype::render_retype(area, buf, modal, ctx);
    }

    if matches!(app.overlay, crate::Overlay::Combine { .. })
        && let Some(modal) = &app.column_forms.combine
    {
        crate::widgets::retype::render_combine(area, buf, modal, ctx);
    }

    if app.overlay == crate::Overlay::Sample
        && let Some(form) = &app.sample.form
    {
        // A dialog over the table: what it covers takes no clicks.
        crate::app::pointer::record(data_area, crate::app::pointer::Hit::Modal);
        crate::widgets::sample_form::render(form, true, data_area, buf, ctx);
    }

    if app.overlay == crate::Overlay::PickTable
        && let Some(tables) = app.pickers.table_choices.as_ref()
    {
        let details: Vec<String> = tables
            .tables
            .iter()
            .enumerate()
            .map(
                |(i, t)| match (tables.current == Some(i), t.detail.is_empty()) {
                    (true, true) => "opened".to_string(),
                    (true, false) => format!("{}, opened", t.detail),
                    (false, _) => t.detail.clone(),
                },
            )
            .collect();
        render_picker_with(
            data_area,
            buf,
            &app.pickers.table_picker,
            ("Table", Context::TablePicker, "No table matches"),
            Some(&details),
            ctx,
        );
    }

    if app.overlay == crate::Overlay::Copy {
        // A commitment like export: compact and centered. The dialog holds
        // the most rows any scope offers, so stepping the scope moves nothing,
        // the spec and the footer; an open Picker earns the room it drops into.
        let modal_width = (area.width * 3 / 4).min(46);
        let wanted = if app.copy_modal.picker.is_some() {
            15
        } else {
            crate::app::modals::copy_modal::CopyModal::MOST_ROWS + 6
        };
        let modal_height = wanted.min(area.height);
        let modal_area = crate::render::layout::centered_rect(area, modal_width, modal_height);
        copy::render_copy_modal(modal_area, buf, &mut app.copy_modal, ctx);
    }
}

/// The drill-down breadcrumb: one line in the header tier above the table,
/// saying which rows these are, with the way back as a chip on the right.
pub(crate) fn render_breadcrumb(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    text: &str,
    ctx: &RenderContext,
) {
    if area.height == 0 || area.width == 0 {
        return;
    }
    // The analysis header's tier, so a drill-down reads like the header over a
    // tool's result.
    let style = Style::default().bg(ctx.controls_bg).fg(ctx.table_header);
    buf.set_style(area, style);
    let back = HintBar::from_ctx(ctx).screen(Context::Table).key("Esc");
    let chip_w = back.flush_width_in(area.width.saturating_sub(12));
    let text_w = area.width.saturating_sub(chip_w + 1);
    Paragraph::new(crate::glyphs::fit(text, text_w as usize))
        .style(style.add_modifier(Modifier::BOLD))
        .render(
            Rect {
                width: text_w,
                ..area
            },
            buf,
        );
    if chip_w > 0 {
        back.render_flush(
            Rect {
                x: area.x + area.width - chip_w,
                width: chip_w,
                ..area
            },
            buf,
        );
    }
}

/// The column picker: a short list over the table, which stays in view behind it so
/// the jump is seen landing. Its height is capped and the list scrolls inside it.
/// Sized by every column rather than the ones the filter admits, so typing moves
/// nothing.
fn render_go_to_column(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    picker: &crate::widgets::ui::PickerState,
    ctx: &RenderContext,
) {
    render_picker(
        area,
        buf,
        picker,
        ("Go to Column", Context::GoToColumn, "No column matches"),
        ctx,
    );
}

/// A short pick-one list over the table: `(title, the screen whose keys it takes, what
/// an empty narrowing says)`.
pub(crate) fn render_picker(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    picker: &crate::widgets::ui::PickerState,
    words: (&str, Context, &str),
    ctx: &RenderContext,
) {
    render_picker_with(area, buf, picker, words, None, ctx);
}

/// [`render_picker`], with a note beside each item, by its index among all of them.
pub(crate) fn render_picker_with(
    area: Rect,
    buf: &mut ratatui::buffer::Buffer,
    picker: &crate::widgets::ui::PickerState,
    (title, keys, none): (&str, Context, &str),
    details: Option<&[String]>,
    ctx: &RenderContext,
) {
    use ratatui::text::{Line, Span};
    let all = picker.items();
    let widest = all
        .iter()
        .enumerate()
        .map(|(i, item)| {
            let note = details
                .and_then(|d| d.get(i))
                .map_or(0, |d| crate::glyphs::display_width(d) + 2);
            crate::glyphs::display_width(item) + note
        })
        .max()
        .unwrap_or(0) as u16;
    let most = if details.is_some() { 64 } else { 48 };
    let width = (widest + 6).clamp(30, most).min(area.width);
    // Frame, filter line, the blank above the footer, footer, and up to ten names.
    let height = (all.len().max(1) as u16 + 5).min(15).min(area.height);
    let items = picker.filtered();
    if width < 4 || height < 4 {
        return;
    }
    let popup = Rect {
        x: area.x + area.width.saturating_sub(width) / 2,
        y: area.y + area.height.saturating_sub(height) / 3,
        width,
        height,
    };
    let footer = HintBar::from_ctx(ctx).screen(keys).key("Enter").key("Esc");
    let inner = crate::widgets::ui::Surface::new(title)
        .footer(&footer)
        .render(popup, buf, ctx);
    let filter = if picker.filter.is_empty() {
        Line::from(Span::styled(
            "type to narrow",
            Style::default().fg(ctx.dimmed),
        ))
    } else {
        Line::from(Span::raw(picker.filter.clone()))
    };
    Paragraph::new(filter).render(Rect { height: 1, ..inner }, buf);
    let list = Rect {
        y: inner.y + 1,
        height: inner.height.saturating_sub(1),
        ..inner
    };
    if items.is_empty() {
        Paragraph::new(Span::styled(none, Style::default().fg(ctx.dimmed)))
            .render(Rect { height: 1, ..list }, buf);
        return;
    }
    let mut list_widget = crate::widgets::ui::Picker::from_state(picker, true);
    if let Some(details) = details {
        let shown = items
            .iter()
            .map(|(i, _)| details.get(*i).cloned().unwrap_or_default())
            .collect();
        list_widget = list_widget.details(shown);
    }
    list_widget.render(list, buf, ctx);
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::buffer::Buffer;

    /// One line, no box: the text on the left, the way back on the right, and
    /// nothing drawn on the row below it.
    #[test]
    fn the_breadcrumb_is_one_plain_line() {
        let ctx = RenderContext::for_test();
        let area = Rect::new(0, 0, 60, 3);
        let mut buf = Buffer::empty(area);
        render_breadcrumb(
            Rect { height: 1, ..area },
            &mut buf,
            "<- Group: department=Engineering",
            &ctx,
        );
        let rows = crate::tests::buffer_lines(&buf);
        assert!(
            rows[0].starts_with("<- Group: department=Engineering"),
            "{rows:?}"
        );
        assert!(rows[0].trim_end().ends_with("Back"), "{rows:?}");
        assert!(rows[0].contains("Esc"), "{rows:?}");
        for row in &rows[1..] {
            assert!(row.trim().is_empty(), "a second row was drawn: {rows:?}");
        }
        let g = crate::glyphs::get();
        for glyph in [
            g.border.top_left,
            g.border.bottom_left,
            g.border.vertical_left,
        ] {
            assert!(
                !rows[0].contains(glyph),
                "a border around one line: {rows:?}"
            );
        }
    }

    /// Too long for the row, the text is cut with a mark and the way back stays.
    #[test]
    fn a_long_breadcrumb_is_cut_and_keeps_the_way_back() {
        let ctx = RenderContext::for_test();
        let area = Rect::new(0, 0, 40, 1);
        let mut buf = Buffer::empty(area);
        let long = format!("<- Group: {}", "x".repeat(80));
        render_breadcrumb(area, &mut buf, &long, &ctx);
        let row = &crate::tests::buffer_lines(&buf)[0];
        assert!(row.contains(crate::glyphs::get().ellipsis), "{row:?}");
        assert!(row.contains("Esc") && row.contains("Back"), "{row:?}");
    }
}