diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2022-06-03 04:49:39 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-03 04:49:39 -0400 |
commit | c1a7979be7e7635b241d2cfe72726525f495dddb (patch) | |
tree | efc046077349179c9ab0a4f855986cfa4e30bcb0 /src | |
parent | 0c648ed14ae050d32f7da821027495ebbc116da5 (diff) |
refactoring: Move around components and state (#746)
A small refactor to move some state/component files around in terms of file structure and code location. Should have no effect on logic.
Diffstat (limited to 'src')
24 files changed, 126 insertions, 123 deletions
@@ -16,6 +16,7 @@ use layout_manager::*; pub use states::*; use crate::{ + components::text_table::SortState, constants, data_conversion::ConvertedData, options::Config, diff --git a/src/app/states.rs b/src/app/states.rs index 3dff4644..0a26e368 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -4,13 +4,11 @@ use unicode_segmentation::GraphemeCursor; use crate::{ app::{layout_manager::BottomWidgetType, query::*}, + components::text_table::{CellContent, TableComponentColumn, TableComponentState, WidthBounds}, constants, }; -pub mod table_state; -pub use table_state::*; - -use super::widgets::ProcWidget; +use super::widgets::{DiskWidgetState, ProcWidget, TempWidgetState}; #[derive(Debug)] pub enum ScrollDirection { @@ -277,32 +275,6 @@ impl MemState { } } -pub struct TempWidgetState { - pub table_state: TableComponentState, -} - -impl Default for TempWidgetState { - fn default() -> Self { - const TEMP_HEADERS: [&str; 2] = ["Sensor", "Temp"]; - const WIDTHS: [WidthBounds; TEMP_HEADERS.len()] = [ - WidthBounds::soft_from_str(TEMP_HEADERS[0], Some(0.8)), - WidthBounds::soft_from_str(TEMP_HEADERS[1], None), - ]; - - TempWidgetState { - table_state: TableComponentState::new( - TEMP_HEADERS - .iter() - .zip(WIDTHS) - .map(|(header, width)| { - TableComponentColumn::new_custom(CellContent::new(*header, None), width) - }) - .collect(), - ), - } - } -} - pub struct TempState { pub widget_states: HashMap<u64, TempWidgetState>, } @@ -321,37 +293,6 @@ impl TempState { } } -pub struct DiskWidgetState { - pub table_state: TableComponentState, -} - -impl Default for DiskWidgetState { - fn default() -> Self { - const DISK_HEADERS: [&str; 7] = ["Disk", "Mount", "Used", "Free", "Total", "R/s", "W/s"]; - const WIDTHS: [WidthBounds; DISK_HEADERS.len()] = [ - WidthBounds::soft_from_str(DISK_HEADERS[0], Some(0.2)), - WidthBounds::soft_from_str(DISK_HEADERS[1], Some(0.2)), - WidthBounds::Hard(4), - WidthBounds::Hard(6), - WidthBounds::Hard(6), - WidthBounds::Hard(7), - WidthBounds::Hard(7), - ]; - - DiskWidgetState { - table_state: TableComponentState::new( - DISK_HEADERS - .iter() - .zip(WIDTHS) - .map(|(header, width)| { - TableComponentColumn::new_custom(CellContent::new(*header, None), width) - }) - .collect(), - ), - } - } -} - pub struct DiskState { pub widget_states: HashMap<u64, DiskWidgetState>, } diff --git a/src/app/widgets.rs b/src/app/widgets.rs index 9d3a54ba..99e9e210 100644 --- a/src/app/widgets.rs +++ b/src/app/widgets.rs @@ -1,2 +1,8 @@ -pub mod process_widget; -pub use process_widget::*; +pub mod process_table_widget; +pub use process_table_widget::*; + +pub mod temperature_table_widget; +pub use temperature_table_widget::*; + +pub mod disk_table_widget; +pub use disk_table_widget::*; diff --git a/src/app/widgets/disk_table_widget.rs b/src/app/widgets/disk_table_widget.rs new file mode 100644 index 00000000..ecffb8cf --- /dev/null +++ b/src/app/widgets/disk_table_widget.rs @@ -0,0 +1,34 @@ +use crate::components::text_table::{ + CellContent, TableComponentColumn, TableComponentState, WidthBounds, +}; + +pub struct DiskWidgetState { + pub table_state: TableComponentState, +} + +impl Default for DiskWidgetState { + fn default() -> Self { + const DISK_HEADERS: [&str; 7] = ["Disk", "Mount", "Used", "Free", "Total", "R/s", "W/s"]; + const WIDTHS: [WidthBounds; DISK_HEADERS.len()] = [ + WidthBounds::soft_from_str(DISK_HEADERS[0], Some(0.2)), + WidthBounds::soft_from_str(DISK_HEADERS[1], Some(0.2)), + WidthBounds::Hard(4), + WidthBounds::Hard(6), + WidthBounds::Hard(6), + WidthBounds::Hard(7), + WidthBounds::Hard(7), + ]; + + DiskWidgetState { + table_state: TableComponentState::new( + DISK_HEADERS + .iter() + .zip(WIDTHS) + .map(|(header, width)| { + TableComponentColumn::new_custom(CellContent::new(*header, None), width) + }) + .collect(), + ), + } + } +} diff --git a/src/app/widgets/process_widget.rs b/src/app/widgets/process_table_widget.rs index f49ab40a..ed5226cb 100644 --- a/src/app/widgets/process_widget.rs +++ b/src/app/widgets/process_table_widget.rs @@ -3,8 +3,11 @@ use crate::{ data_farmer::{DataCollection, ProcessData, StringPidMap}, data_harvester::processes::ProcessHarvest, query::*, - AppSearchState, CellContent, ScrollDirection, SortOrder, SortState, SortableState, - TableComponentColumn, TableComponentHeader, TableComponentState, WidthBounds, + AppSearchState, ScrollDirection, SortState, + }, + components::text_table::{ + CellContent, SortOrder, SortableState, TableComponentColumn, TableComponentHeader, + TableComponentState, WidthBounds, }, data_conversion::{binary_byte_string, dec_bytes_per_second_string, TableData, TableRow}, utils::gen_util::sort_partial_fn, diff --git a/src/app/widgets/temperature_table_widget.rs b/src/app/widgets/temperature_table_widget.rs new file mode 100644 index 00000000..aa0b2d2e --- /dev/null +++ b/src/app/widgets/temperature_table_widget.rs @@ -0,0 +1,29 @@ +use crate::components::text_table::{ + CellContent, TableComponentColumn, TableComponentState, WidthBounds, +}; + +pub struct TempWidgetState { + pub table_state: TableComponentState, +} + +impl Default for TempWidgetState { + fn default() -> Self { + const TEMP_HEADERS: [&str; 2] = ["Sensor", "Temp"]; + const WIDTHS: [WidthBounds; TEMP_HEADERS.len()] = [ + WidthBounds::soft_from_str(TEMP_HEADERS[0], Some(0.8)), + WidthBounds::soft_from_str(TEMP_HEADERS[1], None), + ]; + + TempWidgetState { + table_state: TableComponentState::new( + TEMP_HEADERS + .iter() + .zip(WIDTHS) + .map(|(header, width)| { + TableComponentColumn::new_custom(CellContent::new(*header, None), width) + }) + .collect(), + ), + } + } +} diff --git a/src/canvas.rs b/src/canvas.rs index e69c5053..7bfcf284 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -23,10 +23,7 @@ use crate::{ utils::error::BottomError, }; -pub use self::components::Point; - mod canvas_colours; -mod components; mod dialogs; mod drawing_utils; mod widgets; diff --git a/src/canvas/components.rs b/src/canvas/components.rs deleted file mode 100644 index 219c6245..00000000 --- a/src/canvas/components.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! Some common components to reuse when drawing widgets. - -pub mod time_chart; -pub use time_chart::*; - -pub mod time_graph; -pub use time_graph::*; - -pub mod text_table; -pub use text_table::*; diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index f482b75b..3788887a 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -1,11 +1,11 @@ use std::{borrow::Cow, iter}; use crate::{ - app::{layout_manager::WidgetDirection, App, CellContent, CpuWidgetState}, - canvas::{ - components::{GraphData, TextTable, TimeGraph}, - drawing_utils::should_hide_x_label, - Painter, + app::{layout_manager::WidgetDirection, App, CpuWidgetState}, + canvas::{drawing_utils::should_hide_x_label, Painter}, + components::{ + text_table::{CellContent, TextTable}, + time_graph::{GraphData, TimeGraph}, }, data_conversion::{ConvertedCpuData, TableData, TableRow}, }; diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index 1e52cc5f..418ba33a 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -2,10 +2,8 @@ use tui::{backend::Backend, layout::Rect, terminal::Frame}; use crate::{ app, - canvas::{ - components::{TextTable, TextTableTitle}, - Painter, - }, + canvas::Painter, + components::text_table::{TextTable, TextTableTitle}, }; impl Painter { diff --git a/src/canvas/widgets/mem_graph.rs b/src/canvas/widgets/mem_graph.rs index 442044aa..7ef2fff4 100644 --- a/src/canvas/widgets/mem_graph.rs +++ b/src/canvas/widgets/mem_graph.rs @@ -2,11 +2,8 @@ use std::borrow::Cow; use crate::{ app::App, - canvas::{ - components::{GraphData, TimeGraph}, - drawing_utils::should_hide_x_label, - Painter, - }, + canvas::{drawing_utils::should_hide_x_label, Painter}, + components::time_graph::{GraphData, TimeGraph}, }; use tui::{ diff --git a/src/canvas/widgets/network_graph.rs b/src/canvas/widgets/network_graph.rs index f96be42f..c70f496d 100644 --- a/src/canvas/widgets/network_graph.rs +++ b/src/canvas/widgets/network_graph.rs @@ -1,10 +1,7 @@ use crate::{ app::{App, AxisScaling}, - canvas::{ - components::{GraphData, TimeGraph}, - drawing_utils::should_hide_x_label, - Painter, Point, - }, + canvas::{drawing_utils::should_hide_x_label, Painter}, + components::time_graph::{GraphData, Point, TimeGraph}, units::data_units::DataUnit, utils::gen_util::*, }; diff --git a/src/canvas/widgets/process_table.rs b/src/canvas/widgets/process_table.rs index b38d09cb..2aa07b3e 100644 --- a/src/canvas/widgets/process_table.rs +++ b/src/canvas/widgets/process_table.rs @@ -1,10 +1,7 @@ use crate::{ app::App, - canvas::{ - components::{TextTable, TextTableTitle}, - drawing_utils::get_search_start_position, - Painter, - }, + canvas::{drawing_utils::get_search_start_position, Painter}, + components::text_table::{TextTable, TextTableTitle}, constants::*, data_conversion::{TableData, TableRow}, }; diff --git a/src/canvas/widgets/temp_table.rs b/src/canvas/widgets/temp_table.rs index 45554faa..4d49ae62 100644 --- a/src/canvas/widgets/temp_table.rs +++ b/src/canvas/widgets/temp_table.rs @@ -2,10 +2,8 @@ use tui::{backend::Backend, layout::Rect, terminal::Frame}; use crate::{ app, - canvas::{ - components::{TextTable, TextTableTitle}, - Painter, - }, + canvas::Painter, + components::text_table::{TextTable, TextTableTitle}, }; impl Painter { diff --git a/src/components.rs b/src/components.rs new file mode 100644 index 00000000..f66a2063 --- /dev/null +++ b/src/components.rs @@ -0,0 +1,5 @@ +mod tui_widget; + +pub mod time_graph; + +pub mod text_table; diff --git a/src/components/text_table.rs b/src/components/text_table.rs new file mode 100644 index 00000000..83939ccf --- /dev/null +++ b/src/components/text_table.rs @@ -0,0 +1,5 @@ +pub mod draw; +pub use draw::*; + +pub mod state; +pub use state::*; diff --git a/src/canvas/components/text_table.rs b/src/components/text_table/draw.rs index f5fc31e7..e62734b6 100644 --- a/src/canvas/components/text_table.rs +++ b/src/components/text_table/draw.rs @@ -15,14 +15,17 @@ use tui::{ use unicode_segmentation::UnicodeSegmentation; use crate::{ - app::{ - self, layout_manager::BottomWidget, CellContent, SortState, TableComponentColumn, - TableComponentHeader, TableComponentState, WidthBounds, - }, + app::{self, layout_manager::BottomWidget}, + components::text_table::SortOrder, constants::{SIDE_BORDERS, TABLE_GAP_HEIGHT_LIMIT}, data_conversion::{TableData, TableRow}, }; +use super::{ + CellContent, SortState, TableComponentColumn, TableComponentHeader, TableComponentState, + WidthBounds, +}; + pub struct TextTableTitle<'a> { pub title: Cow<'a, str>, pub is_expanded: bool, @@ -301,8 +304,8 @@ fn build_header<'a, H: TableComponentHeader>( let index = s.current_index; let arrow = match order { - app::SortOrder::Ascending => UP_ARROW, - app::SortOrder::Descending => DOWN_ARROW, + SortOrder::Ascending => UP_ARROW, + SortOrder::Descending => DOWN_ARROW, }; Either::Right(columns.iter().enumerate().filter_map(move |(itx, c)| { diff --git a/src/app/states/table_state.rs b/src/components/text_table/state.rs index c6ce3b46..3a06bbe6 100644 --- a/src/app/states/table_state.rs +++ b/src/components/text_table/state.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, convert::TryInto, ops::Range}; use itertools::Itertools; use tui::{layout::Rect, widgets::TableState}; -use super::ScrollDirection; +use crate::app::ScrollDirection; /// A bound on the width of a column. #[derive(Clone, Copy, Debug)] diff --git a/src/canvas/components/time_graph.rs b/src/components/time_graph.rs index 7d1cac59..74589990 100644 --- a/src/canvas/components/time_graph.rs +++ b/src/components/time_graph.rs @@ -13,7 +13,7 @@ use tui::{ use concat_string::concat_string; use unicode_segmentation::UnicodeSegmentation; -use super::{Axis, Dataset, TimeChart}; +use super::tui_widget::time_chart::{Axis, Dataset, TimeChart, DEFAULT_LEGEND_CONSTRAINTS}; /// A single graph point. pub type Point = (f64, f64); @@ -156,7 +156,7 @@ impl<'a> TimeGraph<'a> { .legend_style(self.graph_style) .hidden_legend_constraints( self.legend_constraints - .unwrap_or(super::DEFAULT_LEGEND_CONSTRAINTS), + .unwrap_or(DEFAULT_LEGEND_CONSTRAINTS), ), draw_loc, ) @@ -194,7 +194,7 @@ mod test { text::{Span, Spans}, }; - use crate::canvas::components::Axis; + use crate::components::tui_widget::time_chart::Axis; use super::TimeGraph; @@ -251,17 +251,17 @@ mod test { #[test] fn time_graph_gen_title() { - let mut tg = create_time_graph(); + let mut time_graph = create_time_graph(); let draw_loc = Rect::new(0, 0, 32, 100); - let title = tg.generate_title(draw_loc); + let title = time_graph.generate_title(draw_loc); assert_eq!( title, Spans::from(Span::styled(" Network ", Style::default().fg(Color::Cyan))) ); - tg.is_expanded = true; - let title = tg.generate_title(draw_loc); + time_graph.is_expanded = true; + let title = time_graph.generate_title(draw_loc); assert_eq!( title, Spans::from(vec![ diff --git a/src/components/tui_widget.rs b/src/components/tui_widget.rs new file mode 100644 index 00000000..a4e0978a --- /dev/null +++ b/src/components/tui_widget.rs @@ -0,0 +1 @@ +pub mod time_chart; diff --git a/src/canvas/components/time_chart.rs b/src/components/tui_widget/time_chart.rs index 7b791d1d..7b791d1d 100644 --- a/src/canvas/components/time_chart.rs +++ b/src/components/tui_widget/time_chart.rs diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 98bbee2c..688e4b03 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -1,8 +1,8 @@ //! This mainly concerns converting collected data into things that the canvas //! can actually handle. -use crate::app::CellContent; -use crate::canvas::Point; +use crate::components::text_table::CellContent; +use crate::components::time_graph::Point; use crate::{app::AxisScaling, units::data_units::DataUnit, Pid}; use crate::{ app::{data_farmer, data_harvester, App}, @@ -44,6 +44,7 @@ pub mod utils { } pub mod canvas; pub mod clap; +pub mod components; pub mod constants; pub mod data_conversion; pub mod options; diff --git a/src/options.rs b/src/options.rs index 2ff82be9..30a12f08 100644 --- a/src/options.rs +++ b/src/options.rs @@ -12,7 +12,7 @@ use std::{ use crate::{ app::{ layout_manager::*, - widgets::{ProcWidget, ProcWidgetMode}, + widgets::{DiskWidgetState, ProcWidget, ProcWidgetMode, TempWidgetState}, *, }, canvas::ColourScheme, |