From d35ddf3c6a2dc13e5c2afe83df40fca53f84942e Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Thu, 10 Oct 2019 18:01:23 -0400 Subject: [PATCH] Added hotkey to reset all data on screen. --- README.md | 31 +++---------------------------- src/canvas.rs | 3 ++- src/main.rs | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index f5b28be2..3e72fac7 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,9 @@ Currently, I'm unable to really dev or test on MacOS, so I'm not sure how well t #### General -- `q`, `Ctrl-C` to quit. +- `q`, `Ctrl-c` to quit. + +- `Ctrl-r` to reset the screen and reset all collected data. - `f` to freeze the screen from updating with new data. Press `f` again to unfreeze. Note that monitoring will still continue in the background. @@ -75,33 +77,6 @@ Currently, I'm unable to really dev or test on MacOS, so I'm not sure how well t - Scrolling currently only scrolls through the list if you are on the Processes panel. This will change in the future. -## Regarding Process Use Percentage (on Linux) - -I cannot guarantee whether they are 100% accurate. I'm using a technique I found online which seems to be a better indicator of process use percentage at the current time, rather than pulling from, say, `ps` (which is average CPU usage over the _entire lifespan_ of the process). I found the options from the library I used to get other data (heim) to be a bit too inaccurate in my testing. - -For reference, the formula I am using to calculate CPU process usage is along the lines of: - -```rust -let idle = idle + iowait; -let non_idle = user + nice + system + irq + softirq + steal + guest; - -let total = idle + non_idle; -let prev_total = prev_idle + prev_non_idle; // Both of these values are calculated using the same formula from the previous polling - -let total_delta : f64 = total - prev_total; -let idle_delta : f64 = idle - prev_idle; - -let final_delta : f64 = total_delta - idle_delta; - -//... - -// Get utime and stime from reading /proc//stat -let after_proc_val = utime + stime; -(after_proc_val - before_proc_val) / cpu_usage * 100_f64; //This gives your use percentage. before_proc_val comes from the previous polling -``` - -Any suggestions on more accurate data is welcome! Note all other fields should be accurate. - ## Thanks - As mentioned, this project is very much inspired by both [gotop](https://github.com/cjbassi/gotop) and [gtop](https://github.com/aksakalli/gtop) . diff --git a/src/canvas.rs b/src/canvas.rs index 04d66b54..ae60873a 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -60,7 +60,8 @@ pub fn draw_data(terminal : &mut Terminal, app_state : if app_state.show_help { let text = [ Text::raw("\nGeneral Keybinds\n"), - Text::raw("q, Ctrl-C to quit.\n"), + Text::raw("q, Ctrl-c to quit.\n"), + Text::raw("Ctrl-r to reset all data.\n"), Text::raw("f to toggle freezing and unfreezing the display.\n"), Text::raw("Up/k, Down/j, Left/h, Right/l to navigate between panels.\n"), Text::raw("Shift+Up and Shift+Down scrolls through the list.\n"), diff --git a/src/main.rs b/src/main.rs index d9825b10..ad05f8cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,10 @@ enum Event { Update(Box), } +enum ResetEvent { + Reset, +} + fn main() -> error::Result<()> { utils::logging::init_logger()?; @@ -156,12 +160,22 @@ fn main() -> error::Result<()> { data_state.init(); data_state.set_stale_max_seconds(STALE_MAX_MILLISECONDS / 1000); data_state.set_temperature_type(app.temperature_type.clone()); + let (rtx, rrx) = mpsc::channel(); { let tx = tx.clone(); let mut first_run = true; thread::spawn(move || { let tx = tx.clone(); loop { + if let Ok(message) = rrx.try_recv() { + match message { + ResetEvent::Reset => { + debug!("Received reset message"); + first_run = true; + data_state.data = app::data_collection::Data::default(); + } + } + } futures::executor::block_on(data_state.update_data()); tx.send(Event::Update(Box::from(data_state.data.clone()))).unwrap(); if first_run { @@ -188,6 +202,12 @@ fn main() -> error::Result<()> { KeyEvent::Char('l') | KeyEvent::Right => app.on_right(), KeyEvent::Char('k') | KeyEvent::Up => app.on_up(), KeyEvent::Char('j') | KeyEvent::Down => app.on_down(), + KeyEvent::Ctrl('r') => { + while rtx.send(ResetEvent::Reset).is_err() { + debug!("Sent reset message."); + } + debug!("Resetting begins..."); + } KeyEvent::ShiftUp => app.decrement_position_count(), KeyEvent::ShiftDown => app.increment_position_count(), KeyEvent::Char(c) => app.on_key(c), @@ -228,6 +248,7 @@ fn main() -> error::Result<()> { // debug!("Update event fired!"); if !app.is_frozen { app.data = *data; + data_collection::processes::sort_processes( &mut app.data.list_of_processes, &app.process_sorting_type,