Add double tap key timeout to prevent accidental dd's

This commit is contained in:
ClementTsang 2019-12-16 02:21:44 -05:00
parent f8209c9162
commit bd37915567
3 changed files with 29 additions and 0 deletions

View file

@ -1,5 +1,8 @@
pub mod data_collection; pub mod data_collection;
use data_collection::{processes, temperature}; use data_collection::{processes, temperature};
use std::time::Instant;
use crate::constants;
mod process_killer; mod process_killer;
@ -41,6 +44,7 @@ pub struct App {
pub use_dot: bool, pub use_dot: bool,
pub show_help: bool, pub show_help: bool,
pub is_frozen: bool, pub is_frozen: bool,
last_key_press: Instant,
} }
impl App { impl App {
@ -66,20 +70,36 @@ impl App {
use_dot, use_dot,
show_help: false, show_help: false,
is_frozen: false, is_frozen: false,
last_key_press: Instant::now(),
} }
} }
pub fn reset(&mut self) {
self.reset_multi_tap_keys();
}
fn reset_multi_tap_keys(&mut self) {
self.awaiting_second_d = false;
}
pub fn on_enter(&mut self) {} pub fn on_enter(&mut self) {}
pub fn on_esc(&mut self) { pub fn on_esc(&mut self) {
if self.show_help { if self.show_help {
self.show_help = false; self.show_help = false;
} }
self.awaiting_second_d = false;
} }
// TODO: How should we make it for process panel specific hotkeys? Only if we're on process panel? Or what? // TODO: How should we make it for process panel specific hotkeys? Only if we're on process panel? Or what?
pub fn on_key(&mut self, c: char) { pub fn on_key(&mut self, c: char) {
if !self.show_help { if !self.show_help {
let current_key_press_inst = Instant::now();
if current_key_press_inst.duration_since(self.last_key_press).as_millis() > constants::MAX_KEY_TIMEOUT_IN_MILLISECONDS {
self.reset_multi_tap_keys();
}
self.last_key_press = current_key_press_inst;
match c { match c {
'd' => { 'd' => {
if self.awaiting_second_d { if self.awaiting_second_d {
@ -170,6 +190,7 @@ impl App {
ApplicationPosition::TEMP => ApplicationPosition::MEM, ApplicationPosition::TEMP => ApplicationPosition::MEM,
_ => self.current_application_position, _ => self.current_application_position,
}; };
self.awaiting_second_d = false;
} }
pub fn on_right(&mut self) { pub fn on_right(&mut self) {
@ -178,6 +199,7 @@ impl App {
ApplicationPosition::NETWORK => ApplicationPosition::PROCESS, ApplicationPosition::NETWORK => ApplicationPosition::PROCESS,
_ => self.current_application_position, _ => self.current_application_position,
}; };
self.awaiting_second_d = false;
} }
pub fn on_up(&mut self) { pub fn on_up(&mut self) {
@ -189,6 +211,7 @@ impl App {
ApplicationPosition::DISK => ApplicationPosition::TEMP, ApplicationPosition::DISK => ApplicationPosition::TEMP,
_ => self.current_application_position, _ => self.current_application_position,
}; };
self.awaiting_second_d = false;
} }
pub fn on_down(&mut self) { pub fn on_down(&mut self) {
@ -199,6 +222,7 @@ impl App {
ApplicationPosition::DISK => ApplicationPosition::PROCESS, ApplicationPosition::DISK => ApplicationPosition::PROCESS,
_ => self.current_application_position, _ => self.current_application_position,
}; };
self.awaiting_second_d = false;
} }
pub fn decrement_position_count(&mut self) { pub fn decrement_position_count(&mut self) {
@ -209,6 +233,7 @@ impl App {
_ => {} _ => {}
} }
self.scroll_direction = ScrollDirection::UP; self.scroll_direction = ScrollDirection::UP;
self.awaiting_second_d = false;
} }
pub fn increment_position_count(&mut self) { pub fn increment_position_count(&mut self) {
@ -219,6 +244,7 @@ impl App {
_ => {} _ => {}
} }
self.scroll_direction = ScrollDirection::DOWN; self.scroll_direction = ScrollDirection::DOWN;
self.awaiting_second_d = false;
} }
fn change_process_position(&mut self, num_to_change_by: i64) { fn change_process_position(&mut self, num_to_change_by: i64) {

View file

@ -3,3 +3,4 @@ pub const STALE_MAX_MILLISECONDS: u64 = 180 * 1000; // We wish to store at most
pub const TIME_STARTS_FROM: u64 = 60 * 1000; // TODO: Fix this pub const TIME_STARTS_FROM: u64 = 60 * 1000; // TODO: Fix this
pub const TICK_RATE_IN_MILLISECONDS: u64 = 200; // We use this as it's a good value to work with. pub const TICK_RATE_IN_MILLISECONDS: u64 = 200; // We use this as it's a good value to work with.
pub const DEFAULT_REFRESH_RATE_IN_MILLISECONDS: u128 = 1000; pub const DEFAULT_REFRESH_RATE_IN_MILLISECONDS: u128 = 1000;
pub const MAX_KEY_TIMEOUT_IN_MILLISECONDS: u128 = 1000;

View file

@ -60,6 +60,7 @@ fn main() -> error::Result<()> {
) )
(@arg RATE_MILLIS: -r --rate +takes_value "Sets a refresh rate in milliseconds; the minimum is 250ms, defaults to 1000ms. Smaller values may take more resources.") (@arg RATE_MILLIS: -r --rate +takes_value "Sets a refresh rate in milliseconds; the minimum is 250ms, defaults to 1000ms. Smaller values may take more resources.")
//(@arg CONFIG_LOCATION: -co --config +takes_value "Sets the location of the config file. Expects a config file in the JSON format.") //(@arg CONFIG_LOCATION: -co --config +takes_value "Sets the location of the config file. Expects a config file in the JSON format.")
//(@arg BASIC_MODE: -b --basic "Sets bottom to basic mode, not showing graphs and only showing basic tables.")
) )
.get_matches(); .get_matches();
@ -223,6 +224,7 @@ fn main() -> error::Result<()> {
debug!("Sent reset message."); debug!("Sent reset message.");
} }
debug!("Resetting begins..."); debug!("Resetting begins...");
app.reset();
} }
KeyEvent::Up => app.decrement_position_count(), KeyEvent::Up => app.decrement_position_count(),
KeyEvent::Down => app.increment_position_count(), KeyEvent::Down => app.increment_position_count(),