Skip to main content

datui_lib/widgets/
schema.rs

1use polars::prelude::*;
2use ratatui::{
3    buffer::Buffer,
4    layout::Rect,
5    style::{Modifier, Style},
6    text::Span,
7    widgets::{Block, BorderType, Borders, Cell, Row, Table, Widget},
8};
9
10struct SchemaView<'a> {
11    lf: &'a LazyFrame,
12}
13
14impl<'a> Widget for &'a SchemaView<'a> {
15    fn render(self, area: Rect, buf: &mut Buffer) {
16        let block = Block::default()
17            .title("Schema")
18            .borders(Borders::ALL)
19            .border_type(BorderType::Rounded);
20
21        let rows: Vec<Row> = if let Ok(schema) = self.lf.clone().collect_schema() {
22            schema
23                .iter()
24                .map(|(name, dtype)| {
25                    let cells: Vec<Cell> = vec![
26                        Cell::from(Span::raw(name.to_string())),
27                        Cell::from(Span::raw(dtype.to_string())),
28                    ];
29                    Row::new(cells)
30                })
31                .collect()
32        } else {
33            vec![]
34        };
35        // iterate the schema and create two colum table,
36        // one column for the name and one for the type
37
38        // calculate widths as fractions of total width, equal per column
39        let widths = vec![area.width / 2; 2];
40        // Create and render the table
41        let table = Table::new(rows, widths)
42            .header(Row::new(vec![
43                Cell::from(Span::styled(
44                    "Column",
45                    Style::default().add_modifier(Modifier::BOLD),
46                )),
47                Cell::from(Span::styled(
48                    "Type",
49                    Style::default().add_modifier(Modifier::BOLD),
50                )),
51            ]))
52            .block(block);
53
54        Widget::render(table, area, buf);
55    }
56}