From bd379155677ee94944a2abdb7619260be58a382d Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Mon, 16 Dec 2019 02:21:44 -0500 Subject: [PATCH] Add double tap key timeout to prevent accidental dd's --- src/app.rs | 26 ++++++++++++++++++++++++++ src/constants.rs | 1 + src/main.rs | 2 ++ 3 files changed, 29 insertions(+) diff --git a/src/app.rs b/src/app.rs index 59f376a3..660a70bb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,5 +1,8 @@ pub mod data_collection; use data_collection::{processes, temperature}; +use std::time::Instant; + +use crate::constants; mod process_killer; @@ -41,6 +44,7 @@ pub struct App { pub use_dot: bool, pub show_help: bool, pub is_frozen: bool, + last_key_press: Instant, } impl App { @@ -66,20 +70,36 @@ impl App { use_dot, show_help: 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_esc(&mut self) { if self.show_help { 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? pub fn on_key(&mut self, c: char) { 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 { 'd' => { if self.awaiting_second_d { @@ -170,6 +190,7 @@ impl App { ApplicationPosition::TEMP => ApplicationPosition::MEM, _ => self.current_application_position, }; + self.awaiting_second_d = false; } pub fn on_right(&mut self) { @@ -178,6 +199,7 @@ impl App { ApplicationPosition::NETWORK => ApplicationPosition::PROCESS, _ => self.current_application_position, }; + self.awaiting_second_d = false; } pub fn on_up(&mut self) { @@ -189,6 +211,7 @@ impl App { ApplicationPosition::DISK => ApplicationPosition::TEMP, _ => self.current_application_position, }; + self.awaiting_second_d = false; } pub fn on_down(&mut self) { @@ -199,6 +222,7 @@ impl App { ApplicationPosition::DISK => ApplicationPosition::PROCESS, _ => self.current_application_position, }; + self.awaiting_second_d = false; } pub fn decrement_position_count(&mut self) { @@ -209,6 +233,7 @@ impl App { _ => {} } self.scroll_direction = ScrollDirection::UP; + self.awaiting_second_d = false; } pub fn increment_position_count(&mut self) { @@ -219,6 +244,7 @@ impl App { _ => {} } self.scroll_direction = ScrollDirection::DOWN; + self.awaiting_second_d = false; } fn change_process_position(&mut self, num_to_change_by: i64) { diff --git a/src/constants.rs b/src/constants.rs index be32a841..0e9b18cb 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -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 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 MAX_KEY_TIMEOUT_IN_MILLISECONDS: u128 = 1000; diff --git a/src/main.rs b/src/main.rs index ada8b05d..f15090ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 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(); @@ -223,6 +224,7 @@ fn main() -> error::Result<()> { debug!("Sent reset message."); } debug!("Resetting begins..."); + app.reset(); } KeyEvent::Up => app.decrement_position_count(), KeyEvent::Down => app.increment_position_count(),