diff options
author | Mark Wong <markwkm@gmail.com> | 2021-01-18 11:05:15 -0800 |
---|---|---|
committer | Mark Wong <markwkm@gmail.com> | 2021-01-18 11:05:15 -0800 |
commit | e6063aaf5d32992db325092e93e2fa10149105a2 (patch) | |
tree | a6e6cdcc5d1e97e84ec4a125f867540fdac539a0 | |
parent | 84e7bcbe1f4c9e4b06de1cd61559c1f5d836cc69 (diff) |
Process keyboard inputrust
Currently only process 'q' for quitting.
-rw-r--r-- | src/pg_top.rs | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/src/pg_top.rs b/src/pg_top.rs index e45f492..992b13f 100644 --- a/src/pg_top.rs +++ b/src/pg_top.rs @@ -10,6 +10,7 @@ use std::env; use std::io::{stdout, Write}; use std::thread; use std::time::Duration; +use termion::input::TermRead; use termion::raw::IntoRawMode; pub struct PGTOP { @@ -18,6 +19,17 @@ pub struct PGTOP { pub cpu: os::CPUSTAT, } +fn move_to_end(line: u16) { + let mut stdout = stdout().into_raw_mode().unwrap(); + write!( + stdout, + "{}{}", + termion::cursor::Goto(1, line), + termion::cursor::Show, + ) + .unwrap(); +} + fn main() { let matches = App::new("pg_top") .version("5.0.0") @@ -36,6 +48,13 @@ fn main() { .get_matches(); let delay = matches.value_of("DELAY").unwrap().parse::<u64>().unwrap(); + + let infinite_loop = if matches.value_of("COUNT").is_none() { + true + } else { + false + }; + let mut count = if matches.value_of("COUNT").is_none() { 1 } else { @@ -82,6 +101,8 @@ fn main() { }, }; + let mut stdin = termion::async_stdin().keys(); + let mut stdout = stdout().into_raw_mode().unwrap(); write!(stdout, "{}{}", termion::clear::All, termion::cursor::Hide).unwrap(); @@ -90,6 +111,21 @@ fn main() { let mut processes = postgresql::get_processes(&mut client); let terminal_size = termion::terminal_size().unwrap(); + /* Process any input. */ + + let input = stdin.next(); + if let Some(Ok(key)) = input { + match key { + termion::event::Key::Char('q') => { + move_to_end(terminal_size.1); + break; + } + _ => { /* */ } + } + } + + /* Update the display. */ + display::loadavg(terminal_size.0 as usize); processes = display::process_summary(processes, terminal_size.0 as usize); pg_top = display::stat_cpu(pg_top, terminal_size.0 as usize); @@ -97,15 +133,11 @@ fn main() { write!(stdout, "{}", termion::clear::AfterCursor,).unwrap(); stdout.flush().unwrap(); - count = count - 1; + if infinite_loop == false { + count = count - 1; + } if count == 0 { - write!( - stdout, - "{}{}", - termion::cursor::Goto(1, terminal_size.1), - termion::cursor::Show, - ) - .unwrap(); + move_to_end(terminal_size.1); break; } thread::sleep(Duration::from_secs(delay)); |