refactor: bind the start and end ranges for tables

This commit is contained in:
ClementTsang 2022-05-01 15:45:52 -04:00
parent 64ed45083e
commit c296b8bf5a
3 changed files with 26 additions and 21 deletions

View File

@ -1,4 +1,4 @@
use std::borrow::Cow; use std::{borrow::Cow, cmp::min};
use concat_string::concat_string; use concat_string::concat_string;
use tui::{ use tui::{
@ -29,8 +29,8 @@ pub struct TextTable<'a> {
/// The border style. /// The border style.
pub border_style: Style, pub border_style: Style,
/// The highlighted entry style. /// The highlighted text style.
pub highlighted_style: Style, pub highlighted_text_style: Style,
/// The graph title. /// The graph title.
pub title: Cow<'a, str>, pub title: Cow<'a, str>,
@ -105,19 +105,24 @@ impl<'a> TextTable<'a> {
} else { } else {
self.table_gap self.table_gap
}; };
let start_position = get_start_position(
usize::from( let sliced_vec = {
(draw_loc.height + (1 - table_gap)).saturating_sub(self.table_height_offset), let num_rows = usize::from(
), (draw_loc.height + 1 - table_gap).saturating_sub(self.table_height_offset),
&state.scroll_direction, );
&mut state.scroll_bar, let start = get_start_position(
state.current_scroll_position, num_rows,
self.is_force_redraw, &state.scroll_direction,
); &mut state.scroll_bar,
state.table_state.select(Some( state.current_scroll_position,
state.current_scroll_position.saturating_sub(start_position), self.is_force_redraw,
)); );
let sliced_vec = &table_data.data[start_position..]; 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 // Calculate widths
if self.recalculate_column_widths { if self.recalculate_column_widths {
@ -187,7 +192,7 @@ impl<'a> TextTable<'a> {
Table::new(disk_rows) Table::new(disk_rows)
.block(disk_block) .block(disk_block)
.header(header) .header(header)
.highlight_style(self.highlighted_style) .highlight_style(self.highlighted_text_style)
.style(self.text_style) .style(self.text_style)
.widths( .widths(
&(widths &(widths

View File

@ -14,7 +14,7 @@ impl Painter {
if let Some(disk_widget_state) = app_state.disk_state.widget_states.get_mut(&widget_id) { 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 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.highlighted_border_style,
self.colours.currently_selected_text_style, self.colours.currently_selected_text_style,
@ -29,7 +29,7 @@ impl Painter {
recalculate_column_widths, recalculate_column_widths,
header_style: self.colours.table_header_style, header_style: self.colours.table_header_style,
border_style, border_style,
highlighted_style, highlighted_text_style,
title: " Disks ".into(), title: " Disks ".into(),
is_expanded: app_state.is_expanded, is_expanded: app_state.is_expanded,
is_on_widget, is_on_widget,

View File

@ -14,7 +14,7 @@ impl Painter {
if let Some(temp_widget_state) = app_state.temp_state.widget_states.get_mut(&widget_id) { 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 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.highlighted_border_style,
self.colours.currently_selected_text_style, self.colours.currently_selected_text_style,
@ -29,7 +29,7 @@ impl Painter {
recalculate_column_widths, recalculate_column_widths,
header_style: self.colours.table_header_style, header_style: self.colours.table_header_style,
border_style, border_style,
highlighted_style, highlighted_text_style,
title: " Temperatures ".into(), title: " Temperatures ".into(),
is_expanded: app_state.is_expanded, is_expanded: app_state.is_expanded,
is_on_widget, is_on_widget,