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 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

View File

@ -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,

View File

@ -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,