refactor: clean up some more drawing/component code (#1372)

* rename battery info widget file

* add widget trait

* move basic table arrows over

* some renaming

* more renaming and shuffling

* cleanup

* fmt
This commit is contained in:
Clement Tsang 2024-01-02 06:24:13 +00:00 committed by GitHub
parent c242b4aff3
commit 77777ef588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 50 additions and 36 deletions

View File

@ -1,7 +1,7 @@
pub mod components;
mod dialogs;
mod drawing_utils;
pub mod styling;
pub mod tui_widgets;
mod widgets;
use std::str::FromStr;
@ -49,7 +49,7 @@ impl FromStr for ColourScheme {
"nord" => Ok(ColourScheme::Nord),
"nord-light" => Ok(ColourScheme::NordLight),
_ => Err(BottomError::ConfigError(format!(
"\"{s}\" is an invalid built-in color scheme."
"`{s}` is an invalid built-in color scheme."
))),
}
}
@ -71,8 +71,10 @@ pub struct Painter {
widget_layout: BottomLayout,
}
// Part of a temporary fix for https://github.com/ClementTsang/bottom/issues/896
enum LayoutConstraint {
/// The constraints of a widget relative to its parent.
///
/// This is used over ratatui's internal representation due to https://github.com/ClementTsang/bottom/issues/896.
pub enum LayoutConstraint {
CanvasHandled,
Grow,
Ratio(u32, u32),

8
src/canvas/components.rs Normal file
View File

@ -0,0 +1,8 @@
//! Lower-level components used throughout bottom.
pub mod data_table;
pub mod time_graph;
mod tui;
pub mod widget_carousel;
pub use tui::*;

View File

@ -183,7 +183,7 @@ mod test {
};
use super::TimeGraph;
use crate::canvas::tui_widgets::time_chart::Axis;
use crate::canvas::components::time_chart::Axis;
const Y_LABELS: [Cow<'static, str>; 3] = [
Cow::Borrowed("0%"),

View File

@ -0,0 +1,4 @@
//! Components derived from ratatui widgets.
pub mod pipe_gauge;
pub mod time_chart;

View File

@ -1,6 +0,0 @@
//! Cstom ratatui widgets used by the rest of bottom.
pub mod data_table;
pub mod pipe_gauge;
pub mod time_chart;
pub mod time_graph;

View File

@ -1,4 +1,3 @@
pub mod basic_table_arrows;
pub mod battery_display;
pub mod cpu_basic;
pub mod cpu_graph;

View File

@ -9,7 +9,7 @@ use tui::{
use crate::{
app::App,
canvas::{
tui_widgets::pipe_gauge::{LabelLimit, PipeGauge},
components::pipe_gauge::{LabelLimit, PipeGauge},
Painter,
},
constants::*,

View File

@ -9,11 +9,11 @@ use tui::{
use crate::{
app::{layout_manager::WidgetDirection, App},
canvas::{
drawing_utils::should_hide_x_label,
tui_widgets::{
components::{
data_table::{DrawInfo, SelectionState},
time_graph::{GraphData, TimeGraph},
},
drawing_utils::should_hide_x_label,
Painter,
},
data_conversion::CpuWidgetData,

View File

@ -3,7 +3,7 @@ use tui::{layout::Rect, terminal::Frame};
use crate::{
app,
canvas::{
tui_widgets::data_table::{DrawInfo, SelectionState},
components::data_table::{DrawInfo, SelectionState},
Painter,
},
};

View File

@ -6,7 +6,7 @@ use tui::{
use crate::{
app::App,
canvas::{tui_widgets::pipe_gauge::PipeGauge, Painter},
canvas::{components::pipe_gauge::PipeGauge, Painter},
constants::*,
};

View File

@ -9,8 +9,8 @@ use tui::{
use crate::{
app::App,
canvas::{
components::time_graph::{GraphData, TimeGraph},
drawing_utils::should_hide_x_label,
tui_widgets::time_graph::{GraphData, TimeGraph},
Painter,
},
};

View File

@ -9,11 +9,11 @@ use tui::{
use crate::{
app::{App, AxisScaling},
canvas::{
drawing_utils::should_hide_x_label,
tui_widgets::{
components::{
time_chart::Point,
time_graph::{GraphData, TimeGraph},
},
drawing_utils::should_hide_x_label,
Painter,
},
utils::{data_prefixes::*, data_units::DataUnit, general::partial_ordering},

View File

@ -10,7 +10,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::{
app::{App, AppSearchState},
canvas::{
tui_widgets::data_table::{DrawInfo, SelectionState},
components::data_table::{DrawInfo, SelectionState},
Painter,
},
constants::*,

View File

@ -3,7 +3,7 @@ use tui::{layout::Rect, terminal::Frame};
use crate::{
app,
canvas::{
tui_widgets::data_table::{DrawInfo, SelectionState},
components::data_table::{DrawInfo, SelectionState},
Painter,
},
};

View File

@ -7,7 +7,7 @@ use kstring::KString;
use crate::{
app::{data_farmer::DataCollection, AxisScaling},
canvas::tui_widgets::time_chart::Point,
canvas::components::time_chart::Point,
data_collection::{cpu::CpuDataType, memory::MemHarvest, temperature::TemperatureType},
utils::{data_prefixes::*, data_units::DataUnit, general::*},
widgets::{DiskWidgetData, TempWidgetData},

View File

@ -1,4 +1,4 @@
pub mod battery_widget;
pub mod battery_info;
pub mod cpu_graph;
pub mod disk_table;
pub mod mem_graph;
@ -6,10 +6,17 @@ pub mod net_graph;
pub mod process_table;
pub mod temperature_table;
pub use battery_widget::*;
pub use battery_info::*;
pub use cpu_graph::*;
pub use disk_table::*;
pub use mem_graph::*;
pub use net_graph::*;
pub use process_table::*;
pub use temperature_table::*;
use tui::{layout::Rect, Frame};
/// A [`Widget`] converts raw data into something that a user can see and interact with.
pub trait Widget<Data> {
/// How to actually draw the widget to the terminal.
fn draw(&self, f: &mut Frame<'_>, draw_location: Rect, widget_id: u64);
}

View File

@ -6,11 +6,11 @@ use tui::{style::Style, text::Text, widgets::Row};
use crate::{
app::AppConfigFields,
canvas::{
styling::CanvasStyling,
tui_widgets::data_table::{
components::data_table::{
Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling,
DataToCell,
},
styling::CanvasStyling,
Painter,
},
data_collection::cpu::CpuDataType,

View File

@ -6,11 +6,11 @@ use tui::text::Text;
use crate::{
app::AppConfigFields,
canvas::{
styling::CanvasStyling,
tui_widgets::data_table::{
components::data_table::{
ColumnHeader, DataTableColumn, DataTableProps, DataTableStyling, DataToCell,
SortColumn, SortDataTable, SortDataTableProps, SortOrder, SortsRow,
},
styling::CanvasStyling,
},
utils::general::{get_decimal_bytes, sort_partial_fn, truncate_to_text},
};

View File

@ -19,11 +19,11 @@ use crate::{
AppConfigFields, AppSearchState,
},
canvas::{
styling::CanvasStyling,
tui_widgets::data_table::{
components::data_table::{
Column, ColumnHeader, ColumnWidthBounds, DataTable, DataTableColumn, DataTableProps,
DataTableStyling, SortColumn, SortDataTable, SortDataTableProps, SortOrder, SortsRow,
},
styling::CanvasStyling,
},
data_collection::processes::ProcessHarvest,
Pid,

View File

@ -4,7 +4,7 @@ use serde::{de::Error, Deserialize, Serialize};
use super::ProcWidgetData;
use crate::{
canvas::tui_widgets::data_table::{ColumnHeader, SortsRow},
canvas::components::data_table::{ColumnHeader, SortsRow},
utils::general::sort_partial_fn,
};

View File

@ -10,7 +10,7 @@ use tui::{text::Text, widgets::Row};
use super::proc_widget_column::ProcColumn;
use crate::{
canvas::{
tui_widgets::data_table::{DataTableColumn, DataToCell},
components::data_table::{DataTableColumn, DataToCell},
Painter,
},
data_collection::processes::ProcessHarvest,

View File

@ -3,7 +3,7 @@ use std::borrow::Cow;
use tui::text::Text;
use crate::{
canvas::tui_widgets::data_table::{ColumnHeader, DataTableColumn, DataToCell},
canvas::components::data_table::{ColumnHeader, DataTableColumn, DataToCell},
utils::general::truncate_to_text,
};

View File

@ -7,11 +7,11 @@ use tui::text::Text;
use crate::{
app::AppConfigFields,
canvas::{
styling::CanvasStyling,
tui_widgets::data_table::{
components::data_table::{
ColumnHeader, DataTableColumn, DataTableProps, DataTableStyling, DataToCell,
SortColumn, SortDataTable, SortDataTableProps, SortOrder, SortsRow,
},
styling::CanvasStyling,
},
data_collection::temperature::TemperatureType,
utils::general::{sort_partial_fn, truncate_to_text},