/// Render the frame for the table
/// Re-calculates the table frame so it can be drawn on screen
Render the frame for the table
Re-calculates the table frame so it can be drawn on screen
Render the frame for the table Re - calculates the table frame so it can be drawn on screen
pub fn table_ui(&mut self, f: &mut Frame<CrosstermBackend<Stdout>>) {
        let rects = Layout::default()
            .constraints([Constraint::Percentage(100)].as_ref())
            .margin(0)
            .split(f.size());
        // Calculate the widths based on # of columns
        let equal_column_width = if self.num_columns() > 1 {
            (100 / self.num_columns()) as u16
        } else {
            // If you're here, there isn't any headers set. Likely no data too.
            // This is just going to prevent immediate panic from division by zero
            100
        };
        let mut column_constraints: Vec<Constraint> = Vec::new();
        // TODO: Maybe provide a way to size the width of a column
        // Define the widths of the columns
        for _ in 0..self.num_columns() {
            column_constraints.push(Constraint::Percentage(equal_column_width));
        }
        let selected_symbol = format!("{} >> ", self.current_selected());
        let selected_style = Style::default().add_modifier(Modifier::REVERSED);
        let header_cells = self.columns.iter().map(|column| {
            // If the column has an alternative label, use that
            // Otherwise, use the key path
            let header_label = if let Some(label) = column.header_label.clone() {
                label
            } else {
                // TODO: Test this with a nested key.
                // We probably want to use the inner-most key in the path if no label given
                column.key_path.clone()
            };
            Cell::from(header_label)
        });
        let header_style = Style::default()
            .bg(Color::Blue)
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD);
        let header = Row::new(header_cells)
            .style(header_style)
            .height(1)
            .bottom_margin(0);
        // render rows based on header order
        let mut rows = vec![];
        for (_index, row_data) in self.data.iter().enumerate() {
            let mut cells = vec![];
            for col in &self.columns {
                let key_path = col.key_path.as_str();
                let value = if let Some(v) = row_data.get(key_path) {
                    v
                } else {
                    // If the user provides a key that doesn't exist
                    // we want to print a column, but leave the value blank
                    ""
                };
                cells.push(Cell::from(value));
            }
            //rows.push(Row::new(cells).height(height as u16).bottom_margin(0))
            rows.push(Row::new(cells).height(1).bottom_margin(0))
        }
        let table_title_text = format!(
            "('c' to clear table | 'q' or ESC to exit) | Items: {}",
            self.data.len()
        );
        let t = Table::new(rows)
            .header(header)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(table_title_text),
            )
            .highlight_style(selected_style)
            .highlight_symbol(selected_symbol.as_str())
            .widths(&column_constraints);
        // draw
        f.render_stateful_widget(t, rects[0], &mut self.state);
    }
pub fn table_ui ( & mut self , f : & mut Frame < CrosstermBackend < Stdout > > ) { let rects = Layout :: default ( ) . constraints ( [ Constraint :: Percentage ( 100 ) ] . as_ref ( ) ) . margin ( 0 ) . split ( f . size ( ) ) ; // Calculate the widths based on # of columns let equal_column_width = if self . num_columns ( ) > 1 { ( 100 / self . num_columns ( ) ) as u16 } else { // If you're here, there isn't any headers set. Likely no data too. // This is just going to prevent immediate panic from division by zero 100 } ; let mut column_constraints : Vec < Constraint > = Vec :: new ( ) ; // TODO: Maybe provide a way to size the width of a column // Define the widths of the columns for _ in 0 .. self . num_columns ( ) { column_constraints . push ( Constraint :: Percentage ( equal_column_width ) ) ; } let selected_symbol = format ! ( " " self current_selected ( ) ) ; let selected_style = Style :: default ( ) . add_modifier ( Modifier :: REVERSED ) ; let header_cells = self . columns . iter ( ) . map ( | column | { // If the column has an alternative label, use that // Otherwise, use the key path let header_label = if let Some ( label ) = column . header_label . clone ( ) { label } else { // TODO: Test this with a nested key. // We probably want to use the inner-most key in the path if no label given column . key_path . clone ( ) } ; Cell :: from ( header_label ) } ) ; let header_style = Style :: default ( ) . bg ( Color :: Blue ) . fg ( Color :: Yellow ) . add_modifier ( Modifier :: BOLD ) ; let header = Row :: new ( header_cells ) . style ( header_style ) . height ( 1 ) . bottom_margin ( 0 ) ; // render rows based on header order let mut rows = vec ! [ ] ; for ( _index , row_data ) in self . data . iter ( ) . enumerate ( ) { let mut cells = vec ! [ ] ; for col in & self . columns { let key_path = col . key_path . as_str ( ) ; let value = if let Some ( v ) = row_data . get ( key_path ) { v } else { // If the user provides a key that doesn't exist // we want to print a column, but leave the value blank " " } ; cells . push ( Cell :: from ( value ) ) ; } //rows.push(Row::new(cells).height(height as u16).bottom_margin(0)) rows . push ( Row :: new ( cells ) . height ( 1 ) . bottom_margin ( 0 ) ) } let table_title_text = format ! ( " " self data len ( ) ) ; let t = Table :: new ( rows ) . header ( header ) . block ( Block :: default ( ) . borders ( Borders :: ALL ) . title ( table_title_text ) , ) . highlight_style ( selected_style ) . highlight_symbol ( selected_symbol . as_str ( ) ) . widths ( & column_constraints ) ; // draw f . render_stateful_widget ( t , rects [ 0 ] , & mut self . state ) ; }
Render the frame for the table
Re-calculates the table frame so it can be drawn on screen
Render the frame for the table Re - calculates the table frame so it can be drawn on screen
&mut self
f
&mut Frame<CrosstermBackend<Stdout>>