From c296b8bf5aacf200995db6312c0e92233b65b766 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sun, 1 May 2022 15:45:52 -0400 Subject: [PATCH] refactor: bind the start and end ranges for tables --- src/canvas/components/text_table.rs | 39 ++++++++++++++++------------- src/canvas/widgets/disk_table.rs | 4 +-- src/canvas/widgets/temp_table.rs | 4 +-- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/canvas/components/text_table.rs b/src/canvas/components/text_table.rs index c1119c43..767b8e6c 100644 --- a/src/canvas/components/text_table.rs +++ b/src/canvas/components/text_table.rs @@ -1,4 +1,4 @@ -use std::borrow::Cow; +use std::{borrow::Cow, cmp::min}; use concat_string::concat_string; use tui::{ @@ -29,8 +29,8 @@ pub struct TextTable<'a> { /// The border style. pub border_style: Style, - /// The highlighted entry style. - pub highlighted_style: Style, + /// The highlighted text style. + pub highlighted_text_style: Style, /// The graph title. pub title: Cow<'a, str>, @@ -105,19 +105,24 @@ impl<'a> TextTable<'a> { } else { self.table_gap }; - let start_position = get_start_position( - usize::from( - (draw_loc.height + (1 - table_gap)).saturating_sub(self.table_height_offset), - ), - &state.scroll_direction, - &mut state.scroll_bar, - state.current_scroll_position, - self.is_force_redraw, - ); - state.table_state.select(Some( - state.current_scroll_position.saturating_sub(start_position), - )); - let sliced_vec = &table_data.data[start_position..]; + + let sliced_vec = { + let num_rows = usize::from( + (draw_loc.height + 1 - table_gap).saturating_sub(self.table_height_offset), + ); + let start = get_start_position( + num_rows, + &state.scroll_direction, + &mut state.scroll_bar, + state.current_scroll_position, + self.is_force_redraw, + ); + let end = min(table_data.data.len(), start + num_rows + 1); + state + .table_state + .select(Some(state.current_scroll_position.saturating_sub(start))); + &table_data.data[start..end] + }; // Calculate widths if self.recalculate_column_widths { @@ -187,7 +192,7 @@ impl<'a> TextTable<'a> { Table::new(disk_rows) .block(disk_block) .header(header) - .highlight_style(self.highlighted_style) + .highlight_style(self.highlighted_text_style) .style(self.text_style) .widths( &(widths diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index 8c7adc7d..e897a770 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -14,7 +14,7 @@ impl Painter { if let Some(disk_widget_state) = app_state.disk_state.widget_states.get_mut(&widget_id) { let is_on_widget = app_state.current_widget.widget_id == widget_id; - let (border_style, highlighted_style) = if is_on_widget { + let (border_style, highlighted_text_style) = if is_on_widget { ( self.colours.highlighted_border_style, self.colours.currently_selected_text_style, @@ -29,7 +29,7 @@ impl Painter { recalculate_column_widths, header_style: self.colours.table_header_style, border_style, - highlighted_style, + highlighted_text_style, title: " Disks ".into(), is_expanded: app_state.is_expanded, is_on_widget, diff --git a/src/canvas/widgets/temp_table.rs b/src/canvas/widgets/temp_table.rs index 2e95a13a..890411e2 100644 --- a/src/canvas/widgets/temp_table.rs +++ b/src/canvas/widgets/temp_table.rs @@ -14,7 +14,7 @@ impl Painter { if let Some(temp_widget_state) = app_state.temp_state.widget_states.get_mut(&widget_id) { let is_on_widget = app_state.current_widget.widget_id == widget_id; - let (border_style, highlighted_style) = if is_on_widget { + let (border_style, highlighted_text_style) = if is_on_widget { ( self.colours.highlighted_border_style, self.colours.currently_selected_text_style, @@ -29,7 +29,7 @@ impl Painter { recalculate_column_widths, header_style: self.colours.table_header_style, border_style, - highlighted_style, + highlighted_text_style, title: " Temperatures ".into(), is_expanded: app_state.is_expanded, is_on_widget,