Skip to main content

max / makenotwork

1.3 KB · 44 lines History Blame Raw
1 //! Shared TUI widget helpers to reduce table-rendering boilerplate.
2
3 use ratatui::Frame;
4 use ratatui::layout::Constraint;
5 use ratatui::style::{Color, Modifier, Style};
6 use ratatui::widgets::{Row, Table};
7
8 /// Style for the currently selected row (DarkGray bg, White fg, Bold).
9 /// Returns default style for non-selected rows.
10 pub fn selected_style(i: usize, selected: Option<usize>) -> Style {
11 if selected == Some(i) {
12 Style::default()
13 .bg(Color::DarkGray)
14 .fg(Color::White)
15 .add_modifier(Modifier::BOLD)
16 } else {
17 Style::default()
18 }
19 }
20
21 /// Render a table with a styled header row.
22 ///
23 /// Callers build their own `Vec<Row>` (using [`selected_style`] for
24 /// row highlighting) and pass column headers + widths. This function
25 /// assembles the header, constructs the `Table`, and renders it.
26 pub fn render_table(
27 frame: &mut Frame,
28 area: ratatui::layout::Rect,
29 headers: &[&str],
30 widths: &[Constraint],
31 rows: Vec<Row>,
32 ) {
33 let header = Row::new(headers.iter().map(|h| h.to_string()).collect::<Vec<_>>())
34 .style(
35 Style::default()
36 .fg(Color::DarkGray)
37 .add_modifier(Modifier::BOLD),
38 )
39 .bottom_margin(0);
40
41 let table = Table::new(rows, widths).header(header);
42 frame.render_widget(table, area);
43 }
44