refactor: move data passing for table to another step (#863)

* refactor: remove redundant scroll direction enum

This was made redundant from the table refactor.

* add some todos/docs

* refactor: temp hack to pass in data on process
This commit is contained in:
Clement Tsang 2022-11-02 00:36:55 -04:00 committed by GitHub
parent 3296feae50
commit 064d740c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 104 additions and 59 deletions

View File

@ -726,7 +726,7 @@ impl App {
KillSignal::Kill(prev_signal) => {
self.delete_dialog_state.selected_signal = match prev_signal - 1 {
0 => KillSignal::Cancel,
// 32+33 are skipped
// 32 + 33 are skipped
33 => KillSignal::Kill(31),
signal => KillSignal::Kill(signal),
};

View File

@ -61,7 +61,6 @@ pub struct ProcessData {
impl ProcessData {
fn ingest(&mut self, list_of_processes: Vec<ProcessHarvest>) {
// TODO: [Optimization] Probably more efficient to all of this in the data collection step, but it's fine for now.
self.process_parent_mapping.clear();
// Reverse as otherwise the pid mappings are in the wrong order.
@ -212,8 +211,6 @@ impl DataCollection {
pub fn eat_data(&mut self, harvested_data: Box<Data>) {
let harvested_time = harvested_data.last_collection_time;
// trace!("Harvested time: {:?}", harvested_time);
// trace!("New current instant: {:?}", self.current_instant);
let mut new_entry = TimedData::default();
// Network
@ -245,7 +242,7 @@ impl DataCollection {
self.eat_cpu(cpu, &mut new_entry);
}
// Load Average
// Load average
if let Some(load_avg) = harvested_data.load_avg {
self.eat_load_avg(load_avg, &mut new_entry);
}

View File

@ -12,20 +12,6 @@ use super::widgets::{
ProcWidget, TempWidgetState,
};
#[derive(Debug)]
pub enum ScrollDirection {
// UP means scrolling up --- this usually DECREMENTS
Up,
// DOWN means scrolling down --- this usually INCREMENTS
Down,
}
impl Default for ScrollDirection {
fn default() -> Self {
ScrollDirection::Down
}
}
#[derive(Debug)]
pub enum CursorDirection {
Left,
@ -112,7 +98,7 @@ impl Default for AppSearchState {
}
impl AppSearchState {
/// Returns a reset but still enabled app search state
/// Resets the [`AppSearchState`] to its default state, albeit still enabled.
pub fn reset(&mut self) {
*self = AppSearchState {
is_enabled: self.is_enabled,
@ -120,6 +106,7 @@ impl AppSearchState {
}
}
/// Returns whether the [`AppSearchState`] has an invalid or blank search.
pub fn is_invalid_or_blank_search(&self) -> bool {
self.is_blank_search || self.is_invalid_search
}

View File

@ -52,7 +52,31 @@ impl ColumnHeader for CpuWidgetColumn {
}
}
impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
pub enum CpuWidgetTableData {
All,
Entry {
data_type: CpuDataType,
last_entry: f64,
},
}
impl CpuWidgetTableData {
pub fn from_cpu_widget_data(data: &CpuWidgetData) -> CpuWidgetTableData {
match data {
CpuWidgetData::All => CpuWidgetTableData::All,
CpuWidgetData::Entry {
data_type,
data: _,
last_entry,
} => CpuWidgetTableData::Entry {
data_type: *data_type,
last_entry: *last_entry,
},
}
}
}
impl DataToCell<CpuWidgetColumn> for CpuWidgetTableData {
fn to_cell<'a>(&'a self, column: &CpuWidgetColumn, calculated_width: u16) -> Option<Text<'a>> {
const CPU_HIDE_BREAKPOINT: u16 = 5;
@ -64,13 +88,12 @@ impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
// This is the same for the use percentages - we just *always* show them, and *always* hide the CPU column if
// it is too small.
match &self {
CpuWidgetData::All => match column {
CpuWidgetTableData::All => match column {
CpuWidgetColumn::CPU => Some(truncate_text("All", calculated_width)),
CpuWidgetColumn::Use => None,
},
CpuWidgetData::Entry {
CpuWidgetTableData::Entry {
data_type,
data: _,
last_entry,
} => match column {
CpuWidgetColumn::CPU => {
@ -106,10 +129,9 @@ impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
#[inline(always)]
fn style_row<'a>(&self, row: Row<'a>, painter: &Painter) -> Row<'a> {
let style = match self {
CpuWidgetData::All => painter.colours.all_colour_style,
CpuWidgetData::Entry {
CpuWidgetTableData::All => painter.colours.all_colour_style,
CpuWidgetTableData::Entry {
data_type,
data: _,
last_entry: _,
} => match data_type {
CpuDataType::Avg => painter.colours.avg_colour_style,
@ -138,7 +160,7 @@ pub struct CpuWidgetState {
pub is_legend_hidden: bool,
pub show_avg: bool,
pub autohide_timer: Option<Instant>,
pub table: DataTable<CpuWidgetData, CpuWidgetColumn>,
pub table: DataTable<CpuWidgetTableData, CpuWidgetColumn>,
pub styling: CpuWidgetStyling,
}
@ -172,4 +194,12 @@ impl CpuWidgetState {
styling: CpuWidgetStyling::from_colours(colours),
}
}
pub fn ingest_data(&mut self, data: &[CpuWidgetData]) {
self.table.set_data(
data.iter()
.map(CpuWidgetTableData::from_cpu_widget_data)
.collect(),
);
}
}

View File

@ -144,4 +144,8 @@ impl DiskTableWidget {
table: DataTable::new(COLUMNS, props, styling),
}
}
pub fn ingest_data(&mut self, data: &[DiskWidgetData]) {
self.table.set_data(data.to_vec());
}
}

View File

@ -80,9 +80,6 @@ pub struct ProcWidget {
/// The state of the main table.
pub table: ProcessTable,
/// The stored process data for this specific table.
pub table_data: Vec<ProcWidgetData>,
/// The state of the togglable table that controls sorting.
pub sort_table: SortTable,
@ -224,17 +221,19 @@ impl ProcWidget {
let id_pid_map = FxHashMap::default();
ProcWidget {
let mut table = ProcWidget {
proc_search: process_search_state,
table,
table_data: vec![],
sort_table,
id_pid_map,
is_sort_open: false,
mode,
force_rerender: true,
force_update_data: false,
}
};
table.sort_table.set_data(table.column_text());
table
}
pub fn is_using_command(&self) -> bool {
@ -264,7 +263,7 @@ impl ProcWidget {
/// This function *only* updates the displayed process data. If there is a need to update the actual *stored* data,
/// call it before this function.
pub fn update_displayed_process_data(&mut self, data_collection: &DataCollection) {
self.table_data = match &self.mode {
let data = match &self.mode {
ProcWidgetMode::Grouped | ProcWidgetMode::Normal => {
self.get_normal_data(&data_collection.process_data.process_harvest)
}
@ -272,6 +271,7 @@ impl ProcWidget {
self.get_tree_data(collapsed_pids, data_collection)
}
};
self.table.set_data(data);
}
fn get_tree_data(
@ -587,6 +587,7 @@ impl ProcWidget {
_ => unreachable!(),
}
self.sort_table.set_data(self.column_text());
self.force_data_update();
}
}
@ -664,6 +665,7 @@ impl ProcWidget {
}
_ => unreachable!(),
}
self.sort_table.set_data(self.column_text());
self.force_rerender_and_update();
}
}
@ -702,6 +704,7 @@ impl ProcWidget {
_ => unreachable!(),
}
self.sort_table.set_data(self.column_text());
self.force_rerender_and_update();
}
}

View File

@ -98,4 +98,8 @@ impl TempWidgetState {
table: DataTable::new(COLUMNS, props, styling),
}
}
pub fn ingest_data(&mut self, data: &[TempWidgetData]) {
self.table.set_data(data.to_vec());
}
}

View File

@ -8,4 +8,4 @@ pub mod mem_graph;
pub mod network_basic;
pub mod network_graph;
pub mod process_table;
pub mod temp_table;
pub mod temperature_table;

View File

@ -247,7 +247,6 @@ impl Painter {
cpu_widget_state.table.draw(
f,
&draw_info,
app_state.converted_data.cpu_data.clone(),
app_state.widget_map.get_mut(&widget_id),
self,
);

View File

@ -24,7 +24,6 @@ impl Painter {
disk_widget_state.table.draw(
f,
&draw_info,
app_state.converted_data.disk_data.clone(),
app_state.widget_map.get_mut(&widget_id),
self,
);

View File

@ -91,7 +91,6 @@ impl Painter {
proc_widget_state.table.draw(
f,
&draw_info,
proc_widget_state.table_data.clone(),
app_state.widget_map.get_mut(&widget_id),
self,
);
@ -324,12 +323,9 @@ impl Painter {
selection_state: SelectionState::new(app_state.is_expanded, is_on_widget),
};
let data = pws.column_text();
pws.sort_table.draw(
f,
&draw_info,
data,
app_state.widget_map.get_mut(&widget_id),
self,
);

View File

@ -24,7 +24,6 @@ impl Painter {
temp_widget_state.table.draw(
f,
&draw_info,
app_state.converted_data.temp_data.clone(),
app_state.widget_map.get_mut(&widget_id),
self,
);

View File

@ -74,7 +74,7 @@ impl<DataType: DataToCell<H>, H: ColumnHeader, S: SortType, C: DataTableColumn<H
}
/// Updates the scroll position to be valid for the number of entries.
fn set_data(&mut self, data: Vec<DataType>) {
pub fn set_data(&mut self, data: Vec<DataType>) {
self.data = data;
let max_pos = self.data.len().saturating_sub(1);
if self.state.current_index > max_pos {

View File

@ -141,11 +141,9 @@ where
}
pub fn draw<B: Backend>(
&mut self, f: &mut Frame<'_, B>, draw_info: &DrawInfo, data: Vec<DataType>,
widget: Option<&mut BottomWidget>, painter: &Painter,
&mut self, f: &mut Frame<'_, B>, draw_info: &DrawInfo, widget: Option<&mut BottomWidget>,
painter: &Painter,
) {
self.set_data(data);
let draw_horizontal = !self.props.is_basic || draw_info.is_on_widget();
let draw_loc = draw_info.loc;
let margined_draw_loc = Layout::default()

View File

@ -59,22 +59,26 @@ pub struct ConvertedData {
pub total_tx_display: String,
pub network_data_rx: Vec<Point>,
pub network_data_tx: Vec<Point>,
pub disk_data: Vec<DiskWidgetData>,
pub temp_data: Vec<TempWidgetData>,
pub mem_labels: Option<(String, String)>,
pub swap_labels: Option<(String, String)>,
#[cfg(feature = "zfs")]
pub arc_labels: Option<(String, String)>,
pub mem_data: Vec<Point>, // TODO: Switch this and all data points over to a better data structure...
pub swap_data: Vec<Point>,
#[cfg(feature = "zfs")]
pub arc_labels: Option<(String, String)>,
#[cfg(feature = "zfs")]
pub arc_data: Vec<Point>,
#[cfg(feature = "gpu")]
pub gpu_data: Option<Vec<ConvertedGpuData>>,
pub load_avg_data: [f32; 3],
pub cpu_data: Vec<CpuWidgetData>,
pub battery_data: Vec<ConvertedBatteryData>,
#[cfg(feature = "gpu")]
pub gpu_data: Option<Vec<ConvertedGpuData>>,
pub disk_data: Vec<DiskWidgetData>,
pub temp_data: Vec<TempWidgetData>,
}
impl ConvertedData {

View File

@ -22,7 +22,7 @@ use std::{
sync::Arc,
sync::Condvar,
sync::Mutex,
thread,
thread::{self, JoinHandle},
time::{Duration, Instant},
};
@ -35,6 +35,7 @@ use crossterm::{
use app::{
data_harvester,
frozen_state::FrozenState,
layout_manager::{UsedWidgets, WidgetDirection},
App,
};
@ -323,8 +324,8 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
pub fn update_data(app: &mut App) {
let data_source = match &app.frozen_state {
app::frozen_state::FrozenState::NotFrozen => &app.data_collection,
app::frozen_state::FrozenState::Frozen(data) => data,
FrozenState::NotFrozen => &app.data_collection,
FrozenState::Frozen(data) => data,
};
for proc in app.proc_state.widget_states.values_mut() {
@ -338,9 +339,30 @@ pub fn update_data(app: &mut App) {
if app.cpu_state.force_update.is_some() {
app.converted_data.ingest_cpu_data(data_source);
app.converted_data.load_avg_data = data_source.load_avg_harvest;
app.cpu_state.force_update = None;
}
// FIXME: This is a bit of a temp hack to move data over.
{
let data = &app.converted_data.cpu_data;
for cpu in app.cpu_state.widget_states.values_mut() {
cpu.ingest_data(data)
}
}
{
let data = &app.converted_data.temp_data;
for temp in app.temp_state.widget_states.values_mut() {
temp.ingest_data(data);
}
}
{
let data = &app.converted_data.disk_data;
for disk in app.disk_state.widget_states.values_mut() {
disk.ingest_data(data);
}
}
// TODO: [OPT] Prefer reassignment over new vectors?
if app.mem_state.force_update.is_some() {
app.converted_data.mem_data = convert_mem_data_points(data_source);
@ -375,7 +397,7 @@ pub fn create_input_thread(
BottomEvent<crossterm::event::KeyEvent, crossterm::event::MouseEvent>,
>,
termination_ctrl_lock: Arc<Mutex<bool>>,
) -> std::thread::JoinHandle<()> {
) -> JoinHandle<()> {
thread::spawn(move || {
let mut mouse_timer = Instant::now();
let mut keyboard_timer = Instant::now();
@ -421,7 +443,7 @@ pub fn create_collection_thread(
termination_ctrl_lock: Arc<Mutex<bool>>, termination_ctrl_cvar: Arc<Condvar>,
app_config_fields: &app::AppConfigFields, filters: app::DataFilters,
used_widget_set: UsedWidgets,
) -> std::thread::JoinHandle<()> {
) -> JoinHandle<()> {
let temp_type = app_config_fields.temperature_type;
let use_current_cpu_total = app_config_fields.use_current_cpu_total;
let show_average_cpu = app_config_fields.show_average_cpu;

View File

@ -1 +1,4 @@
pub mod data_units;
// FIXME: Maybe move temperature units here? idk.
// FIXME: Maybe combine this with utils?