Added hotkey to reset all data on screen.

This commit is contained in:
ClementTsang 2019-10-10 18:01:23 -04:00
parent 2e6f087a3a
commit d35ddf3c6a
3 changed files with 26 additions and 29 deletions

View File

@ -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/<PID>/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) .

View File

@ -60,7 +60,8 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, 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"),

View File

@ -33,6 +33,10 @@ enum Event<I, J> {
Update(Box<data_collection::Data>),
}
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,