| 1 | use std::{ |
| 2 | cmp::{max, min}, |
| 3 | iter::once, |
| 4 | }; |
| 5 | |
| 6 | use concat_string::concat_string; |
| 7 | use ratatui::{ |
| 8 | Frame, |
| 9 | layout::{Constraint, Direction, Layout, Rect}, |
| 10 | symbols::line, |
| 11 | text::{Line, Span, Text}, |
| 12 | widgets::{Block, Cell, Padding, Row, Table}, |
| 13 | }; |
| 14 | |
| 15 | use super::{ |
| 16 | CalculateColumnWidths, ColumnHeader, ColumnWidthBounds, DataTable, DataTableColumn, DataToCell, |
| 17 | SortType, |
| 18 | }; |
| 19 | use crate::{ |
| 20 | app::layout_manager::BottomWidget, |
| 21 | canvas::{ |
| 22 | Painter, |
| 23 | components::scroll_bar::{ScrollBarArgs, draw_scroll_bar}, |
| 24 | drawing_utils::widget_block, |
| 25 | }, |
| 26 | constants::TABLE_GAP_HEIGHT_LIMIT, |
| 27 | options::config::flags::TableGap, |
| 28 | utils::strings::truncate_to_text, |
| 29 | }; |
| 30 | |
| 31 | pub enum SelectionState { |
| 32 | NotSelected, |
| 33 | Selected, |
| 34 | Expanded, |
| 35 | } |
| 36 | |
| 37 | impl SelectionState { |
| 38 | pub fn new(is_expanded: bool, is_on_widget: bool) -> Self { |
| 39 | if is_expanded { |
| 40 | SelectionState::Expanded |
| 41 | } else if is_on_widget { |
| 42 | SelectionState::Selected |
| 43 | } else { |
| 44 | SelectionState::NotSelected |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// A [`DrawInfo`] is information required on each draw call. |
| 50 | pub struct DrawInfo { |
| 51 | pub loc: Rect, |
| 52 | pub force_redraw: bool, |
| 53 | pub recalculate_column_widths: bool, |
| 54 | pub selection_state: SelectionState, |
| 55 | } |
| 56 | |
| 57 | impl DrawInfo { |
| 58 | pub fn is_on_widget(&self) -> bool { |
| 59 | !matches!(self.selection_state, SelectionState::NotSelected) |
| 60 | } |
| 61 | |
| 62 | pub fn is_expanded(&self) -> bool { |
| 63 | matches!(self.selection_state, SelectionState::Expanded) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl<DataType, H, S, C> DataTable<DataType, H, S, C> |
| 68 | where |
| 69 | DataType: DataToCell<H>, |
| 70 | H: ColumnHeader, |
| 71 | S: SortType, |
| 72 | C: DataTableColumn<H>, |
| 73 | { |
| 74 | fn block<'a>(&self, draw_info: &'a DrawInfo, data_len: usize) -> Block<'a> { |
| 75 | let is_selected = match draw_info.selection_state { |
| 76 | SelectionState::NotSelected => false, |
| 77 | SelectionState::Selected | SelectionState::Expanded => true, |
| 78 | }; |
| 79 | |
| 80 | let border_style = if is_selected { |
| 81 | self.styling.highlighted_border_style |
| 82 | } else { |
| 83 | self.styling.border_style |
| 84 | }; |
| 85 | |
| 86 | let mut block = widget_block( |
| 87 | self.props.is_basic, |
| 88 | is_selected, |
| 89 | self.styling.border_type, |
| 90 | self.styling.general_widget_style, |
| 91 | ) |
| 92 | .border_style(border_style); |
| 93 | |
| 94 | if let Some((left_title, right_title)) = self.generate_title(draw_info, data_len) { |
| 95 | if !self.props.is_basic { |
| 96 | block = block.title_top(left_title); |
| 97 | } |
| 98 | |
| 99 | if let Some(right_title) = right_title { |
| 100 | block = block.title_top(right_title); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | block |
| 105 | } |
| 106 | |
| 107 | /// Generates a title, given the available space. |
| 108 | fn generate_title( |
| 109 | &self, draw_info: &'_ DrawInfo, total_items: usize, |
| 110 | ) -> Option<(Line<'static>, Option<Line<'static>>)> { |
| 111 | self.props.title.as_ref().map(|title| { |
| 112 | let current_index = self.state.current_index.saturating_add(1); |
| 113 | let draw_loc = draw_info.loc; |
| 114 | let title_style = self.styling.title_style; |
| 115 | |
| 116 | let title = if self.props.show_table_scroll_position { |
| 117 | let pos = current_index.to_string(); |
| 118 | let tot = total_items.to_string(); |
| 119 | let title_string = concat_string!(title, "(", pos, " of ", tot, ") "); |
| 120 | |
| 121 | if title_string.len() + 2 <= draw_loc.width.into() { |
| 122 | title_string |
| 123 | } else { |
| 124 | title.to_string() |
| 125 | } |
| 126 | } else { |
| 127 | title.to_string() |
| 128 | }; |
| 129 | |
| 130 | let left_title = Line::from(Span::styled(title, title_style)).left_aligned(); |
| 131 | |
| 132 | let right_title = if draw_info.is_expanded() { |
| 133 | Some(Line::from(" Esc to go back ").right_aligned()) |
| 134 | } else { |
| 135 | None |
| 136 | }; |
| 137 | |
| 138 | (left_title, right_title) |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | pub fn draw( |
| 143 | &mut self, f: &mut Frame<'_>, draw_info: &DrawInfo, widget: Option<&mut BottomWidget>, |
| 144 | painter: &Painter, |
| 145 | ) { |
| 146 | let draw_loc = draw_info.loc; |
| 147 | let margined_draw_loc = Layout::default() |
| 148 | .constraints([Constraint::Percentage(100)]) |
| 149 | .direction(Direction::Horizontal) |
| 150 | .split(draw_loc)[0]; |
| 151 | |
| 152 | let mut block = self.block(draw_info, self.data.len()); |
| 153 | |
| 154 | let horizontal_padding = if self.props.is_basic && !draw_info.is_on_widget() { |
| 155 | 1 |
| 156 | } else { |
| 157 | 0 |
| 158 | }; |
| 159 | let right_padding = horizontal_padding + u16::from(self.props.show_table_scroll_bar); |
| 160 | block = block.padding(Padding::new(horizontal_padding, right_padding, 0, 0)); |
| 161 | |
| 162 | let (inner_width, inner_height) = { |
| 163 | let inner_rect = block.inner(margined_draw_loc); |
| 164 | self.state.inner_rect = inner_rect; |
| 165 | (inner_rect.width, inner_rect.height) |
| 166 | }; |
| 167 | |
| 168 | if inner_width == 0 || inner_height == 0 { |
| 169 | f.render_widget(block, margined_draw_loc); |
| 170 | } else { |
| 171 | // Calculate widths |
| 172 | if draw_info.recalculate_column_widths { |
| 173 | let col_widths = DataType::column_widths(&self.data, &self.columns); |
| 174 | |
| 175 | self.columns |
| 176 | .iter_mut() |
| 177 | .zip(&col_widths) |
| 178 | .for_each(|(column, &width)| { |
| 179 | let header_len = column.header_len() as u16; |
| 180 | if let ColumnWidthBounds::Soft { |
| 181 | desired, |
| 182 | max_percentage: _, |
| 183 | } = &mut column.bounds_mut() |
| 184 | { |
| 185 | *desired = max(header_len, width); |
| 186 | } |
| 187 | }); |
| 188 | |
| 189 | self.state.calculated_widths = self |
| 190 | .columns |
| 191 | .calculate_column_widths(inner_width, self.props.left_to_right); |
| 192 | |
| 193 | // Update draw loc in widget map |
| 194 | if let Some(widget) = widget { |
| 195 | widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); |
| 196 | widget.bottom_right_corner = |
| 197 | Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | let show_header = inner_height > 1; |
| 202 | let header_height = u16::from(show_header); |
| 203 | let table_gap_setting = self.props.table_gap; |
| 204 | let table_gap = if !show_header || draw_loc.height < TABLE_GAP_HEIGHT_LIMIT { |
| 205 | 0 |
| 206 | } else { |
| 207 | table_gap_setting.height() |
| 208 | }; |
| 209 | |
| 210 | if !self.data.is_empty() || !self.first_draw { |
| 211 | if self.first_draw { |
| 212 | // TODO: Doing it this way is fine, but it could be done better (e.g. showing |
| 213 | // custom no results/entries message) |
| 214 | self.first_draw = false; |
| 215 | if let Some(first_index) = self.first_index { |
| 216 | self.set_position(first_index); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | let num_rows: usize = inner_height |
| 221 | .saturating_sub(table_gap + header_height) |
| 222 | .into(); |
| 223 | |
| 224 | let columns = &self.columns; |
| 225 | let rows = { |
| 226 | self.state |
| 227 | .get_start_position(num_rows, draw_info.force_redraw); |
| 228 | let start = self.state.display_start_index; |
| 229 | let end = min(self.data.len(), start + num_rows); |
| 230 | self.state |
| 231 | .table_state |
| 232 | .select(Some(self.state.current_index.saturating_sub(start))); |
| 233 | |
| 234 | self.data[start..end].iter().map(|data_row| { |
| 235 | let row = Row::new( |
| 236 | columns |
| 237 | .iter() |
| 238 | .zip(&self.state.calculated_widths) |
| 239 | .filter_map(|(column, &width)| { |
| 240 | data_row.to_cell_text(column.inner(), width).map(|content| { |
| 241 | let content = truncate_to_text(&content, width.get()); |
| 242 | |
| 243 | if let Some(style) = |
| 244 | data_row.style_cell(column.inner(), painter) |
| 245 | { |
| 246 | Cell::new(content).style(style) |
| 247 | } else { |
| 248 | Cell::new(content) |
| 249 | } |
| 250 | }) |
| 251 | }), |
| 252 | ); |
| 253 | |
| 254 | data_row.style_row(row, painter) |
| 255 | }) |
| 256 | }; |
| 257 | |
| 258 | let headers = self |
| 259 | .sort_type |
| 260 | .build_header(columns, &self.state.calculated_widths) |
| 261 | .style(self.styling.header_style) |
| 262 | .bottom_margin(table_gap); |
| 263 | |
| 264 | let widget = { |
| 265 | let highlight_style = if draw_info.is_on_widget() |
| 266 | || self.props.show_current_entry_when_unfocused |
| 267 | { |
| 268 | self.styling.highlighted_text_style |
| 269 | } else { |
| 270 | self.styling.text_style |
| 271 | }; |
| 272 | let mut table = Table::new( |
| 273 | rows, |
| 274 | self.state.calculated_widths.iter().map(|nzu| nzu.get()), |
| 275 | ) |
| 276 | .block(block) |
| 277 | .row_highlight_style(highlight_style) |
| 278 | .style(self.styling.text_style); |
| 279 | |
| 280 | if show_header { |
| 281 | table = table.header(headers); |
| 282 | } |
| 283 | |
| 284 | table |
| 285 | }; |
| 286 | |
| 287 | let table_state = &mut self.state.table_state; |
| 288 | f.render_stateful_widget(widget, margined_draw_loc, table_state); |
| 289 | |
| 290 | if self.props.show_table_scroll_bar { |
| 291 | let scrollbar_area = Rect { |
| 292 | x: self.state.inner_rect.x + self.state.inner_rect.width, |
| 293 | y: self.state.inner_rect.y + header_height + table_gap, |
| 294 | width: 1, |
| 295 | height: num_rows as u16, |
| 296 | }; |
| 297 | |
| 298 | draw_scroll_bar( |
| 299 | f, |
| 300 | scrollbar_area, |
| 301 | ScrollBarArgs { |
| 302 | content_length: self.data.len(), |
| 303 | viewport_length: num_rows, |
| 304 | position: self.state.current_index, |
| 305 | style: self.styling.text_style, |
| 306 | }, |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | if table_gap > 0 && table_gap_setting == TableGap::Line && show_header { |
| 311 | let y = self.state.inner_rect.y + header_height; |
| 312 | let buf = f.buffer_mut(); |
| 313 | let border_style = if draw_info.is_on_widget() { |
| 314 | self.styling.highlighted_border_style |
| 315 | } else { |
| 316 | self.styling.border_style |
| 317 | }; |
| 318 | |
| 319 | for x in (draw_loc.x + 1)..(draw_loc.x + draw_loc.width - 1) { |
| 320 | if let Some(cell) = buf.cell_mut((x, y)) { |
| 321 | cell.set_symbol(line::NORMAL.horizontal); |
| 322 | cell.set_style(border_style); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if !self.props.is_basic || draw_info.is_on_widget() { |
| 327 | if let Some(cell) = buf.cell_mut((draw_loc.x, y)) { |
| 328 | cell.set_symbol(line::NORMAL.vertical_right); |
| 329 | cell.set_style(border_style); |
| 330 | } |
| 331 | |
| 332 | if let Some(cell) = buf.cell_mut((draw_loc.x + draw_loc.width - 1, y)) { |
| 333 | cell.set_symbol(line::NORMAL.vertical_left); |
| 334 | cell.set_style(border_style); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } else { |
| 339 | let table = Table::new( |
| 340 | once(Row::new(Text::raw("No data"))), |
| 341 | [Constraint::Percentage(100)], |
| 342 | ) |
| 343 | .block(block) |
| 344 | .style(self.styling.text_style); |
| 345 | f.render_widget(table, margined_draw_loc); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |