facett-table 0.1.10

facett — generic data-table viewer (columns + string rows, striped grid)
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
//! **WarehouseTableView** — a generic warehouse-style data browser: a left
//! **table picker**, and for the selected table either a striped grid OR (when a
//! numeric column exists) a hand-painted horizontal **bar chart**, with combo
//! pickers for the value/label columns. Generalised from nornir's
//! `src/viz/warehouse_tab.rs` (`WarehouseBrowser`).
//!
//! The nornir original couples this to an Iceberg warehouse + a `Warehouse.Scan`
//! gRPC; THIS is the reusable UI half — the source is just an in-memory map of
//! `table name → `[`PreviewTable`]`. A host that has Iceberg/RPC builds those
//! previews and feeds them in; this crate keeps no I/O. A canonical [`Facet`].

use facett_core::scroll_engine::{SmoothScroll, smooth_scroll_area};
use facett_core::{Facet, FacetCaps, Semantics};
use serde::{Deserialize, Serialize};

const CELL_MAX_CHARS: usize = 80;

/// A previewed table: named `columns` + stringified `rows`. The shape nornir's
/// `IcebergWarehouse::scan_preview` produces — but source-agnostic.
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct PreviewTable {
    pub columns: Vec<String>,
    pub rows: Vec<Vec<String>>,
}

impl PreviewTable {
    pub fn new(columns: Vec<String>, rows: Vec<Vec<String>>) -> Self {
        Self { columns, rows }
    }

    /// True if at least half of the column's non-empty cells parse as `f64` (and
    /// at least one does) — the test for offering it as a chart value column.
    /// Direct port of nornir's `col_is_numeric`.
    fn col_is_numeric(&self, col: usize) -> bool {
        let (mut seen, mut ok) = (0usize, 0usize);
        for row in &self.rows {
            let cell = row.get(col).map(String::as_str).unwrap_or("");
            if cell.is_empty() {
                continue;
            }
            seen += 1;
            if cell.trim().parse::<f64>().is_ok() {
                ok += 1;
            }
        }
        ok > 0 && ok * 2 >= seen
    }

    /// The indices of every numeric (chart-able) column.
    pub fn numeric_columns(&self) -> Vec<usize> {
        (0..self.columns.len()).filter(|&i| self.col_is_numeric(i)).collect()
    }
}

/// A warehouse-style browser over a set of named [`PreviewTable`]s.
pub struct WarehouseTableView {
    title: String,
    tables: Vec<(String, PreviewTable)>,
    selected: Option<usize>,
    /// 📊 chart mode: render a numeric column as horizontal bars instead of the grid.
    chart: bool,
    chart_val: Option<usize>,
    chart_label: Option<usize>,
    /// Smooth scroll (both axes) for the central data grid (SCRL-3): eased,
    /// injected-clock (FC-7) — state lives here, not in egui memory.
    grid_scroll_x: SmoothScroll,
    grid_scroll_y: SmoothScroll,
}

impl WarehouseTableView {
    /// An empty browser titled `title`. Add tables with [`with_table`](Self::with_table).
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            tables: Vec::new(),
            selected: None,
            chart: false,
            chart_val: None,
            chart_label: None,
            grid_scroll_x: SmoothScroll::default(),
            grid_scroll_y: SmoothScroll::default(),
        }
    }

    /// Add a named preview table (builder form). The first added becomes the
    /// default selection.
    pub fn with_table(mut self, name: impl Into<String>, table: PreviewTable) -> Self {
        if self.tables.is_empty() {
            self.selected = Some(0);
        }
        self.tables.push((name.into(), table));
        self
    }

    /// Re-title the view (the deck keys facets off [`Facet::title`]).
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = title.into();
    }

    /// **Mutator**: set (or replace) the named table's preview in place — the
    /// host-driven counterpart of the [`with_table`](Self::with_table) builder, used
    /// by a live feed that grows a table's rows each tick. If a table with `name`
    /// already exists its preview is swapped (selection preserved); otherwise the
    /// table is appended (and becomes the default selection if it's the first one).
    pub fn set_table(&mut self, name: impl Into<String>, table: PreviewTable) {
        let name = name.into();
        if let Some(slot) = self.tables.iter_mut().find(|(n, _)| *n == name) {
            slot.1 = table;
        } else {
            if self.tables.is_empty() {
                self.selected = Some(0);
            }
            self.tables.push((name, table));
        }
    }

    /// The names of every loaded table (the left-panel list).
    pub fn table_names(&self) -> Vec<String> {
        self.tables.iter().map(|(n, _)| n.clone()).collect()
    }

    /// The name of the currently-selected table, if any.
    pub fn selected_name(&self) -> Option<&str> {
        self.selected.and_then(|i| self.tables.get(i)).map(|(n, _)| n.as_str())
    }

    /// Select a table by name (the headless equivalent of clicking it); clears any
    /// stale chart-column choices. Returns `true` if such a table exists.
    pub fn select(&mut self, name: &str) -> bool {
        match self.tables.iter().position(|(n, _)| n == name) {
            Some(i) => {
                self.selected = Some(i);
                self.chart_val = None;
                self.chart_label = None;
                true
            }
            None => false,
        }
    }

    fn current(&self) -> Option<&PreviewTable> {
        self.selected.and_then(|i| self.tables.get(i)).map(|(_, t)| t)
    }

    /// **Discovery-contract `local()`** — seeds two realistic tables (one numeric,
    /// chart-able; one text) so the browser renders with no host attached.
    pub fn local() -> Self {
        Self::new("Warehouse")
            .with_table(
                "bench_runs",
                PreviewTable::new(
                    vec!["bench".into(), "ms".into()],
                    vec![
                        vec!["xml_parse".into(), "12.4".into()],
                        vec!["json_parse".into(), "8.1".into()],
                        vec!["zstd_decode".into(), "31.7".into()],
                    ],
                ),
            )
            .with_table(
                "repos",
                PreviewTable::new(
                    vec!["name".into(), "lang".into()],
                    vec![vec!["nornir".into(), "rust".into()], vec!["facett".into(), "rust".into()]],
                ),
            )
    }

    /// **Discovery-contract `remote()`** — same surface, re-titled (the variant a
    /// host builds from a server scan; no transport here).
    pub fn remote() -> Self {
        let mut v = Self::local();
        v.title = "Warehouse (remote)".into();
        v
    }
}

const ROW_H: f32 = 18.0;
const LABEL_W: f32 = 230.0;

impl Facet for WarehouseTableView {
    fn title(&self) -> &str {
        &self.title
    }

    fn ui(&mut self, ui: &mut egui::Ui) {
        egui::Panel::left("wh_tables").default_size(220.0).show_inside(ui, |ui| {
            ui.heading(format!("tables ({})", self.tables.len()));
            ui.separator();
            egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
                let names: Vec<String> = self.table_names();
                for name in &names {
                    let selected = self.selected_name() == Some(name.as_str());
                    if ui.selectable_label(selected, name).clicked() {
                        self.select(name);
                    }
                }
            });
        });

        egui::CentralPanel::default().show_inside(ui, |ui| {
            let th = facett_core::theme(ui);
            let Some(p) = self.current().cloned() else {
                ui.label("select a table on the left to view its rows");
                return;
            };
            let table_name = self.selected_name().unwrap_or("").to_string();
            let ncols = p.columns.len();
            let numeric = p.numeric_columns();
            let mut chart = self.chart;
            let mut chart_val = self.chart_val.filter(|&i| i < ncols);
            let mut chart_label = self.chart_label.filter(|&i| i < ncols);

            ui.horizontal(|ui| {
                ui.heading(&table_name);
                ui.label(format!("· {} rows · {} columns", p.rows.len(), ncols));
                if !numeric.is_empty() {
                    ui.separator();
                    ui.checkbox(&mut chart, "📊 chart");
                }
            });

            if chart && !numeric.is_empty() {
                if chart_val.map(|i| !numeric.contains(&i)).unwrap_or(true) {
                    chart_val = numeric.first().copied();
                }
                if chart_label.is_none() {
                    chart_label = (0..ncols).find(|i| !numeric.contains(i));
                }
                ui.horizontal(|ui| {
                    ui.label("value:");
                    egui::ComboBox::from_id_salt("wh_chart_val")
                        .selected_text(chart_val.map(|i| p.columns[i].clone()).unwrap_or_default())
                        .show_ui(ui, |ui| {
                            for &i in &numeric {
                                ui.selectable_value(&mut chart_val, Some(i), &p.columns[i]);
                            }
                        });
                    ui.label("label:");
                    egui::ComboBox::from_id_salt("wh_chart_label")
                        .selected_text(chart_label.map(|i| p.columns[i].clone()).unwrap_or("(row #)".into()))
                        .show_ui(ui, |ui| {
                            ui.selectable_value(&mut chart_label, None, "(row #)");
                            for i in 0..ncols {
                                ui.selectable_value(&mut chart_label, Some(i), &p.columns[i]);
                            }
                        });
                });
                ui.separator();
                if let Some(v) = chart_val {
                    draw_bar_chart(ui, &p, v, chart_label, &th);
                }
            } else {
                ui.separator();
                let base_id = ui.id().with("wh-grid");
                // Smooth-scroll both axes (SCRL-3) from this frame's `stable_dt`
                // (FC-7); `SmoothScroll` is `Copy`, so work on copies + write back.
                let dt = ui.input(|i| i.stable_dt);
                let (mut gx, mut gy) = (self.grid_scroll_x, self.grid_scroll_y);
                smooth_scroll_area(
                    ui,
                    egui::ScrollArea::both().auto_shrink([false, false]),
                    dt,
                    &mut gy,
                    Some(&mut gx),
                    |ui| {
                    egui::Grid::new("wh_grid").striped(true).num_columns(ncols.max(1)).show(ui, |ui| {
                        for c in &p.columns {
                            ui.strong(c);
                        }
                        ui.end_row();
                        for (ri, row) in p.rows.iter().enumerate() {
                            for cell in row {
                                let resp = ui
                                    .push_id(facett_core::stable_id(base_id, &ri.to_string()), |ui| ui.label(truncate(cell)))
                                    .inner;
                                let txt = truncate(cell);
                                resp.widget_info(move || Semantics::new(egui::WidgetType::Label, txt.clone()).widget_info());
                            }
                            ui.end_row();
                        }
                    });
                    },
                );
                self.grid_scroll_x = gx;
                self.grid_scroll_y = gy;
            }

            self.chart = chart;
            self.chart_val = chart_val;
            self.chart_label = chart_label;
        });

        #[cfg(feature = "testmatrix")]
        facett_core::testmatrix::emit(
            "facett-table::WarehouseTableView::ui",
            "ui_render",
            true,
            &format!("tables={} selected={:?} chart={}", self.tables.len(), self.selected_name(), self.chart),
        );
    }

    fn state_json(&self) -> serde_json::Value {
        let preview = match self.current() {
            None => serde_json::json!({ "loaded": false }),
            Some(p) => serde_json::json!({
                "loaded": true,
                "columns": p.columns,
                "rows": p.rows.len(),
                "numeric_columns": p.numeric_columns(),
            }),
        };
        serde_json::json!({
            "title": self.title,
            "tables": self.table_names(),
            "table_count": self.tables.len(),
            "selected": self.selected_name(),
            "chart": { "on": self.chart, "value_col": self.chart_val, "label_col": self.chart_label },
            "preview": preview,
        })
    }

    fn caps(&self) -> FacetCaps {
        FacetCaps::NONE.selectable().searchable().resizable().themeable()
    }

    fn selection_json(&self) -> serde_json::Value {
        serde_json::json!(self.selected_name())
    }

    /// Opt into typed downcast so a host can drive this view's mutators
    /// ([`set_table`](Self::set_table) / [`select`](Self::select)) when it lives boxed
    /// inside a `FacetDeck` (e.g. a live sim feeding a growing table each tick).
    fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
        Some(self)
    }
}

/// Hand-painted horizontal bar chart over the preview rows (no plotting dep —
/// consistent with the nornir original). `val` is the numeric column; `label`
/// the optional label column (row index when `None`).
fn draw_bar_chart(ui: &mut egui::Ui, p: &PreviewTable, val: usize, label: Option<usize>, th: &facett_core::theme::Theme) {
    let red = egui::Color32::from_rgb(224, 90, 90);
    let rows: Vec<(String, f64)> = p
        .rows
        .iter()
        .enumerate()
        .filter_map(|(i, row)| {
            let v: f64 = row.get(val)?.trim().parse().ok()?;
            let l = match label {
                Some(li) => truncate(row.get(li).map(String::as_str).unwrap_or("")),
                None => format!("#{i}"),
            };
            Some((l, v))
        })
        .take(120)
        .collect();
    if rows.is_empty() {
        ui.label("no numeric values to chart in this column");
        return;
    }
    let max = rows.iter().map(|(_, v)| v.abs()).fold(f64::EPSILON, f64::max);
    egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
        let h = ROW_H * rows.len() as f32 + 8.0;
        let (resp, painter) = ui.allocate_painter(egui::Vec2::new(ui.available_width(), h), egui::Sense::hover());
        let rect = resp.rect;
        let bar_w = (rect.width() - LABEL_W - 90.0).max(40.0);
        for (i, (l, v)) in rows.iter().enumerate() {
            let y = rect.top() + 4.0 + i as f32 * ROW_H;
            painter.text(egui::Pos2::new(rect.left() + 4.0, y + ROW_H / 2.0), egui::Align2::LEFT_CENTER, l, egui::FontId::monospace(11.0), th.text_dim);
            let w = ((v.abs() / max) as f32 * bar_w).max(1.0);
            let bar = egui::Rect::from_min_size(egui::Pos2::new(rect.left() + LABEL_W, y + 2.0), egui::Vec2::new(w, ROW_H - 5.0));
            painter.rect_filled(bar, 2.0, if *v < 0.0 { red } else { th.accent });
            painter.text(bar.right_center() + egui::Vec2::new(6.0, 0.0), egui::Align2::LEFT_CENTER, format!("{v}"), egui::FontId::monospace(11.0), th.text_dim);
        }
    });
}

/// Char-safe truncation (never splits a multibyte char) for grid/chart cells.
fn truncate(s: &str) -> String {
    if s.chars().count() > CELL_MAX_CHARS {
        let head: String = s.chars().take(CELL_MAX_CHARS).collect();
        format!("{head}")
    } else {
        s.to_string()
    }
}

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

    #[test]
    fn numeric_column_detection() {
        let p = PreviewTable::new(
            vec!["name".into(), "ms".into(), "mixed".into()],
            vec![
                vec!["a".into(), "1.0".into(), "1".into()],
                vec!["b".into(), "2.5".into(), "x".into()],
                vec!["c".into(), "3".into(), "y".into()],
            ],
        );
        // col 1 is fully numeric; col 0 not; col 2 only 1/3 numeric (< half).
        assert_eq!(p.numeric_columns(), vec![1]);
    }

    #[test]
    fn select_switches_table_and_clears_chart_cols() {
        let mut v = WarehouseTableView::local();
        // default selection is the first added table.
        assert_eq!(v.selected_name(), Some("bench_runs"));
        v.chart_val = Some(1);
        assert!(v.select("repos"));
        assert_eq!(v.selected_name(), Some("repos"));
        assert_eq!(v.chart_val, None, "switching tables clears stale chart cols");
        assert!(!v.select("nope"));
    }

    /// inject-assert (LAW 6): feed real tables, assert `state_json` reports the
    /// list, the selection, and the selected preview's shape + numeric columns.
    #[test]
    fn state_json_reports_tables_selection_and_preview() {
        let v = WarehouseTableView::local();
        let j = v.state_json();
        assert_eq!(j["table_count"], 2);
        assert_eq!(j["tables"], serde_json::json!(["bench_runs", "repos"]));
        assert_eq!(j["selected"], "bench_runs");
        assert_eq!(j["preview"]["loaded"], true);
        assert_eq!(j["preview"]["rows"], 3);
        assert_eq!(j["preview"]["columns"], serde_json::json!(["bench", "ms"]));
        // the `ms` column (index 1) is detected numeric → chart-able.
        assert_eq!(j["preview"]["numeric_columns"], serde_json::json!([1]));
    }

    #[test]
    fn empty_view_reports_no_preview() {
        let v = WarehouseTableView::new("Empty");
        let j = v.state_json();
        assert_eq!(j["table_count"], 0);
        assert_eq!(j["selected"], serde_json::Value::Null);
        assert_eq!(j["preview"]["loaded"], false);
    }

    #[test]
    fn local_remote_titles() {
        assert_eq!(<WarehouseTableView as Facet>::title(&WarehouseTableView::local()), "Warehouse");
        assert_eq!(<WarehouseTableView as Facet>::title(&WarehouseTableView::remote()), "Warehouse (remote)");
    }
}