diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2022-04-30 20:19:51 -0400 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2022-05-07 20:53:52 -0400 |
commit | 64ed45083ea763690c618ce9fabd22d5224d7625 (patch) | |
tree | 39a04aace1199d21944c9abbeeeb443785a88e21 /src | |
parent | 2a65bc95fe7a20e81725e6f559aee015fba852cc (diff) |
refactor: remove unneeded freeze param
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 8 | ||||
-rw-r--r-- | src/app/data_farmer.rs | 6 | ||||
-rw-r--r-- | src/bin/main.rs | 6 | ||||
-rw-r--r-- | src/canvas/components/text_table.rs | 24 | ||||
-rw-r--r-- | src/data_conversion.rs | 129 | ||||
-rw-r--r-- | src/lib.rs | 11 |
6 files changed, 100 insertions, 84 deletions
@@ -147,8 +147,8 @@ pub struct App { pub current_widget: BottomWidget, pub used_widgets: UsedWidgets, pub filters: DataFilters, - pub config: Config, - pub config_path: Option<PathBuf>, + pub config: Config, // TODO: Is this even used...? + pub config_path: Option<PathBuf>, // TODO: Is this even used...? } #[cfg(target_os = "windows")] @@ -1528,7 +1528,9 @@ impl App { 'f' => { self.is_frozen = !self.is_frozen; if self.is_frozen { - self.data_collection.set_frozen_time(); + self.data_collection.freeze(); + } else { + self.data_collection.thaw(); } } 'C' => { diff --git a/src/app/data_farmer.rs b/src/app/data_farmer.rs index 1860c91b..64e08602 100644 --- a/src/app/data_farmer.rs +++ b/src/app/data_farmer.rs @@ -108,10 +108,14 @@ impl DataCollection { } } - pub fn set_frozen_time(&mut self) { + pub fn freeze(&mut self) { self.frozen_instant = Some(self.current_instant); } + pub fn thaw(&mut self) { + self.frozen_instant = None; + } + pub fn clean_data(&mut self, max_time_millis: u64) { let current_time = Instant::now(); diff --git a/src/bin/main.rs b/src/bin/main.rs index 6a1f0aae..627e1e2a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -158,7 +158,6 @@ fn main() -> Result<()> { if app.used_widgets.use_net { let network_data = convert_network_data_points( &app.data_collection, - false, app.app_config_fields.use_basic_mode || app.app_config_fields.use_old_network_legend, &app.app_config_fields.network_scale_type, @@ -190,9 +189,9 @@ fn main() -> Result<()> { // Memory if app.used_widgets.use_mem { app.canvas_data.mem_data = - convert_mem_data_points(&app.data_collection, false); + convert_mem_data_points(&app.data_collection); app.canvas_data.swap_data = - convert_swap_data_points(&app.data_collection, false); + convert_swap_data_points(&app.data_collection); let (memory_labels, swap_labels) = convert_mem_labels(&app.data_collection); @@ -206,7 +205,6 @@ fn main() -> Result<()> { convert_cpu_data_points( &app.data_collection, &mut app.canvas_data.cpu_data, - false, ); app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest; } diff --git a/src/canvas/components/text_table.rs b/src/canvas/components/text_table.rs index 16e049ed..c1119c43 100644 --- a/src/canvas/components/text_table.rs +++ b/src/canvas/components/text_table.rs @@ -14,7 +14,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ app::{self, TableComponentState}, constants::{SIDE_BORDERS, TABLE_GAP_HEIGHT_LIMIT}, - data_conversion::TableData, + data_conversion::{CellContent, TableData}, }; pub struct TextTable<'a> { @@ -204,14 +204,24 @@ impl<'a> TextTable<'a> { } /// Truncates text if it is too long, and adds an ellipsis at the end if needed. -fn truncate_text(text: &str, width: usize) -> Text<'_> { - let graphemes = UnicodeSegmentation::graphemes(text, true).collect::<Vec<&str>>(); +fn truncate_text(content: &CellContent, width: usize) -> Text<'_> { + let (text, opt) = match content { + CellContent::Simple(s) => (s, None), + CellContent::HasShort { short, long } => (long, Some(short)), + }; + + let graphemes = UnicodeSegmentation::graphemes(text.as_ref(), true).collect::<Vec<&str>>(); if graphemes.len() > width && width > 0 { - // Truncate with ellipsis - let first_n = graphemes[..(width - 1)].concat(); - return Text::raw(concat_string!(first_n, "…")); + if let Some(s) = opt { + // If an alternative exists, use that. + Text::raw(s.as_ref()) + } else { + // Truncate with ellipsis + let first_n = graphemes[..(width - 1)].concat(); + Text::raw(concat_string!(first_n, "…")) + } } else { - Text::raw(text) + Text::raw(text.as_ref()) } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 93470fc5..4b2dc7df 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -1,18 +1,18 @@ //! This mainly concerns converting collected data into things that the canvas //! can actually handle. +use crate::canvas::Point; use crate::{app::AxisScaling, units::data_units::DataUnit, Pid}; use crate::{ app::{data_farmer, data_harvester, App, ProcWidgetState}, utils::{self, gen_util::*}, }; +use concat_string::concat_string; use data_harvester::processes::ProcessSorting; use fxhash::FxBuildHasher; use indexmap::IndexSet; +use std::borrow::Cow; use std::collections::{HashMap, VecDeque}; -/// Point is of time, data -type Point = (f64, f64); - #[derive(Default, Debug)] pub struct ConvertedBatteryData { pub battery_name: String, @@ -23,9 +23,26 @@ pub struct ConvertedBatteryData { pub health: String, } +pub enum CellContent { + Simple(Cow<'static, str>), + HasShort { + short: Cow<'static, str>, + long: Cow<'static, str>, + }, +} + +impl CellContent { + pub fn len(&self) -> usize { + match self { + CellContent::Simple(s) => s.len(), + CellContent::HasShort { short: _, long } => long.len(), + } + } +} + #[derive(Default)] pub struct TableData { - pub data: Vec<Vec<String>>, + pub data: Vec<Vec<CellContent>>, pub row_widths: Vec<usize>, } @@ -94,18 +111,23 @@ pub fn convert_temp_row(app: &App) -> TableData { let temp_type = &app.app_config_fields.temperature_type; let mut row_widths = vec![0; 2]; - let mut sensor_vector: Vec<Vec<String>> = current_data + let mut sensor_vector: Vec<Vec<CellContent>> = current_data .temp_harvest .iter() .map(|temp_harvest| { let row = vec![ - temp_harvest.name.clone(), - (temp_harvest.temperature.ceil() as u64).to_string() - + match temp_type { - data_harvester::temperature::TemperatureType::Celsius => "°C", - data_harvester::temperature::TemperatureType::Kelvin => "K", - data_harvester::temperature::TemperatureType::Fahrenheit => "°F", - }, + CellContent::Simple(temp_harvest.name.clone().into()), + CellContent::Simple( + concat_string!( + (temp_harvest.temperature.ceil() as u64).to_string(), + match temp_type { + data_harvester::temperature::TemperatureType::Celsius => "°C", + data_harvester::temperature::TemperatureType::Kelvin => "K", + data_harvester::temperature::TemperatureType::Fahrenheit => "°F", + } + ) + .into(), + ), ]; row_widths.iter_mut().zip(&row).for_each(|(curr, r)| { @@ -117,7 +139,10 @@ pub fn convert_temp_row(app: &App) -> TableData { .collect(); if sensor_vector.is_empty() { - sensor_vector.push(vec!["No Sensors Found".to_string(), "".to_string()]); + sensor_vector.push(vec![ + CellContent::Simple("No Sensors Found".into()), + CellContent::Simple("".into()), + ]); } TableData { @@ -127,7 +152,7 @@ pub fn convert_temp_row(app: &App) -> TableData { } pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> TableData { - let mut disk_vector: Vec<Vec<String>> = Vec::new(); + let mut disk_vector: Vec<Vec<CellContent>> = Vec::new(); let mut row_widths = vec![0; 8]; current_data @@ -137,9 +162,9 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> TableData .for_each(|(disk, (io_read, io_write))| { let free_space_fmt = if let Some(free_space) = disk.free_space { let converted_free_space = get_decimal_bytes(free_space); - format!("{:.*}{}", 0, converted_free_space.0, converted_free_space.1) + format!("{:.*}{}", 0, converted_free_space.0, converted_free_space.1).into() } else { - "N/A".to_string() + "N/A".into() }; let total_space_fmt = if let Some(total_space) = disk.total_space { let converted_total_space = get_decimal_bytes(total_space); @@ -147,26 +172,27 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> TableData "{:.*}{}", 0, converted_total_space.0, converted_total_space.1 ) + .into() } else { - "N/A".to_string() + "N/A".into() }; let usage_fmt = if let (Some(used_space), Some(total_space)) = (disk.used_space, disk.total_space) { - format!("{:.0}%", used_space as f64 / total_space as f64 * 100_f64) + format!("{:.0}%", used_space as f64 / total_space as f64 * 100_f64).into() } else { - "N/A".to_string() + "N/A".into() }; let row = vec![ - disk.name.to_string(), - disk.mount_point.to_string(), - usage_fmt, - free_space_fmt, - total_space_fmt, - io_read.to_string(), - io_write.to_string(), + CellContent::Simple(disk.name.clone().into()), + CellContent::Simple(disk.mount_point.clone().into()), + CellContent::Simple(usage_fmt), + CellContent::Simple(free_space_fmt), + CellContent::Simple(total_space_fmt), + CellContent::Simple(io_read.clone().into()), + CellContent::Simple(io_write.clone().into()), ]; row_widths.iter_mut().zip(&row).for_each(|(curr, r)| { *curr = std::cmp::max(*curr, r.len()); @@ -175,7 +201,10 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> TableData }); if disk_vector.is_empty() { - disk_vector.push(vec!["No Disks Found".to_string(), "".to_string()]); + disk_vector.push(vec![ + CellContent::Simple("No Disks Found".into()), + CellContent::Simple("".into()), + ]); } TableData { @@ -186,14 +215,9 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> TableData pub fn convert_cpu_data_points( current_data: &data_farmer::DataCollection, existing_cpu_data: &mut Vec<ConvertedCpuData>, - is_frozen: bool, ) { - let current_time = if is_frozen { - if let Some(frozen_instant) = current_data.frozen_instant { - frozen_instant - } else { - current_data.current_instant - } + let current_time = if let Some(frozen_instant) = current_data.frozen_instant { + frozen_instant } else { current_data.current_instant }; @@ -264,16 +288,10 @@ pub fn convert_cpu_data_points( } } -pub fn convert_mem_data_points( - current_data: &data_farmer::DataCollection, is_frozen: bool, -) -> Vec<Point> { +pub fn convert_mem_data_points(current_data: &data_farmer::DataCollection) -> Vec<Point> { let mut result: Vec<Point> = Vec::new(); - let current_time = if is_frozen { - if let Some(frozen_instant) = current_data.frozen_instant { - frozen_instant - } else { - current_data.current_instant - } + let current_time = if let Some(frozen_instant) = current_data.frozen_instant { + frozen_instant } else { current_data.current_instant }; @@ -292,16 +310,10 @@ pub fn convert_mem_data_points( result } -pub fn convert_swap_data_points( - current_data: &data_farmer::DataCollection, is_frozen: bool, -) -> Vec<Point> { +pub fn convert_swap_data_points(current_data: &data_farmer::DataCollection) -> Vec<Point> { let mut result: Vec<Point> = Vec::new(); - let current_time = if is_frozen { - if let Some(frozen_instant) = current_data.frozen_instant { - frozen_instant - } else { - current_data.current_instant - } + let current_time = if let Some(frozen_instant) = current_data.frozen_instant { + frozen_instant } else { current_data.current_instant }; @@ -391,18 +403,14 @@ pub fn convert_mem_labels( } pub fn get_rx_tx_data_points( - current_data: &data_farmer::DataCollection, is_frozen: bool, network_scale_type: &AxisScaling, + current_data: &data_farmer::DataCollection, network_scale_type: &AxisScaling, network_unit_type: &DataUnit, network_use_binary_prefix: bool, ) -> (Vec<Point>, Vec<Point>) { let mut rx: Vec<Point> = Vec::new(); let mut tx: Vec<Point> = Vec::new(); - let current_time = if is_frozen { - if let Some(frozen_instant) = current_data.frozen_instant { - frozen_instant - } else { - current_data.current_instant - } + let current_time = if let Some(frozen_instant) = current_data.frozen_instant { + frozen_instant } else { current_data.current_instant }; @@ -446,13 +454,12 @@ pub fn get_rx_tx_data_points( } pub fn convert_network_data_points( - current_data: &data_farmer::DataCollection, is_frozen: bool, need_four_points: bool, + current_data: &data_farmer::DataCollection, need_four_points: bool, network_scale_type: &AxisScaling, network_unit_type: &DataUnit, network_use_binary_prefix: bool, ) -> ConvertedNetworkData { let (rx, tx) = get_rx_tx_data_points( current_data, - is_frozen, network_scale_type, network_unit_type, network_use_binary_prefix, @@ -314,26 +314,21 @@ pub fn handle_force_redraws(app: &mut App) { } if app.cpu_state.force_update.is_some() { - convert_cpu_data_points( - &app.data_collection, - &mut app.canvas_data.cpu_data, - app.is_frozen, - ); + convert_cpu_data_points(&app.data_collection, &mut app.canvas_data.cpu_data); app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest; app.cpu_state.force_update = None; } // FIXME: [OPT] Prefer reassignment over new vectors? if app.mem_state.force_update.is_some() { - app.canvas_data.mem_data = convert_mem_data_points(&app.data_collection, app.is_frozen); - app.canvas_data.swap_data = convert_swap_data_points(&app.data_collection, app.is_frozen); + app.canvas_data.mem_data = convert_mem_data_points(&app.data_collection); + app.canvas_data.swap_data = convert_swap_data_points(&app.data_collection); app.mem_state.force_update = None; } if app.net_state.force_update.is_some() { let (rx, tx) = get_rx_tx_data_points( &app.data_collection, - app.is_frozen, &app.app_config_fields.network_scale_type, &app.app_config_fields.network_unit_type, app.app_config_fields.network_use_binary_prefix, |