mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-24 22:24:53 +02:00
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:
parent
0c648ed14a
commit
c1a7979be7
@ -16,6 +16,7 @@ use layout_manager::*;
|
|||||||
pub use states::*;
|
pub use states::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
components::text_table::SortState,
|
||||||
constants,
|
constants,
|
||||||
data_conversion::ConvertedData,
|
data_conversion::ConvertedData,
|
||||||
options::Config,
|
options::Config,
|
||||||
|
@ -4,13 +4,11 @@ use unicode_segmentation::GraphemeCursor;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{layout_manager::BottomWidgetType, query::*},
|
app::{layout_manager::BottomWidgetType, query::*},
|
||||||
|
components::text_table::{CellContent, TableComponentColumn, TableComponentState, WidthBounds},
|
||||||
constants,
|
constants,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod table_state;
|
use super::widgets::{DiskWidgetState, ProcWidget, TempWidgetState};
|
||||||
pub use table_state::*;
|
|
||||||
|
|
||||||
use super::widgets::ProcWidget;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ScrollDirection {
|
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 struct TempState {
|
||||||
pub widget_states: HashMap<u64, TempWidgetState>,
|
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 struct DiskState {
|
||||||
pub widget_states: HashMap<u64, DiskWidgetState>,
|
pub widget_states: HashMap<u64, DiskWidgetState>,
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,8 @@
|
|||||||
pub mod process_widget;
|
pub mod process_table_widget;
|
||||||
pub use process_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::*;
|
||||||
|
34
src/app/widgets/disk_table_widget.rs
Normal file
34
src/app/widgets/disk_table_widget.rs
Normal 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(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,8 +3,11 @@ use crate::{
|
|||||||
data_farmer::{DataCollection, ProcessData, StringPidMap},
|
data_farmer::{DataCollection, ProcessData, StringPidMap},
|
||||||
data_harvester::processes::ProcessHarvest,
|
data_harvester::processes::ProcessHarvest,
|
||||||
query::*,
|
query::*,
|
||||||
AppSearchState, CellContent, ScrollDirection, SortOrder, SortState, SortableState,
|
AppSearchState, ScrollDirection, SortState,
|
||||||
TableComponentColumn, TableComponentHeader, TableComponentState, WidthBounds,
|
},
|
||||||
|
components::text_table::{
|
||||||
|
CellContent, SortOrder, SortableState, TableComponentColumn, TableComponentHeader,
|
||||||
|
TableComponentState, WidthBounds,
|
||||||
},
|
},
|
||||||
data_conversion::{binary_byte_string, dec_bytes_per_second_string, TableData, TableRow},
|
data_conversion::{binary_byte_string, dec_bytes_per_second_string, TableData, TableRow},
|
||||||
utils::gen_util::sort_partial_fn,
|
utils::gen_util::sort_partial_fn,
|
29
src/app/widgets/temperature_table_widget.rs
Normal file
29
src/app/widgets/temperature_table_widget.rs
Normal 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(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,10 +23,7 @@ use crate::{
|
|||||||
utils::error::BottomError,
|
utils::error::BottomError,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::components::Point;
|
|
||||||
|
|
||||||
mod canvas_colours;
|
mod canvas_colours;
|
||||||
mod components;
|
|
||||||
mod dialogs;
|
mod dialogs;
|
||||||
mod drawing_utils;
|
mod drawing_utils;
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
@ -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::*;
|
|
@ -1,11 +1,11 @@
|
|||||||
use std::{borrow::Cow, iter};
|
use std::{borrow::Cow, iter};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{layout_manager::WidgetDirection, App, CellContent, CpuWidgetState},
|
app::{layout_manager::WidgetDirection, App, CpuWidgetState},
|
||||||
canvas::{
|
canvas::{drawing_utils::should_hide_x_label, Painter},
|
||||||
components::{GraphData, TextTable, TimeGraph},
|
components::{
|
||||||
drawing_utils::should_hide_x_label,
|
text_table::{CellContent, TextTable},
|
||||||
Painter,
|
time_graph::{GraphData, TimeGraph},
|
||||||
},
|
},
|
||||||
data_conversion::{ConvertedCpuData, TableData, TableRow},
|
data_conversion::{ConvertedCpuData, TableData, TableRow},
|
||||||
};
|
};
|
||||||
|
@ -2,10 +2,8 @@ use tui::{backend::Backend, layout::Rect, terminal::Frame};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app,
|
app,
|
||||||
canvas::{
|
canvas::Painter,
|
||||||
components::{TextTable, TextTableTitle},
|
components::text_table::{TextTable, TextTableTitle},
|
||||||
Painter,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Painter {
|
impl Painter {
|
||||||
|
@ -2,11 +2,8 @@ use std::borrow::Cow;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
canvas::{
|
canvas::{drawing_utils::should_hide_x_label, Painter},
|
||||||
components::{GraphData, TimeGraph},
|
components::time_graph::{GraphData, TimeGraph},
|
||||||
drawing_utils::should_hide_x_label,
|
|
||||||
Painter,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use tui::{
|
use tui::{
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{App, AxisScaling},
|
app::{App, AxisScaling},
|
||||||
canvas::{
|
canvas::{drawing_utils::should_hide_x_label, Painter},
|
||||||
components::{GraphData, TimeGraph},
|
components::time_graph::{GraphData, Point, TimeGraph},
|
||||||
drawing_utils::should_hide_x_label,
|
|
||||||
Painter, Point,
|
|
||||||
},
|
|
||||||
units::data_units::DataUnit,
|
units::data_units::DataUnit,
|
||||||
utils::gen_util::*,
|
utils::gen_util::*,
|
||||||
};
|
};
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
canvas::{
|
canvas::{drawing_utils::get_search_start_position, Painter},
|
||||||
components::{TextTable, TextTableTitle},
|
components::text_table::{TextTable, TextTableTitle},
|
||||||
drawing_utils::get_search_start_position,
|
|
||||||
Painter,
|
|
||||||
},
|
|
||||||
constants::*,
|
constants::*,
|
||||||
data_conversion::{TableData, TableRow},
|
data_conversion::{TableData, TableRow},
|
||||||
};
|
};
|
||||||
|
@ -2,10 +2,8 @@ use tui::{backend::Backend, layout::Rect, terminal::Frame};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app,
|
app,
|
||||||
canvas::{
|
canvas::Painter,
|
||||||
components::{TextTable, TextTableTitle},
|
components::text_table::{TextTable, TextTableTitle},
|
||||||
Painter,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Painter {
|
impl Painter {
|
||||||
|
5
src/components.rs
Normal file
5
src/components.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod tui_widget;
|
||||||
|
|
||||||
|
pub mod time_graph;
|
||||||
|
|
||||||
|
pub mod text_table;
|
5
src/components/text_table.rs
Normal file
5
src/components/text_table.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod draw;
|
||||||
|
pub use draw::*;
|
||||||
|
|
||||||
|
pub mod state;
|
||||||
|
pub use state::*;
|
@ -15,14 +15,17 @@ use tui::{
|
|||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{
|
app::{self, layout_manager::BottomWidget},
|
||||||
self, layout_manager::BottomWidget, CellContent, SortState, TableComponentColumn,
|
components::text_table::SortOrder,
|
||||||
TableComponentHeader, TableComponentState, WidthBounds,
|
|
||||||
},
|
|
||||||
constants::{SIDE_BORDERS, TABLE_GAP_HEIGHT_LIMIT},
|
constants::{SIDE_BORDERS, TABLE_GAP_HEIGHT_LIMIT},
|
||||||
data_conversion::{TableData, TableRow},
|
data_conversion::{TableData, TableRow},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
CellContent, SortState, TableComponentColumn, TableComponentHeader, TableComponentState,
|
||||||
|
WidthBounds,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct TextTableTitle<'a> {
|
pub struct TextTableTitle<'a> {
|
||||||
pub title: Cow<'a, str>,
|
pub title: Cow<'a, str>,
|
||||||
pub is_expanded: bool,
|
pub is_expanded: bool,
|
||||||
@ -301,8 +304,8 @@ fn build_header<'a, H: TableComponentHeader>(
|
|||||||
let index = s.current_index;
|
let index = s.current_index;
|
||||||
|
|
||||||
let arrow = match order {
|
let arrow = match order {
|
||||||
app::SortOrder::Ascending => UP_ARROW,
|
SortOrder::Ascending => UP_ARROW,
|
||||||
app::SortOrder::Descending => DOWN_ARROW,
|
SortOrder::Descending => DOWN_ARROW,
|
||||||
};
|
};
|
||||||
|
|
||||||
Either::Right(columns.iter().enumerate().filter_map(move |(itx, c)| {
|
Either::Right(columns.iter().enumerate().filter_map(move |(itx, c)| {
|
@ -3,7 +3,7 @@ use std::{borrow::Cow, convert::TryInto, ops::Range};
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use tui::{layout::Rect, widgets::TableState};
|
use tui::{layout::Rect, widgets::TableState};
|
||||||
|
|
||||||
use super::ScrollDirection;
|
use crate::app::ScrollDirection;
|
||||||
|
|
||||||
/// A bound on the width of a column.
|
/// A bound on the width of a column.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
@ -13,7 +13,7 @@ use tui::{
|
|||||||
use concat_string::concat_string;
|
use concat_string::concat_string;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
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.
|
/// A single graph point.
|
||||||
pub type Point = (f64, f64);
|
pub type Point = (f64, f64);
|
||||||
@ -156,7 +156,7 @@ impl<'a> TimeGraph<'a> {
|
|||||||
.legend_style(self.graph_style)
|
.legend_style(self.graph_style)
|
||||||
.hidden_legend_constraints(
|
.hidden_legend_constraints(
|
||||||
self.legend_constraints
|
self.legend_constraints
|
||||||
.unwrap_or(super::DEFAULT_LEGEND_CONSTRAINTS),
|
.unwrap_or(DEFAULT_LEGEND_CONSTRAINTS),
|
||||||
),
|
),
|
||||||
draw_loc,
|
draw_loc,
|
||||||
)
|
)
|
||||||
@ -194,7 +194,7 @@ mod test {
|
|||||||
text::{Span, Spans},
|
text::{Span, Spans},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::canvas::components::Axis;
|
use crate::components::tui_widget::time_chart::Axis;
|
||||||
|
|
||||||
use super::TimeGraph;
|
use super::TimeGraph;
|
||||||
|
|
||||||
@ -251,17 +251,17 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn time_graph_gen_title() {
|
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 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!(
|
assert_eq!(
|
||||||
title,
|
title,
|
||||||
Spans::from(Span::styled(" Network ", Style::default().fg(Color::Cyan)))
|
Spans::from(Span::styled(" Network ", Style::default().fg(Color::Cyan)))
|
||||||
);
|
);
|
||||||
|
|
||||||
tg.is_expanded = true;
|
time_graph.is_expanded = true;
|
||||||
let title = tg.generate_title(draw_loc);
|
let title = time_graph.generate_title(draw_loc);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
title,
|
title,
|
||||||
Spans::from(vec![
|
Spans::from(vec![
|
1
src/components/tui_widget.rs
Normal file
1
src/components/tui_widget.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod time_chart;
|
@ -1,8 +1,8 @@
|
|||||||
//! This mainly concerns converting collected data into things that the canvas
|
//! This mainly concerns converting collected data into things that the canvas
|
||||||
//! can actually handle.
|
//! can actually handle.
|
||||||
|
|
||||||
use crate::app::CellContent;
|
use crate::components::text_table::CellContent;
|
||||||
use crate::canvas::Point;
|
use crate::components::time_graph::Point;
|
||||||
use crate::{app::AxisScaling, units::data_units::DataUnit, Pid};
|
use crate::{app::AxisScaling, units::data_units::DataUnit, Pid};
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{data_farmer, data_harvester, App},
|
app::{data_farmer, data_harvester, App},
|
||||||
|
@ -44,6 +44,7 @@ pub mod utils {
|
|||||||
}
|
}
|
||||||
pub mod canvas;
|
pub mod canvas;
|
||||||
pub mod clap;
|
pub mod clap;
|
||||||
|
pub mod components;
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod data_conversion;
|
pub mod data_conversion;
|
||||||
pub mod options;
|
pub mod options;
|
||||||
|
@ -12,7 +12,7 @@ use std::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{
|
app::{
|
||||||
layout_manager::*,
|
layout_manager::*,
|
||||||
widgets::{ProcWidget, ProcWidgetMode},
|
widgets::{DiskWidgetState, ProcWidget, ProcWidgetMode, TempWidgetState},
|
||||||
*,
|
*,
|
||||||
},
|
},
|
||||||
canvas::ColourScheme,
|
canvas::ColourScheme,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user