From 86eb14f620958e1e87ad3b7931a0de42d07a4ff7 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 11 Sep 2019 22:30:57 -0400 Subject: [PATCH] Added temp setting to app, need to add to data collection. Also refactoring again. --- src/{app/mod.rs => app.rs} | 16 ++++++++++++++++ .../mod.rs => data_collection.rs} | 0 src/{canvas/mod.rs => canvas.rs} | 19 +++++-------------- src/main.rs | 15 ++++++++++++--- 4 files changed, 33 insertions(+), 17 deletions(-) rename src/{app/mod.rs => app.rs} (94%) rename src/app/{data_collection/mod.rs => data_collection.rs} (100%) rename src/{canvas/mod.rs => canvas.rs} (90%) diff --git a/src/app/mod.rs b/src/app.rs similarity index 94% rename from src/app/mod.rs rename to src/app.rs index bf2d3afd..8b09d343 100644 --- a/src/app/mod.rs +++ b/src/app.rs @@ -4,6 +4,14 @@ use data_collection::{cpu, disks, mem, network, processes, temperature}; use std::collections::HashMap; use sysinfo::{System, SystemExt}; +#[allow(dead_code)] +#[derive(Clone)] +pub enum TemperatureType { + Celsius, + Kelvin, + Fahrenheit, +} + #[allow(dead_code)] pub struct App<'a> { title : &'a str, @@ -12,6 +20,7 @@ pub struct App<'a> { pub process_sorting_reverse : bool, pub to_be_resorted : bool, pub current_selected_process_position : u64, + pub temperature_type : TemperatureType, } fn set_if_valid(result : &Result, value_to_set : &mut T) { @@ -46,6 +55,7 @@ pub struct DataState { prev_pid_stats : HashMap, // TODO: Purge list? prev_idle : f64, prev_non_idle : f64, + temperature_type : TemperatureType, } impl Default for DataState { @@ -57,6 +67,7 @@ impl Default for DataState { prev_pid_stats : HashMap::new(), prev_idle : 0_f64, prev_non_idle : 0_f64, + temperature_type : TemperatureType::Celsius, } } } @@ -66,6 +77,10 @@ impl DataState { self.stale_max_seconds = stale_max_seconds; } + pub fn set_temperature_type(&mut self, temperature_type : TemperatureType) { + self.temperature_type = temperature_type; + } + pub fn init(&mut self) { self.sys.refresh_system(); self.sys.refresh_network(); @@ -157,6 +172,7 @@ impl<'a> App<'a> { process_sorting_reverse : true, to_be_resorted : false, current_selected_process_position : 0, + temperature_type : TemperatureType::Celsius, } } diff --git a/src/app/data_collection/mod.rs b/src/app/data_collection.rs similarity index 100% rename from src/app/data_collection/mod.rs rename to src/app/data_collection.rs diff --git a/src/canvas/mod.rs b/src/canvas.rs similarity index 90% rename from src/canvas/mod.rs rename to src/canvas.rs index 9469cf62..98744b25 100644 --- a/src/canvas/mod.rs +++ b/src/canvas.rs @@ -6,7 +6,8 @@ use tui::{ Terminal, }; -const COLOUR_LIST : [Color; 6] = [Color::LightCyan, Color::LightMagenta, Color::LightRed, Color::LightGreen, Color::LightYellow, Color::LightBlue]; +const COLOUR_LIST : [Color; 6] = [Color::LightRed, Color::LightGreen, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta]; +const TEXT_COLOUR : Color = Color::Green; #[derive(Default)] pub struct CanvasData { @@ -20,21 +21,11 @@ pub struct CanvasData { // TODO: Change the error pub fn draw_data(terminal : &mut Terminal, canvas_data : &CanvasData) -> Result<(), io::Error> { - let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| { - Row::StyledData( - sensor.iter(), // TODO: Change this based on temperature type - Style::default().fg(Color::White), - ) - }); + let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR))); - let disk_rows = canvas_data.disk_data.iter().map(|disk| { - Row::StyledData( - disk.iter(), // TODO: Change this based on temperature type - Style::default().fg(Color::White), - ) - }); + let disk_rows = canvas_data.disk_data.iter().map(|disk| Row::StyledData(disk.iter(), Style::default().fg(TEXT_COLOUR))); - let process_rows = canvas_data.process_data.iter().map(|process| Row::StyledData(process.iter(), Style::default().fg(Color::White))); + let process_rows = canvas_data.process_data.iter().map(|process| Row::StyledData(process.iter(), Style::default().fg(TEXT_COLOUR))); // TODO: Convert this into a separate func! terminal.draw(|mut f| { diff --git a/src/main.rs b/src/main.rs index 84c380f5..b3d79851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,6 +53,7 @@ async fn main() -> Result<(), io::Error> { let mut data_state = app::DataState::default(); data_state.init(); data_state.set_stale_max_seconds(STALE_MAX_SECONDS); + data_state.set_temperature_type(app.temperature_type.clone()); { let tx = tx.clone(); thread::spawn(move || { @@ -101,7 +102,7 @@ async fn main() -> Result<(), io::Error> { // Convert all data into tui components canvas_data.disk_data = update_disk_row(&app_data); - canvas_data.temp_sensor_data = update_temp_row(&app_data); + canvas_data.temp_sensor_data = update_temp_row(&app_data, &app.temperature_type); canvas_data.process_data = update_process_row(&app_data); canvas_data.mem_data = update_mem_data_points(&app_data); canvas_data.swap_data = update_swap_data_points(&app_data); @@ -121,11 +122,19 @@ async fn main() -> Result<(), io::Error> { Ok(()) } -fn update_temp_row(app_data : &app::Data) -> Vec> { +fn update_temp_row(app_data : &app::Data, temp_type : &app::TemperatureType) -> Vec> { let mut sensor_vector : Vec> = Vec::new(); for sensor in &app_data.list_of_temperature_sensor { - sensor_vector.push(vec![sensor.component_name.to_string(), sensor.temperature.to_string() + "C"]); + sensor_vector.push(vec![ + sensor.component_name.to_string(), + sensor.temperature.to_string() + + match temp_type { + app::TemperatureType::Celsius => "C", + app::TemperatureType::Kelvin => "K", + app::TemperatureType::Fahrenheit => "F", + }, + ]); } sensor_vector