diff --git a/src/app.rs b/src/app.rs index e1b4e5fc..7a00e848 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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), }; diff --git a/src/app/data_farmer.rs b/src/app/data_farmer.rs index a89af0a7..f0427c5d 100644 --- a/src/app/data_farmer.rs +++ b/src/app/data_farmer.rs @@ -61,7 +61,6 @@ pub struct ProcessData { impl ProcessData { fn ingest(&mut self, list_of_processes: Vec) { - // 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) { 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); } diff --git a/src/app/states.rs b/src/app/states.rs index dcecc15a..63b9bae8 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -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 } diff --git a/src/app/widgets/cpu_graph.rs b/src/app/widgets/cpu_graph.rs index 4c127bb4..ca520ad5 100644 --- a/src/app/widgets/cpu_graph.rs +++ b/src/app/widgets/cpu_graph.rs @@ -52,7 +52,31 @@ impl ColumnHeader for CpuWidgetColumn { } } -impl DataToCell 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 for CpuWidgetTableData { fn to_cell<'a>(&'a self, column: &CpuWidgetColumn, calculated_width: u16) -> Option> { const CPU_HIDE_BREAKPOINT: u16 = 5; @@ -64,13 +88,12 @@ impl DataToCell 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 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, - pub table: DataTable, + pub table: DataTable, 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(), + ); + } } diff --git a/src/app/widgets/disk_table.rs b/src/app/widgets/disk_table.rs index be444b45..0749a0d6 100644 --- a/src/app/widgets/disk_table.rs +++ b/src/app/widgets/disk_table.rs @@ -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()); + } } diff --git a/src/app/widgets/process_table.rs b/src/app/widgets/process_table.rs index 967af03c..adba1133 100644 --- a/src/app/widgets/process_table.rs +++ b/src/app/widgets/process_table.rs @@ -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, - /// 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(); } } diff --git a/src/app/widgets/temperature_table.rs b/src/app/widgets/temperature_table.rs index ea3a8c32..0be51ffe 100644 --- a/src/app/widgets/temperature_table.rs +++ b/src/app/widgets/temperature_table.rs @@ -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()); + } } diff --git a/src/canvas/widgets.rs b/src/canvas/widgets.rs index b753ef53..6b2fe78f 100644 --- a/src/canvas/widgets.rs +++ b/src/canvas/widgets.rs @@ -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; diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index c70790ee..c61d340a 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -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, ); diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index 27e22422..ac4045a6 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -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, ); diff --git a/src/canvas/widgets/process_table.rs b/src/canvas/widgets/process_table.rs index 96ab2f20..6e29d39c 100644 --- a/src/canvas/widgets/process_table.rs +++ b/src/canvas/widgets/process_table.rs @@ -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, ); diff --git a/src/canvas/widgets/temp_table.rs b/src/canvas/widgets/temperature_table.rs similarity index 94% rename from src/canvas/widgets/temp_table.rs rename to src/canvas/widgets/temperature_table.rs index 5e956bf9..7388a005 100644 --- a/src/canvas/widgets/temp_table.rs +++ b/src/canvas/widgets/temperature_table.rs @@ -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, ); diff --git a/src/components/data_table.rs b/src/components/data_table.rs index 9cbbe65f..650e14a5 100644 --- a/src/components/data_table.rs +++ b/src/components/data_table.rs @@ -74,7 +74,7 @@ impl, H: ColumnHeader, S: SortType, C: DataTableColumn) { + pub fn set_data(&mut self, data: Vec) { self.data = data; let max_pos = self.data.len().saturating_sub(1); if self.state.current_index > max_pos { diff --git a/src/components/data_table/draw.rs b/src/components/data_table/draw.rs index 25f48deb..0313153c 100644 --- a/src/components/data_table/draw.rs +++ b/src/components/data_table/draw.rs @@ -141,11 +141,9 @@ where } pub fn draw( - &mut self, f: &mut Frame<'_, B>, draw_info: &DrawInfo, data: Vec, - 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() diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 237c2369..57246fa9 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -59,22 +59,26 @@ pub struct ConvertedData { pub total_tx_display: String, pub network_data_rx: Vec, pub network_data_tx: Vec, - pub disk_data: Vec, - pub temp_data: Vec, 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, // TODO: Switch this and all data points over to a better data structure... pub swap_data: Vec, + + #[cfg(feature = "zfs")] + pub arc_labels: Option<(String, String)>, #[cfg(feature = "zfs")] pub arc_data: Vec, + + #[cfg(feature = "gpu")] + pub gpu_data: Option>, + pub load_avg_data: [f32; 3], pub cpu_data: Vec, pub battery_data: Vec, - #[cfg(feature = "gpu")] - pub gpu_data: Option>, + pub disk_data: Vec, + pub temp_data: Vec, } impl ConvertedData { diff --git a/src/lib.rs b/src/lib.rs index 9e652533..7bfadfe4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, >, termination_ctrl_lock: Arc>, -) -> 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>, termination_ctrl_cvar: Arc, 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; diff --git a/src/units.rs b/src/units.rs index 937154de..aa458fb8 100644 --- a/src/units.rs +++ b/src/units.rs @@ -1 +1,4 @@ pub mod data_units; + +// FIXME: Maybe move temperature units here? idk. +// FIXME: Maybe combine this with utils?