Skip to main content

rat_ftable2/vec/
widget.rs

1use crate::TableSelection;
2use crate::derive::derive_table_widget;
3use crate::selection::RowSelection;
4use crate::table::Table;
5use crate::vec::TableVecState;
6use ratatui_core::buffer::Buffer;
7use ratatui_core::layout::Rect;
8use ratatui_core::widgets::StatefulWidget;
9
10///
11/// A table that holds a Vec of the data in its state.
12///
13/// ```rust, ignore
14/// TableVec::default()
15///         .widths([
16///             Constraint::Length(6),
17///             Constraint::Length(20),
18///             Constraint::Length(15),
19///             Constraint::Length(15),
20///             Constraint::Length(3),
21///         ])
22///         .render_cell(0, |_v, area, buf, ctx| {
23///             row_fmt.fmt_u(ctx.row).render(area, buf)
24///         })
25///         .render_cell(1, |v, area, buf, _ctx| v.text.render(area, buf))
26///         .render_cell(2, |v, area, buf, _ctx| {
27///             num1_fmt.fmt_u(v.num1).render(area, buf)
28///         })
29///         .render_cell(3, |v, area, buf, _ctx| {
30///             num2_fmt.fmt_u(v.num2).render(area, buf)
31///         })
32///         .render_cell(4, |v, area, buf, _ctx| {
33///             let cc = if v.check { "\u{2622}" } else { "\u{2623}" };
34///             cc.render(area, buf);
35///         })
36///         .column_spacing(1)
37///         .header(vec!["Nr", "Text", "Val1", "Val2", "State"])
38///         .footer(vec!["a", "b", "c", "d", "e"])
39///         .block(
40///             Block::bordered()
41///                 .border_type(BorderType::Rounded)
42///                 .title("tabledata-iter (no row-count)"),
43///         )
44///         .vscroll(Scroll::new())
45///         .styles(table(&ctx.theme))
46///         .render(l0[0], buf, &mut state.table);
47/// ```
48pub struct TableVec<'a, T, Selection = RowSelection> {
49    core: Table<'a, &'a T, Selection>,
50}
51
52impl<'a, T, Selection> Default for TableVec<'a, T, Selection> {
53    fn default() -> Self {
54        Self {
55            core: Default::default(),
56        }
57    }
58}
59
60impl<'a, T, Selection> TableVec<'a, T, Selection> {
61    pub fn new() -> Self
62    where
63        Selection: Default,
64    {
65        Self::default()
66    }
67
68    derive_table_widget!(REF_T);
69    derive_table_widget!();
70}
71
72impl<'a, 'b, T, Selection> TableVec<'a, T, Selection>
73where
74    Selection: TableSelection + Default,
75    'b: 'a,
76{
77    pub fn render(
78        mut self,
79        area: Rect,
80        buf: &mut Buffer,
81        state: &'b mut TableVecState<T, Selection>,
82    ) {
83        self.core = self.core.data(state.data.iter());
84        self.core.render(area, buf, &mut state.table);
85    }
86}