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.
This commit is contained in:
Clement Tsang 2022-06-03 04:49:39 -04:00 committed by GitHub
parent 0c648ed14a
commit c1a7979be7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 126 additions and 123 deletions

View file

@ -16,6 +16,7 @@ use layout_manager::*;
pub use states::*;
use crate::{
components::text_table::SortState,
constants,
data_conversion::ConvertedData,
options::Config,

View file

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

View file

@ -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::*;

View file

@ -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(),
),
}
}
}

View file

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

View file

@ -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(),
),
}
}
}

View file

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

View file

@ -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::*;

View file

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

View file

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

View file

@ -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::{

View file

@ -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::*,
};

View file

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

View file

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

5
src/components.rs Normal file
View file

@ -0,0 +1,5 @@
mod tui_widget;
pub mod time_graph;
pub mod text_table;

View file

@ -0,0 +1,5 @@
pub mod draw;
pub use draw::*;
pub mod state;
pub use state::*;

View file

@ -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)| {

View file

@ -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)]

View file

@ -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![

View file

@ -0,0 +1 @@
pub mod time_chart;

View file

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

View file

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

View file

@ -12,7 +12,7 @@ use std::{
use crate::{
app::{
layout_manager::*,
widgets::{ProcWidget, ProcWidgetMode},
widgets::{DiskWidgetState, ProcWidget, ProcWidgetMode, TempWidgetState},
*,
},
canvas::ColourScheme,