From 94c6167dc61595e655b763d7228e9117ada7abce Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 10 Apr 2024 11:50:31 +0200 Subject: [PATCH] lib: Rework table widget --- src/ui/widget.rs | 190 +++++++++++++++++++++++++++++++------ src/ui/widget/container.rs | 37 ++++++-- 2 files changed, 189 insertions(+), 38 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index 4323b474..bcf10a27 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -33,6 +33,10 @@ pub trait Render

{ fn render(&self, frame: &mut Frame, area: Rect, props: P); } +pub trait ToRow { + fn to_row(&self) -> Vec; +} + pub struct Shortcut { pub short: String, pub long: String, @@ -120,40 +124,103 @@ impl Render for Shortcuts { } } -pub trait ToRow { - fn to_row(&self) -> [Cell; W]; +#[derive(Clone, Debug)] +pub struct Column { + pub title: String, + pub width: Constraint, + pub skip: bool, +} + +impl Column { + pub fn new(title: &str, width: Constraint) -> Self { + Self { + title: title.to_string(), + width, + skip: false, + } + } + + pub fn skip(mut self, skip: bool) -> Self { + self.skip = skip; + self + } } #[derive(Debug)] -pub struct TableProps, const W: usize> { +pub struct TableProps { pub items: Vec, pub focus: bool, - pub widths: [Constraint; W], + pub columns: Vec, pub has_header: bool, pub has_footer: bool, pub cutoff: usize, pub cutoff_after: usize, + pub page_size: usize, } -pub struct Table { +impl Default for TableProps { + fn default() -> Self { + Self { + items: vec![], + focus: false, + columns: vec![], + has_header: false, + has_footer: false, + cutoff: usize::MAX, + cutoff_after: usize::MAX, + page_size: 1, + } + } +} + +pub struct Table { /// Sending actions to the state store pub action_tx: UnboundedSender, + /// Internal table properties + pub props: TableProps, /// Internal selection state state: TableState, } -impl Table { - pub fn selected(&self) -> Option { - self.state.selected() +impl Table { + pub fn items(mut self, items: Vec) -> Self { + self.props.items = items; + self } - pub fn prev(&mut self) -> Option { + pub fn columns(mut self, columns: Vec) -> Self { + self.props.columns = columns; + self + } + + pub fn header(mut self, has_header: bool) -> Self { + self.props.has_header = has_header; + self + } + + pub fn footer(mut self, has_footer: bool) -> Self { + self.props.has_footer = has_footer; + self + } + + pub fn cutoff(mut self, cutoff: usize, cutoff_after: usize) -> Self { + self.props.cutoff = cutoff; + self.props.cutoff_after = cutoff_after; + self + } + + pub fn page_size(mut self, page_size: usize) -> Self { + self.props.page_size = page_size; + self + } + + fn prev(&mut self) -> Option { let selected = self.selected().map(|current| current.saturating_sub(1)); self.state.select(selected); selected } - pub fn next(&mut self, len: usize) -> Option { + fn next(&mut self, len: usize) -> Option { let selected = self.selected().map(|current| { if current < len.saturating_sub(1) { current.saturating_add(1) @@ -165,7 +232,7 @@ impl Table { selected } - pub fn prev_page(&mut self, page_size: usize) -> Option { + fn prev_page(&mut self, page_size: usize) -> Option { let selected = self .selected() .map(|current| current.saturating_sub(page_size)); @@ -173,7 +240,7 @@ impl Table { selected } - pub fn next_page(&mut self, len: usize, page_size: usize) -> Option { + fn next_page(&mut self, len: usize, page_size: usize) -> Option { let selected = self.selected().map(|current| { if current < len.saturating_sub(1) { cmp::min(current.saturating_add(page_size), len.saturating_sub(1)) @@ -185,16 +252,20 @@ impl Table { selected } - pub fn begin(&mut self) -> Option { + fn begin(&mut self) -> Option { self.state.select(Some(0)); self.state.selected() } - pub fn end(&mut self, len: usize) -> Option { + fn end(&mut self, len: usize) -> Option { self.state.select(Some(len.saturating_sub(1))); self.state.selected() } + pub fn selected(&self) -> Option { + self.state.selected() + } + pub fn progress(&self, len: usize) -> (usize, usize) { let step = self .selected() @@ -224,13 +295,17 @@ impl Table { } } -impl Widget for Table { +impl Widget for Table +where + R: ToRow, +{ fn new(state: &S, action_tx: UnboundedSender) -> Self where Self: Sized, { Self { action_tx: action_tx.clone(), + props: TableProps::default(), state: TableState::default().with_selected(Some(0)), } .move_with_state(state) @@ -240,40 +315,95 @@ impl Widget for Table { where Self: Sized, { - Self { ..self } + let mut me = Self { ..self }; + + if let Some(selected) = me.selected() { + if selected > me.props.items.len() { + me.begin(); + } + } + + me } fn name(&self) -> &str { - "shortcuts" + "table" } - fn handle_key_event(&mut self, _key: Key) {} + fn handle_key_event(&mut self, key: Key) { + match key { + Key::Up | Key::Char('k') => { + self.prev(); + } + Key::Down | Key::Char('j') => { + self.next(self.props.items.len()); + } + Key::PageUp => { + self.prev_page(self.props.page_size); + } + Key::PageDown => { + self.next_page(self.props.items.len(), self.props.page_size); + } + Key::Home => { + self.begin(); + } + Key::End => { + self.end(self.props.items.len()); + } + _ => {} + } + } } -impl Render> for Table +impl Render<()> for Table where - R: ToRow + Debug, + R: ToRow + Debug, { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: TableProps) { - let widths = props.widths.to_vec(); - let widths = if area.width < props.cutoff as u16 { - widths.iter().take(props.cutoff_after).collect::>() + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { + let widths: Vec = self + .props + .columns + .iter() + .filter_map(|c| if !c.skip { Some(c.width) } else { None }) + .collect(); + + let widths = if area.width < self.props.cutoff as u16 { + widths + .iter() + .take(self.props.cutoff_after) + .collect::>() } else { widths.iter().collect::>() }; - let borders = match (props.has_header, props.has_footer) { + let borders = match (self.props.has_header, self.props.has_footer) { (false, false) => Borders::ALL, (true, false) => Borders::BOTTOM | Borders::LEFT | Borders::RIGHT, (false, true) => Borders::TOP | Borders::LEFT | Borders::RIGHT, (true, true) => Borders::LEFT | Borders::RIGHT, }; - if !props.items.is_empty() { - let rows = props + if !self.props.items.is_empty() { + let rows = self + .props .items .iter() - .map(|item| Row::new(item.to_row())) + .map(|item| { + let mut cells = vec![]; + let mut it = self.props.columns.iter(); + + for cell in item.to_row() { + if let Some(col) = it.next() { + if !col.skip { + cells.push(cell.clone()); + } + } else { + continue; + } + } + + Row::new(cells) + }) .collect::>(); let rows = ratatui::widgets::Table::default() .rows(rows) @@ -281,7 +411,7 @@ where .column_spacing(1) .block( Block::default() - .border_style(style::border(props.focus)) + .border_style(style::border(self.props.focus)) .border_type(BorderType::Rounded) .borders(borders), ) @@ -290,7 +420,7 @@ where frame.render_stateful_widget(rows, area, &mut self.state.clone()); } else { let block = Block::default() - .border_style(style::border(props.focus)) + .border_style(style::border(self.props.focus)) .border_type(BorderType::Rounded) .borders(borders); diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 5b041b94..4e43c3db 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -10,7 +10,7 @@ use ratatui::widgets::{BorderType, Borders, Row}; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::style; -use super::{Render, Widget}; +use super::{Column, Render, Widget}; #[derive(Debug)] pub struct FooterProps<'a, const W: usize> { @@ -105,9 +105,8 @@ impl<'a, A, const W: usize> Render> for Footer { } #[derive(Debug)] -pub struct HeaderProps<'a, const W: usize> { - pub cells: [Text<'a>; W], - pub widths: [Constraint; W], +pub struct HeaderProps { + pub columns: Vec, pub cutoff: usize, pub cutoff_after: usize, pub focus: bool, @@ -143,9 +142,31 @@ impl Widget for Header { fn handle_key_event(&mut self, _key: Key) {} } -impl<'a, A, const W: usize> Render> for Header { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: HeaderProps) { - let widths = props.widths.to_vec(); +impl Render for Header { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: HeaderProps) { + let widths: Vec = props + .columns + .iter() + .filter_map(|column| { + if !column.skip { + Some(column.width) + } else { + None + } + }) + .collect(); + let cells = props + .columns + .iter() + .filter_map(|column| { + if !column.skip { + Some(column.title.clone()) + } else { + None + } + }) + .collect::>(); + let widths = if area.width < props.cutoff as u16 { widths.iter().take(props.cutoff_after).collect::>() } else { @@ -165,7 +186,7 @@ impl<'a, A, const W: usize> Render> for Header { .horizontal_margin(1) .split(area); - let header = Row::new(props.cells).style(style::reset().bold()); + let header = Row::new(cells).style(style::reset().bold()); let header = ratatui::widgets::Table::default() .column_spacing(1) .header(header)