From 12b4518fa1c9b76ada04a57aa11ccc96a5529c2d Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Fri, 13 Dec 2019 02:42:36 -0500 Subject: [PATCH] Fixed problem with invalid vector index access. --- Cargo.toml | 10 ++-------- src/app/data_collection.rs | 4 ++++ src/canvas.rs | 15 ++++++++------- src/{convert_data.rs => data_conversion.rs} | 0 src/main.rs | 18 ++++++++---------- 5 files changed, 22 insertions(+), 25 deletions(-) rename src/{convert_data.rs => data_conversion.rs} (100%) diff --git a/Cargo.toml b/Cargo.toml index 1d2048f0..198bb10f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,16 +26,10 @@ heim = "0.0.8" log = "0.4" rayon = "1.2" regex = "1.3.1" -sysinfo = "0.10" +sysinfo = "0.9" tokio = "0.2.4" winapi = "0.3.8" - -[dependencies.tui] -# git = "https://github.com/ClementTsang/tui-rs" -# path = "../tui-rs" -version = "0.7" -default-features = false -features = ['crossterm'] +tui = {version = "0.7", features = ["crossterm"], default-features = false } [dev-dependencies] assert_cmd = "0.10" diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index 08f071ff..73c0008c 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -72,6 +72,10 @@ impl DataState { pub fn init(&mut self) { self.sys.refresh_system(); self.sys.refresh_network(); + if !cfg!(target_os = "linux") { + // For now, might be just windows tbh + self.sys.refresh_processes(); + } } pub async fn update_data(&mut self) { diff --git a/src/canvas.rs b/src/canvas.rs index cb586b33..c863cca4 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -104,19 +104,19 @@ pub fn draw_data(terminal: &mut Terminal, app_state: &mu .split(vertical_chunks[2]); // Component specific chunks - let cpu_chunk = Layout::default() + let _cpu_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) .split(vertical_chunks[0]); - let mem_chunk = Layout::default() + let _mem_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) .split(middle_chunks[0]); - let network_chunk = Layout::default() + let _network_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) @@ -156,12 +156,13 @@ pub fn draw_data(terminal: &mut Terminal, app_state: &mu } if !canvas_data.cpu_data.is_empty() && app_state.show_average_cpu { + // Unwrap should be safe here, this assumes that the cpu_data vector is populated... dataset_vector.push( Dataset::default() - .name(&canvas_data.cpu_data[0].0) + .name(&canvas_data.cpu_data.first().unwrap().0) .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) .style(Style::default().fg(COLOUR_LIST[canvas_data.cpu_data.len() - 1 % COLOUR_LIST.len()])) - .data(&(canvas_data.cpu_data[0].1)), + .data(&(canvas_data.cpu_data.first().unwrap().1)), ); } @@ -195,8 +196,8 @@ pub fn draw_data(terminal: &mut Terminal, app_state: &mu + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)) + &format!( " {:.1}GB/{:.1}GB", - canvas_data.mem_values[0].0 as f64 / 1024.0, - canvas_data.mem_values[0].1 as f64 / 1024.0 + canvas_data.mem_values.first().unwrap_or(&(0, 0)).0 as f64 / 1024.0, + canvas_data.mem_values.first().unwrap_or(&(0, 0)).1 as f64 / 1024.0 ); let swap_name: String; diff --git a/src/convert_data.rs b/src/data_conversion.rs similarity index 100% rename from src/convert_data.rs rename to src/data_conversion.rs diff --git a/src/main.rs b/src/main.rs index 0ee010d5..473dd9a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,11 @@ mod utils { } mod canvas; mod constants; -mod convert_data; +mod data_conversion; use app::data_collection; use constants::TICK_RATE_IN_MILLISECONDS; -use convert_data::*; +use data_conversion::*; use utils::error::{self, RustopError}; // End imports @@ -175,17 +175,15 @@ fn main() -> error::Result<()> { } // Event loop - + let mut data_state = data_collection::DataState::default(); + data_state.init(); + data_state.set_temperature_type(app.temperature_type.clone()); let (rtx, rrx) = mpsc::channel(); { let tx = tx.clone(); - let temp_type = app.temperature_type.clone(); let mut first_run = true; thread::spawn(move || { let tx = tx.clone(); - let mut data_state = data_collection::DataState::default(); - data_state.init(); - data_state.set_temperature_type(temp_type); loop { if let Ok(message) = rrx.try_recv() { match message { @@ -282,19 +280,19 @@ fn main() -> error::Result<()> { canvas_data.swap_data = update_swap_data_points(&app.data); canvas_data.cpu_data = update_cpu_data_points(app.show_average_cpu, &app.data); - debug!("Update event complete."); + //debug!("Update event complete."); } } } } // Draw! if let Err(err) = canvas::draw_data(&mut terminal, &mut app, &canvas_data) { - input().disable_mouse_mode().unwrap(); + input().disable_mouse_mode()?; error!("{}", err); return Err(err); } } - input().disable_mouse_mode().unwrap(); + input().disable_mouse_mode()?; Ok(()) }