diff --git a/src/app.rs b/src/app.rs index f40ea123..ec577661 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,17 +7,12 @@ mod process_killer; pub mod query; pub mod widgets; -use std::{ - cmp::{max, min}, - collections::HashMap, - time::Instant, -}; +use std::{collections::HashMap, time::Instant}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent}; use fxhash::FxHashMap; use indextree::{Arena, NodeId}; -use unicode_segmentation::GraphemeCursor; -use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; +use unicode_width::UnicodeWidthStr; pub use data_farmer::*; use data_harvester::{processes, temperature}; @@ -26,8 +21,7 @@ use layout_manager::*; pub use widgets::*; use crate::{ - canvas, - constants::{self, MAX_SIGNAL}, + canvas, constants, units::data_units::DataUnit, utils::error::{BottomError, Result}, BottomEvent, Pid, @@ -35,8 +29,6 @@ use crate::{ use self::event::{ComponentEventResult, EventResult, ReturnSignal}; -const MAX_SEARCH_LENGTH: usize = 200; - #[derive(Debug, Clone)] pub enum AxisScaling { Log, @@ -152,12 +144,6 @@ pub struct AppState { pub widget_map: HashMap, pub current_widget: BottomWidget, - last_key_press: Instant, - - awaiting_second_char: bool, - - second_char: Option, - pub basic_mode_use_percent: bool, pub is_force_redraw: bool, @@ -214,9 +200,6 @@ impl AppState { basic_table_widget_state: Default::default(), widget_map: Default::default(), current_widget: Default::default(), - last_key_press: Instant::now(), - awaiting_second_char: Default::default(), - second_char: Default::default(), basic_mode_use_percent: Default::default(), is_force_redraw: Default::default(), is_determining_widget_boundary: Default::default(), @@ -533,11 +516,6 @@ impl AppState { ) } - fn reset_multi_tap_keys(&mut self) { - self.awaiting_second_char = false; - self.second_char = None; - } - fn is_in_dialog(&self) -> bool { self.delete_dialog_state.is_showing_dd } @@ -629,376 +607,6 @@ impl AppState { } } - pub fn on_slash(&mut self) { - if !self.ignore_normal_keybinds() { - match &self.current_widget.widget_type { - BottomWidgetType::Proc | BottomWidgetType::ProcSort => { - // Toggle on - if let Some(proc_widget_state) = self.proc_state.get_mut_widget_state( - self.current_widget.widget_id - - match &self.current_widget.widget_type { - BottomWidgetType::ProcSort => 2, - _ => 0, - }, - ) { - proc_widget_state - .process_search_state - .search_state - .is_enabled = true; - self.move_widget_selection(&WidgetDirection::Down); - self.is_force_redraw = true; - } - } - _ => {} - } - } - } - - pub fn toggle_sort(&mut self) { - match &self.current_widget.widget_type { - BottomWidgetType::Proc | BottomWidgetType::ProcSort => { - let widget_id = self.current_widget.widget_id - - match &self.current_widget.widget_type { - BottomWidgetType::Proc => 0, - BottomWidgetType::ProcSort => 2, - _ => 0, - }; - - if let Some(proc_widget_state) = self.proc_state.get_mut_widget_state(widget_id) { - // Open up sorting dialog for that specific proc widget. - // TODO: It might be a decent idea to allow sorting ALL? I dunno. - - proc_widget_state.is_sort_open = !proc_widget_state.is_sort_open; - if proc_widget_state.is_sort_open { - // If it just opened, move left - proc_widget_state - .columns - .set_to_sorted_index_from_type(&proc_widget_state.process_sorting_type); - self.move_widget_selection(&WidgetDirection::Left); - } else { - // Otherwise, move right if currently on the sort widget - if let BottomWidgetType::ProcSort = self.current_widget.widget_type { - self.move_widget_selection(&WidgetDirection::Right); - } - } - } - - self.is_force_redraw = true; - } - _ => {} - } - } - - pub fn invert_sort(&mut self) { - match &self.current_widget.widget_type { - BottomWidgetType::Proc | BottomWidgetType::ProcSort => { - let widget_id = self.current_widget.widget_id - - match &self.current_widget.widget_type { - BottomWidgetType::Proc => 0, - BottomWidgetType::ProcSort => 2, - _ => 0, - }; - - if let Some(proc_widget_state) = self.proc_state.get_mut_widget_state(widget_id) { - proc_widget_state.is_process_sort_descending = - !proc_widget_state.is_process_sort_descending; - - self.proc_state.force_update = Some(widget_id); - } - } - _ => {} - } - } - - pub fn toggle_percentages(&mut self) { - match &self.current_widget.widget_type { - BottomWidgetType::BasicMem => { - self.basic_mode_use_percent = !self.basic_mode_use_percent; // Oh god this is so lazy. - } - BottomWidgetType::Proc => { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - proc_widget_state - .columns - .toggle(&processes::ProcessSorting::Mem); - if let Some(mem_percent_state) = proc_widget_state - .columns - .toggle(&processes::ProcessSorting::MemPercent) - { - if proc_widget_state.process_sorting_type - == processes::ProcessSorting::MemPercent - || proc_widget_state.process_sorting_type - == processes::ProcessSorting::Mem - { - if mem_percent_state { - proc_widget_state.process_sorting_type = - processes::ProcessSorting::MemPercent; - } else { - proc_widget_state.process_sorting_type = - processes::ProcessSorting::Mem; - } - } - } - - proc_widget_state.requires_redraw = true; - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - _ => {} - } - } - - pub fn toggle_ignore_case(&mut self) { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state - .process_search_state - .search_toggle_ignore_case(); - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - - pub fn toggle_search_whole_word(&mut self) { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state - .process_search_state - .search_toggle_whole_word(); - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - - pub fn toggle_search_regex(&mut self) { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state.process_search_state.search_toggle_regex(); - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - - pub fn toggle_tree_mode(&mut self) { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id)) - { - proc_widget_state.is_tree_mode = !proc_widget_state.is_tree_mode; - - // FIXME: For consistency, either disable tree mode if grouped, or allow grouped mode if in tree mode. - if proc_widget_state.is_tree_mode { - // Disable grouping if so! - proc_widget_state.is_grouped = false; - - proc_widget_state - .columns - .try_enable(&processes::ProcessSorting::State); - - #[cfg(target_family = "unix")] - proc_widget_state - .columns - .try_enable(&processes::ProcessSorting::User); - - proc_widget_state - .columns - .try_disable(&processes::ProcessSorting::Count); - - proc_widget_state - .columns - .try_enable(&processes::ProcessSorting::Pid); - - // We enabled... set PID sort type to ascending. - proc_widget_state.process_sorting_type = processes::ProcessSorting::Pid; - proc_widget_state.is_process_sort_descending = false; - } - - self.proc_state.force_update = Some(self.current_widget.widget_id); - proc_widget_state.requires_redraw = true; - } - } - - /// One of two functions allowed to run while in a dialog... - pub fn on_enter(&mut self) { - if self.delete_dialog_state.is_showing_dd { - if self.dd_err.is_some() { - self.close_dd(); - } else if self.delete_dialog_state.selected_signal != KillSignal::Cancel { - // If within dd... - if self.dd_err.is_none() { - // Also ensure that we didn't just fail a dd... - let dd_result = self.kill_highlighted_process(); - self.delete_dialog_state.scroll_pos = 0; - self.delete_dialog_state.selected_signal = KillSignal::default(); - - // Check if there was an issue... if so, inform the user. - if let Err(dd_err) = dd_result { - self.dd_err = Some(dd_err.to_string()); - } else { - self.delete_dialog_state.is_showing_dd = false; - } - } - } else { - self.delete_dialog_state.scroll_pos = 0; - self.delete_dialog_state.selected_signal = KillSignal::default(); - self.delete_dialog_state.is_showing_dd = false; - } - self.is_force_redraw = true; - } else if !self.is_in_dialog() { - if let BottomWidgetType::ProcSort = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 2)) - { - proc_widget_state.update_sorting_with_columns(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 2); - self.toggle_sort(); - } - } - } - } - - pub fn on_delete(&mut self) { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget { - if proc_widget_state - .process_search_state - .search_state - .is_enabled - && proc_widget_state.get_search_cursor_position() - < proc_widget_state - .process_search_state - .search_state - .current_search_query - .len() - { - let current_cursor = proc_widget_state.get_search_cursor_position(); - proc_widget_state - .search_walk_forward(proc_widget_state.get_search_cursor_position()); - - let _removed_chars: String = proc_widget_state - .process_search_state - .search_state - .current_search_query - .drain(current_cursor..proc_widget_state.get_search_cursor_position()) - .collect(); - - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - current_cursor, - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } else { - self.start_killing_process() - } - } - } - } - - pub fn on_backspace(&mut self) { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget - && proc_widget_state - .process_search_state - .search_state - .is_enabled - && proc_widget_state.get_search_cursor_position() > 0 - { - let current_cursor = proc_widget_state.get_search_cursor_position(); - proc_widget_state - .search_walk_back(proc_widget_state.get_search_cursor_position()); - - let removed_chars: String = proc_widget_state - .process_search_state - .search_state - .current_search_query - .drain(proc_widget_state.get_search_cursor_position()..current_cursor) - .collect(); - - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - proc_widget_state.get_search_cursor_position(), - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - - proc_widget_state - .process_search_state - .search_state - .char_cursor_position -= UnicodeWidthStr::width(removed_chars.as_str()); - - proc_widget_state - .process_search_state - .search_state - .cursor_direction = CursorDirection::Left; - - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - } - - pub fn get_process_filter(&self, widget_id: u64) -> &Option { - if let Some(process_widget_state) = self.proc_state.widget_states.get(&widget_id) { - &process_widget_state.process_search_state.search_state.query - } else { - &None - } - } - #[cfg(target_family = "unix")] pub fn on_number(&mut self, number_char: char) { if self.delete_dialog_state.is_showing_dd { @@ -1033,48 +641,6 @@ impl AppState { } } - pub fn on_up_key(&mut self) { - if !self.is_in_dialog() { - self.decrement_position_count(); - } else if self.help_dialog.is_showing() { - self.help_scroll_up(); - } else if self.delete_dialog_state.is_showing_dd { - #[cfg(target_os = "windows")] - self.on_right_key(); - #[cfg(target_family = "unix")] - { - if self.app_config_fields.is_advanced_kill { - self.on_left_key(); - } else { - self.on_right_key(); - } - } - return; - } - self.reset_multi_tap_keys(); - } - - pub fn on_down_key(&mut self) { - if !self.is_in_dialog() { - self.increment_position_count(); - } else if self.help_dialog.is_showing() { - self.help_scroll_down(); - } else if self.delete_dialog_state.is_showing_dd { - #[cfg(target_os = "windows")] - self.on_left_key(); - #[cfg(target_family = "unix")] - { - if self.app_config_fields.is_advanced_kill { - self.on_right_key(); - } else { - self.on_left_key(); - } - } - return; - } - self.reset_multi_tap_keys(); - } - pub fn on_left_key(&mut self) { if !self.is_in_dialog() { match self.current_widget.widget_type { @@ -1221,206 +787,6 @@ impl AppState { } } - pub fn on_page_up(&mut self) { - if self.delete_dialog_state.is_showing_dd { - let mut new_signal = match self.delete_dialog_state.selected_signal { - KillSignal::Cancel => 0, - KillSignal::Kill(signal) => max(signal, 8) - 8, - }; - if new_signal > 23 && new_signal < 33 { - new_signal -= 2; - } - self.delete_dialog_state.selected_signal = match new_signal { - 0 => KillSignal::Cancel, - sig => KillSignal::Kill(sig), - }; - } - } - - pub fn on_page_down(&mut self) { - if self.delete_dialog_state.is_showing_dd { - let mut new_signal = match self.delete_dialog_state.selected_signal { - KillSignal::Cancel => 8, - KillSignal::Kill(signal) => min(signal + 8, MAX_SIGNAL), - }; - if new_signal > 31 && new_signal < 42 { - new_signal += 2; - } - self.delete_dialog_state.selected_signal = KillSignal::Kill(new_signal); - } - } - - pub fn skip_cursor_beginning(&mut self) { - if !self.ignore_normal_keybinds() { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget { - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - 0, - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - proc_widget_state - .process_search_state - .search_state - .char_cursor_position = 0; - proc_widget_state - .process_search_state - .search_state - .cursor_direction = CursorDirection::Left; - } - } - } - } - } - - pub fn skip_cursor_end(&mut self) { - if !self.ignore_normal_keybinds() { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget { - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - proc_widget_state - .process_search_state - .search_state - .char_cursor_position = UnicodeWidthStr::width( - proc_widget_state - .process_search_state - .search_state - .current_search_query - .as_str(), - ); - proc_widget_state - .process_search_state - .search_state - .cursor_direction = CursorDirection::Right; - } - } - } - } - } - - pub fn clear_search(&mut self) { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - proc_widget_state.clear_search(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - - pub fn clear_previous_word(&mut self) { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - // Traverse backwards from the current cursor location until you hit non-whitespace characters, - // then continue to traverse (and delete) backwards until you hit a whitespace character. Halt. - - // So... first, let's get our current cursor position using graphemes... - let end_index = proc_widget_state.get_char_cursor_position(); - - // Then, let's crawl backwards until we hit our location, and store the "head"... - let query = proc_widget_state.get_current_search_query(); - let mut start_index = 0; - let mut saw_non_whitespace = false; - - for (itx, c) in query - .chars() - .rev() - .enumerate() - .skip(query.len() - end_index) - { - if c.is_whitespace() { - if saw_non_whitespace { - start_index = query.len() - itx; - break; - } - } else { - saw_non_whitespace = true; - } - } - - let removed_chars: String = proc_widget_state - .process_search_state - .search_state - .current_search_query - .drain(start_index..end_index) - .collect(); - - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - start_index, - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - - proc_widget_state - .process_search_state - .search_state - .char_cursor_position -= UnicodeWidthStr::width(removed_chars.as_str()); - - proc_widget_state - .process_search_state - .search_state - .cursor_direction = CursorDirection::Left; - - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - - // Now, convert this range into a String-friendly range and remove it all at once! - - // Now make sure to also update our current cursor positions... - - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - } - } - } - pub fn start_killing_process(&mut self) { todo!() @@ -1461,294 +827,6 @@ impl AppState { // } } - pub fn on_char_key(&mut self, caught_char: char) { - // Skip control code chars - if caught_char.is_control() { - return; - } - - // Forbid any char key presses when showing a dialog box... - if !self.ignore_normal_keybinds() { - 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 as u128 - { - self.reset_multi_tap_keys(); - } - self.last_key_press = current_key_press_inst; - - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - let is_in_search_widget = self.is_in_search_widget(); - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget - && proc_widget_state.is_search_enabled() - && UnicodeWidthStr::width( - proc_widget_state - .process_search_state - .search_state - .current_search_query - .as_str(), - ) <= MAX_SEARCH_LENGTH - { - proc_widget_state - .process_search_state - .search_state - .current_search_query - .insert(proc_widget_state.get_search_cursor_position(), caught_char); - - proc_widget_state - .process_search_state - .search_state - .grapheme_cursor = GraphemeCursor::new( - proc_widget_state.get_search_cursor_position(), - proc_widget_state - .process_search_state - .search_state - .current_search_query - .len(), - true, - ); - proc_widget_state - .search_walk_forward(proc_widget_state.get_search_cursor_position()); - - proc_widget_state - .process_search_state - .search_state - .char_cursor_position += - UnicodeWidthChar::width(caught_char).unwrap_or(0); - - proc_widget_state.update_query(); - self.proc_state.force_update = Some(self.current_widget.widget_id - 1); - proc_widget_state - .process_search_state - .search_state - .cursor_direction = CursorDirection::Right; - - return; - } - } - } - self.handle_char(caught_char); - } else if self.help_dialog.is_showing() { - match caught_char { - '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {} - 'j' | 'k' | 'g' | 'G' => self.handle_char(caught_char), - _ => {} - } - } else if self.delete_dialog_state.is_showing_dd { - match caught_char { - 'h' => self.on_left_key(), - 'j' => self.on_down_key(), - 'k' => self.on_up_key(), - 'l' => self.on_right_key(), - #[cfg(target_family = "unix")] - '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => { - self.on_number(caught_char) - } - 'u' => self.on_page_up(), - 'd' => self.on_page_down(), - 'g' => { - let mut is_first_g = true; - if let Some(second_char) = self.second_char { - if self.awaiting_second_char && second_char == 'g' { - is_first_g = false; - self.awaiting_second_char = false; - self.second_char = None; - self.skip_to_first(); - } - } - - if is_first_g { - self.awaiting_second_char = true; - self.second_char = Some('g'); - } - } - 'G' => self.skip_to_last(), - _ => {} - } - } - } - - fn handle_char(&mut self, caught_char: char) { - match caught_char { - '/' => { - self.on_slash(); - } - 'd' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - let mut is_first_d = true; - if let Some(second_char) = self.second_char { - if self.awaiting_second_char && second_char == 'd' { - is_first_d = false; - self.awaiting_second_char = false; - self.second_char = None; - - self.start_killing_process(); - } - } - - if is_first_d { - self.awaiting_second_char = true; - self.second_char = Some('d'); - } - } - } - 'g' => { - let mut is_first_g = true; - if let Some(second_char) = self.second_char { - if self.awaiting_second_char && second_char == 'g' { - is_first_g = false; - self.awaiting_second_char = false; - self.second_char = None; - self.skip_to_first(); - } - } - - if is_first_g { - self.awaiting_second_char = true; - self.second_char = Some('g'); - } - } - 'G' => self.skip_to_last(), - 'k' => self.on_up_key(), - 'j' => self.on_down_key(), - 'f' => {} - 'C' => { - // self.open_config(), - } - 'c' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state - .columns - .set_to_sorted_index_from_type(&processes::ProcessSorting::CpuPercent); - proc_widget_state.update_sorting_with_columns(); - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - } - 'm' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.columns.set_to_sorted_index_from_type( - &(if proc_widget_state - .columns - .is_enabled(&processes::ProcessSorting::MemPercent) - { - processes::ProcessSorting::MemPercent - } else { - processes::ProcessSorting::Mem - }), - ); - proc_widget_state.update_sorting_with_columns(); - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - } - 'p' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - // Skip if grouped - if !proc_widget_state.is_grouped { - proc_widget_state - .columns - .set_to_sorted_index_from_type(&processes::ProcessSorting::Pid); - proc_widget_state.update_sorting_with_columns(); - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - } - } - 'P' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.is_using_command = !proc_widget_state.is_using_command; - proc_widget_state - .toggle_command_and_name(proc_widget_state.is_using_command); - - match &proc_widget_state.process_sorting_type { - processes::ProcessSorting::Command - | processes::ProcessSorting::ProcessName => { - if proc_widget_state.is_using_command { - proc_widget_state.process_sorting_type = - processes::ProcessSorting::Command; - } else { - proc_widget_state.process_sorting_type = - processes::ProcessSorting::ProcessName; - } - } - _ => {} - } - proc_widget_state.requires_redraw = true; - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - } - 'n' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.columns.set_to_sorted_index_from_type( - &(if proc_widget_state.is_using_command { - processes::ProcessSorting::Command - } else { - processes::ProcessSorting::ProcessName - }), - ); - proc_widget_state.update_sorting_with_columns(); - self.proc_state.force_update = Some(self.current_widget.widget_id); - } - } - } - '?' => { - self.help_dialog.show(); - self.is_force_redraw = true; - } - 'H' | 'A' => self.move_widget_selection(&WidgetDirection::Left), - 'L' | 'D' => self.move_widget_selection(&WidgetDirection::Right), - 'K' | 'W' => self.move_widget_selection(&WidgetDirection::Up), - 'J' | 'S' => self.move_widget_selection(&WidgetDirection::Down), - 't' => self.toggle_tree_mode(), - '+' => self.on_plus(), - '-' => self.on_minus(), - '=' => self.reset_zoom(), - 'e' => self.toggle_expand_widget(), - 's' => self.toggle_sort(), - 'I' => self.invert_sort(), - '%' => self.toggle_percentages(), - ' ' => self.on_space(), - _ => {} - } - - if let Some(second_char) = self.second_char { - if self.awaiting_second_char && caught_char != second_char { - self.awaiting_second_char = false; - } - } - } - - pub fn on_space(&mut self) {} - pub fn kill_highlighted_process(&mut self) -> Result<()> { if let BottomWidgetType::Proc = self.current_widget.widget_type { if let Some(current_selected_processes) = &self.to_delete_process_list { @@ -1781,913 +859,4 @@ impl AppState { pub fn get_to_delete_processes(&self) -> Option<(String, Vec)> { self.to_delete_process_list.clone() } - - fn toggle_expand_widget(&mut self) { - if self.is_expanded { - self.is_expanded = false; - self.is_force_redraw = true; - } else { - self.expand_widget(); - } - } - - fn expand_widget(&mut self) { - if !self.ignore_normal_keybinds() && !self.app_config_fields.use_basic_mode { - // Pop-out mode. We ignore if in process search. - - match self.current_widget.widget_type { - BottomWidgetType::ProcSearch => {} - _ => { - self.is_expanded = true; - self.is_force_redraw = true; - } - } - } - } - - pub fn move_widget_selection(&mut self, direction: &WidgetDirection) { - // Since we only want to call reset once, we do it like this to avoid - // redundant calls on recursion. - self.move_widget_selection_logic(direction); - self.reset_multi_tap_keys(); - } - - fn move_widget_selection_logic(&mut self, direction: &WidgetDirection) { - /* - The actual logic for widget movement. - - We follow these following steps: - 1. Send a movement signal in `direction`. - 2. Check if this new widget we've landed on is hidden. If not, halt. - 3. If it hidden, loop and either send: - - A signal equal to the current direction, if it is opposite of the reflection. - - Reflection direction. - */ - - if !self.ignore_normal_keybinds() && !self.is_expanded { - if let Some(new_widget_id) = &(match direction { - WidgetDirection::Left => self.current_widget.left_neighbour, - WidgetDirection::Right => self.current_widget.right_neighbour, - WidgetDirection::Up => self.current_widget.up_neighbour, - WidgetDirection::Down => self.current_widget.down_neighbour, - }) { - if let Some(new_widget) = self.widget_map.get(new_widget_id) { - match &new_widget.widget_type { - BottomWidgetType::Temp - | BottomWidgetType::Proc - | BottomWidgetType::ProcSort - | BottomWidgetType::Disk - | BottomWidgetType::Battery - if self.basic_table_widget_state.is_some() - && (*direction == WidgetDirection::Left - || *direction == WidgetDirection::Right) => - { - // Gotta do this for the sort widget - if let BottomWidgetType::ProcSort = new_widget.widget_type { - if let Some(proc_widget_state) = - self.proc_state.widget_states.get(&(new_widget_id - 2)) - { - if proc_widget_state.is_sort_open { - self.current_widget = new_widget.clone(); - } else if let Some(next_new_widget_id) = match direction { - WidgetDirection::Left => new_widget.left_neighbour, - _ => new_widget.right_neighbour, - } { - if let Some(next_new_widget) = - self.widget_map.get(&next_new_widget_id) - { - self.current_widget = next_new_widget.clone(); - } - } - } - } else { - self.current_widget = new_widget.clone(); - } - - if let Some(basic_table_widget_state) = - &mut self.basic_table_widget_state - { - basic_table_widget_state.currently_displayed_widget_id = - self.current_widget.widget_id; - basic_table_widget_state.currently_displayed_widget_type = - self.current_widget.widget_type.clone(); - } - - // And let's not forget: - self.is_determining_widget_boundary = true; - } - BottomWidgetType::BasicTables => { - match &direction { - WidgetDirection::Up => { - // Note this case would fail if it moved up into a hidden - // widget, but it's for basic so whatever, it's all hard-coded - // right now anyways... - if let Some(next_new_widget_id) = new_widget.up_neighbour { - if let Some(next_new_widget) = - self.widget_map.get(&next_new_widget_id) - { - self.current_widget = next_new_widget.clone(); - } - } - } - WidgetDirection::Down => { - // Assuming we're in basic mode (BasicTables), then - // we want to move DOWN to the currently shown widget. - if let Some(basic_table_widget_state) = - &mut self.basic_table_widget_state - { - // We also want to move towards Proc if we had set it to ProcSort. - if let BottomWidgetType::ProcSort = - basic_table_widget_state.currently_displayed_widget_type - { - basic_table_widget_state - .currently_displayed_widget_type = - BottomWidgetType::Proc; - basic_table_widget_state - .currently_displayed_widget_id -= 2; - } - - if let Some(next_new_widget) = self.widget_map.get( - &basic_table_widget_state.currently_displayed_widget_id, - ) { - self.current_widget = next_new_widget.clone(); - } - } - } - _ => self.current_widget = new_widget.clone(), - } - } - _ if new_widget.parent_reflector.is_some() => { - // It may be hidden... - if let Some((parent_direction, offset)) = &new_widget.parent_reflector { - if direction.is_opposite(parent_direction) { - // Keep going in the current direction if hidden... - // unless we hit a wall of sorts. - let option_next_neighbour_id = match &direction { - WidgetDirection::Left => new_widget.left_neighbour, - WidgetDirection::Right => new_widget.right_neighbour, - WidgetDirection::Up => new_widget.up_neighbour, - WidgetDirection::Down => new_widget.down_neighbour, - }; - match &new_widget.widget_type { - BottomWidgetType::CpuLegend => { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get(&(new_widget_id - *offset)) - { - if cpu_widget_state.is_legend_hidden { - if let Some(next_neighbour_id) = - option_next_neighbour_id - { - if let Some(next_neighbour_widget) = - self.widget_map.get(&next_neighbour_id) - { - self.current_widget = - next_neighbour_widget.clone(); - } - } - } else { - self.current_widget = new_widget.clone(); - } - } - } - BottomWidgetType::ProcSearch - | BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get(&(new_widget_id - *offset)) - { - match &new_widget.widget_type { - BottomWidgetType::ProcSearch => { - if !proc_widget_state.is_search_enabled() { - if let Some(next_neighbour_id) = - option_next_neighbour_id - { - if let Some(next_neighbour_widget) = - self.widget_map - .get(&next_neighbour_id) - { - self.current_widget = - next_neighbour_widget - .clone(); - } - } - } else { - self.current_widget = - new_widget.clone(); - } - } - BottomWidgetType::ProcSort => { - if !proc_widget_state.is_sort_open { - if let Some(next_neighbour_id) = - option_next_neighbour_id - { - if let Some(next_neighbour_widget) = - self.widget_map - .get(&next_neighbour_id) - { - self.current_widget = - next_neighbour_widget - .clone(); - } - } - } else { - self.current_widget = - new_widget.clone(); - } - } - _ => { - self.current_widget = new_widget.clone(); - } - } - } - } - _ => { - self.current_widget = new_widget.clone(); - } - } - } else { - // Reflect - match &new_widget.widget_type { - BottomWidgetType::CpuLegend => { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get(&(new_widget_id - *offset)) - { - if cpu_widget_state.is_legend_hidden { - if let Some(parent_cpu_widget) = self - .widget_map - .get(&(new_widget_id - *offset)) - { - self.current_widget = - parent_cpu_widget.clone(); - } - } else { - self.current_widget = new_widget.clone(); - } - } - } - BottomWidgetType::ProcSearch - | BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get(&(new_widget_id - *offset)) - { - match &new_widget.widget_type { - BottomWidgetType::ProcSearch => { - if !proc_widget_state.is_search_enabled() { - if let Some(parent_proc_widget) = self - .widget_map - .get(&(new_widget_id - *offset)) - { - self.current_widget = - parent_proc_widget.clone(); - } - } else { - self.current_widget = - new_widget.clone(); - } - } - BottomWidgetType::ProcSort => { - if !proc_widget_state.is_sort_open { - if let Some(parent_proc_widget) = self - .widget_map - .get(&(new_widget_id - *offset)) - { - self.current_widget = - parent_proc_widget.clone(); - } - } else { - self.current_widget = - new_widget.clone(); - } - } - _ => { - self.current_widget = new_widget.clone(); - } - } - } - } - _ => { - self.current_widget = new_widget.clone(); - } - } - } - } - } - _ => { - // Cannot be hidden, does not special treatment. - self.current_widget = new_widget.clone(); - } - } - - let mut reflection_dir: Option = None; - if let Some((parent_direction, offset)) = &self.current_widget.parent_reflector - { - match &self.current_widget.widget_type { - BottomWidgetType::CpuLegend => { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get(&(self.current_widget.widget_id - *offset)) - { - if cpu_widget_state.is_legend_hidden { - reflection_dir = Some(parent_direction.clone()); - } - } - } - BottomWidgetType::ProcSearch | BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get(&(self.current_widget.widget_id - *offset)) - { - match &self.current_widget.widget_type { - BottomWidgetType::ProcSearch => { - if !proc_widget_state.is_search_enabled() { - reflection_dir = Some(parent_direction.clone()); - } - } - BottomWidgetType::ProcSort => { - if !proc_widget_state.is_sort_open { - reflection_dir = Some(parent_direction.clone()); - } - } - _ => {} - } - } - } - _ => {} - } - } - - if let Some(ref_dir) = &reflection_dir { - self.move_widget_selection_logic(ref_dir); - } - } - } - } else { - match direction { - WidgetDirection::Left => self.handle_left_expanded_movement(), - WidgetDirection::Right => self.handle_right_expanded_movement(), - WidgetDirection::Up => { - if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - if let Some(current_widget) = - self.widget_map.get(&self.current_widget.widget_id) - { - if let Some(new_widget_id) = current_widget.up_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = new_widget.clone(); - } - } - } - } - } - WidgetDirection::Down => match &self.current_widget.widget_type { - BottomWidgetType::Proc | BottomWidgetType::ProcSort => { - let widget_id = self.current_widget.widget_id - - match &self.current_widget.widget_type { - BottomWidgetType::ProcSort => 2, - _ => 0, - }; - if let Some(current_widget) = self.widget_map.get(&widget_id) { - if let Some(new_widget_id) = current_widget.down_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - if let Some(proc_widget_state) = - self.proc_state.get_widget_state(widget_id) - { - if proc_widget_state.is_search_enabled() { - self.current_widget = new_widget.clone(); - } - } - } - } - } - } - _ => {} - }, - } - } - } - - fn handle_left_expanded_movement(&mut self) { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(new_widget_id) = self.current_widget.left_neighbour { - if let Some(proc_widget_state) = self - .proc_state - .widget_states - .get(&self.current_widget.widget_id) - { - if proc_widget_state.is_sort_open { - if let Some(proc_sort_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = proc_sort_widget.clone(); - } - } - } - } - } else if self.app_config_fields.left_legend { - if let BottomWidgetType::Cpu = self.current_widget.widget_type { - if let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get(&self.current_widget.widget_id) - { - if !cpu_widget_state.is_legend_hidden { - if let Some(new_widget_id) = current_widget.left_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = new_widget.clone(); - } - } - } - } - } - } - } else if let BottomWidgetType::CpuLegend = self.current_widget.widget_type { - if let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) { - if let Some(new_widget_id) = current_widget.left_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = new_widget.clone(); - } - } - } - } - } - - fn handle_right_expanded_movement(&mut self) { - if let BottomWidgetType::ProcSort = self.current_widget.widget_type { - if let Some(new_widget_id) = self.current_widget.right_neighbour { - if let Some(proc_sort_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = proc_sort_widget.clone(); - } - } - } else if self.app_config_fields.left_legend { - if let BottomWidgetType::CpuLegend = self.current_widget.widget_type { - if let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) { - if let Some(new_widget_id) = current_widget.right_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = new_widget.clone(); - } - } - } - } - } else if let BottomWidgetType::Cpu = self.current_widget.widget_type { - if let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get(&self.current_widget.widget_id) - { - if !cpu_widget_state.is_legend_hidden { - if let Some(new_widget_id) = current_widget.right_neighbour { - if let Some(new_widget) = self.widget_map.get(&new_widget_id) { - self.current_widget = new_widget.clone(); - } - } - } - } - } - } - } - - pub fn skip_to_first(&mut self) { - if !self.ignore_normal_keybinds() { - match self.current_widget.widget_type { - BottomWidgetType::Proc => { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.scroll_state.current_scroll_position = 0; - proc_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } - } - BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id - 2) - { - proc_widget_state.columns.current_scroll_position = 0; - proc_widget_state.columns.scroll_direction = ScrollDirection::Up; - } - } - BottomWidgetType::Temp => { - if let Some(temp_widget_state) = self - .temp_state - .get_mut_widget_state(self.current_widget.widget_id) - { - temp_widget_state.scroll_state.current_scroll_position = 0; - temp_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } - } - BottomWidgetType::Disk => { - if let Some(disk_widget_state) = self - .disk_state - .get_mut_widget_state(self.current_widget.widget_id) - { - disk_widget_state.scroll_state.current_scroll_position = 0; - disk_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } - } - BottomWidgetType::CpuLegend => { - if let Some(cpu_widget_state) = self - .cpu_state - .get_mut_widget_state(self.current_widget.widget_id - 1) - { - cpu_widget_state.scroll_state.current_scroll_position = 0; - cpu_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } - } - - _ => {} - } - self.reset_multi_tap_keys(); - } else if self.help_dialog.is_showing() { - // self.help_dialog_state.scroll_state.current_scroll_index = 0; - } else if self.delete_dialog_state.is_showing_dd { - self.delete_dialog_state.selected_signal = KillSignal::Cancel; - } - } - - pub fn skip_to_last(&mut self) {} - - pub fn decrement_position_count(&mut self) { - if !self.ignore_normal_keybinds() { - match self.current_widget.widget_type { - BottomWidgetType::Proc => { - self.increment_process_position(-1); - } - BottomWidgetType::ProcSort => self.increment_process_sort_position(-1), - BottomWidgetType::Temp => self.increment_temp_position(-1), - BottomWidgetType::Disk => self.increment_disk_position(-1), - BottomWidgetType::CpuLegend => self.increment_cpu_legend_position(-1), - _ => {} - } - } - } - - pub fn increment_position_count(&mut self) { - if !self.ignore_normal_keybinds() { - match self.current_widget.widget_type { - BottomWidgetType::Proc => { - self.increment_process_position(1); - } - BottomWidgetType::ProcSort => self.increment_process_sort_position(1), - BottomWidgetType::Temp => self.increment_temp_position(1), - BottomWidgetType::Disk => self.increment_disk_position(1), - BottomWidgetType::CpuLegend => self.increment_cpu_legend_position(1), - _ => {} - } - } - } - - fn increment_process_sort_position(&mut self, num_to_change_by: i64) { - if let Some(proc_widget_state) = self - .proc_state - .get_mut_widget_state(self.current_widget.widget_id - 2) - { - let current_posn = proc_widget_state.columns.current_scroll_position; - let num_columns = proc_widget_state.columns.get_enabled_columns_len(); - - if current_posn as i64 + num_to_change_by >= 0 - && current_posn as i64 + num_to_change_by < num_columns as i64 - { - proc_widget_state.columns.current_scroll_position = - (current_posn as i64 + num_to_change_by) as usize; - } - - if num_to_change_by < 0 { - proc_widget_state.columns.scroll_direction = ScrollDirection::Up; - } else { - proc_widget_state.columns.scroll_direction = ScrollDirection::Down; - } - } - } - - fn increment_cpu_legend_position(&mut self, num_to_change_by: i64) { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - let current_posn = cpu_widget_state.scroll_state.current_scroll_position; - - let cap = self.canvas_data.cpu_data.len(); - if current_posn as i64 + num_to_change_by >= 0 - && current_posn as i64 + num_to_change_by < cap as i64 - { - cpu_widget_state.scroll_state.current_scroll_position = - (current_posn as i64 + num_to_change_by) as usize; - } - - if num_to_change_by < 0 { - cpu_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } else { - cpu_widget_state.scroll_state.scroll_direction = ScrollDirection::Down; - } - } - } - - /// Returns the new position. - fn increment_process_position(&mut self, _num_to_change_by: i64) -> Option { - None - } - - fn increment_temp_position(&mut self, num_to_change_by: i64) { - if let Some(temp_widget_state) = self - .temp_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let current_posn = temp_widget_state.scroll_state.current_scroll_position; - - if current_posn as i64 + num_to_change_by >= 0 - && current_posn as i64 + num_to_change_by - < self.canvas_data.temp_sensor_data.len() as i64 - { - temp_widget_state.scroll_state.current_scroll_position = - (current_posn as i64 + num_to_change_by) as usize; - } - - if num_to_change_by < 0 { - temp_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } else { - temp_widget_state.scroll_state.scroll_direction = ScrollDirection::Down; - } - } - } - - fn increment_disk_position(&mut self, num_to_change_by: i64) { - if let Some(disk_widget_state) = self - .disk_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let current_posn = disk_widget_state.scroll_state.current_scroll_position; - - if current_posn as i64 + num_to_change_by >= 0 - && current_posn as i64 + num_to_change_by < self.canvas_data.disk_data.len() as i64 - { - disk_widget_state.scroll_state.current_scroll_position = - (current_posn as i64 + num_to_change_by) as usize; - } - - if num_to_change_by < 0 { - disk_widget_state.scroll_state.scroll_direction = ScrollDirection::Up; - } else { - disk_widget_state.scroll_state.scroll_direction = ScrollDirection::Down; - } - } - } - - fn help_scroll_up(&mut self) { - // if self.help_dialog_state.scroll_state.current_scroll_index > 0 { - // self.help_dialog_state.scroll_state.current_scroll_index -= 1; - // } - } - - fn help_scroll_down(&mut self) { - // if self.help_dialog_state.scroll_state.current_scroll_index + 1 - // < self.help_dialog_state.scroll_state.max_scroll_index - // { - // self.help_dialog_state.scroll_state.current_scroll_index += 1; - // } - } - - fn on_plus(&mut self) { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - // Toggle collapsing if tree - self.toggle_collapsing_process_branch(); - } else { - self.zoom_in(); - } - } - - fn on_minus(&mut self) { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - // Toggle collapsing if tree - self.toggle_collapsing_process_branch(); - } else { - self.zoom_out(); - } - } - - fn toggle_collapsing_process_branch(&mut self) {} - - fn zoom_out(&mut self) { - match self.current_widget.widget_type { - BottomWidgetType::Cpu => { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = cpu_widget_state.current_display_time - + self.app_config_fields.time_interval; - if new_time <= constants::STALE_MAX_MILLISECONDS { - cpu_widget_state.current_display_time = new_time; - self.cpu_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - cpu_widget_state.autohide_timer = Some(Instant::now()); - } - } else if cpu_widget_state.current_display_time - != constants::STALE_MAX_MILLISECONDS - { - cpu_widget_state.current_display_time = constants::STALE_MAX_MILLISECONDS; - self.cpu_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - cpu_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - BottomWidgetType::Mem => { - if let Some(mem_widget_state) = self - .mem_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = mem_widget_state.current_display_time - + self.app_config_fields.time_interval; - if new_time <= constants::STALE_MAX_MILLISECONDS { - mem_widget_state.current_display_time = new_time; - self.mem_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - mem_widget_state.autohide_timer = Some(Instant::now()); - } - } else if mem_widget_state.current_display_time - != constants::STALE_MAX_MILLISECONDS - { - mem_widget_state.current_display_time = constants::STALE_MAX_MILLISECONDS; - self.mem_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - mem_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - BottomWidgetType::Net => { - if let Some(net_widget_state) = self - .net_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = net_widget_state.current_display_time - + self.app_config_fields.time_interval; - if new_time <= constants::STALE_MAX_MILLISECONDS { - net_widget_state.current_display_time = new_time; - self.net_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - net_widget_state.autohide_timer = Some(Instant::now()); - } - } else if net_widget_state.current_display_time - != constants::STALE_MAX_MILLISECONDS - { - net_widget_state.current_display_time = constants::STALE_MAX_MILLISECONDS; - self.net_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - net_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - _ => {} - } - } - - fn zoom_in(&mut self) { - match self.current_widget.widget_type { - BottomWidgetType::Cpu => { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = cpu_widget_state.current_display_time - - self.app_config_fields.time_interval; - if new_time >= constants::STALE_MIN_MILLISECONDS { - cpu_widget_state.current_display_time = new_time; - self.cpu_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - cpu_widget_state.autohide_timer = Some(Instant::now()); - } - } else if cpu_widget_state.current_display_time - != constants::STALE_MIN_MILLISECONDS - { - cpu_widget_state.current_display_time = constants::STALE_MIN_MILLISECONDS; - self.cpu_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - cpu_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - BottomWidgetType::Mem => { - if let Some(mem_widget_state) = self - .mem_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = mem_widget_state.current_display_time - - self.app_config_fields.time_interval; - if new_time >= constants::STALE_MIN_MILLISECONDS { - mem_widget_state.current_display_time = new_time; - self.mem_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - mem_widget_state.autohide_timer = Some(Instant::now()); - } - } else if mem_widget_state.current_display_time - != constants::STALE_MIN_MILLISECONDS - { - mem_widget_state.current_display_time = constants::STALE_MIN_MILLISECONDS; - self.mem_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - mem_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - BottomWidgetType::Net => { - if let Some(net_widget_state) = self - .net_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - let new_time = net_widget_state.current_display_time - - self.app_config_fields.time_interval; - if new_time >= constants::STALE_MIN_MILLISECONDS { - net_widget_state.current_display_time = new_time; - self.net_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - net_widget_state.autohide_timer = Some(Instant::now()); - } - } else if net_widget_state.current_display_time - != constants::STALE_MIN_MILLISECONDS - { - net_widget_state.current_display_time = constants::STALE_MIN_MILLISECONDS; - self.net_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - net_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - } - _ => {} - } - } - - fn reset_cpu_zoom(&mut self) { - if let Some(cpu_widget_state) = self - .cpu_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - cpu_widget_state.current_display_time = self.app_config_fields.default_time_value; - self.cpu_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - cpu_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - - fn reset_mem_zoom(&mut self) { - if let Some(mem_widget_state) = self - .mem_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - mem_widget_state.current_display_time = self.app_config_fields.default_time_value; - self.mem_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - mem_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - - fn reset_net_zoom(&mut self) { - if let Some(net_widget_state) = self - .net_state - .widget_states - .get_mut(&self.current_widget.widget_id) - { - net_widget_state.current_display_time = self.app_config_fields.default_time_value; - self.net_state.force_update = Some(self.current_widget.widget_id); - if self.app_config_fields.autohide_time { - net_widget_state.autohide_timer = Some(Instant::now()); - } - } - } - - fn reset_zoom(&mut self) { - match self.current_widget.widget_type { - BottomWidgetType::Cpu => self.reset_cpu_zoom(), - BottomWidgetType::Mem => self.reset_mem_zoom(), - BottomWidgetType::Net => self.reset_net_zoom(), - _ => {} - } - } } diff --git a/src/lib.rs b/src/lib.rs index cf963426..f52745cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,21 +20,13 @@ use std::{ }; use crossterm::{ - event::{ - poll, read, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, - MouseEventKind, - }, + event::{poll, read, DisableMouseCapture, Event, KeyEvent, MouseEvent, MouseEventKind}, execute, style::Print, terminal::{disable_raw_mode, LeaveAlternateScreen}, }; -use app::{ - data_harvester::{self}, - event::EventResult, - layout_manager::WidgetDirection, - AppState, UsedWidgets, -}; +use app::{data_harvester, AppState, UsedWidgets}; use constants::*; use options::*; use utils::error; @@ -75,104 +67,6 @@ pub enum ThreadControlEvent { UpdateUpdateTime(u64), } -pub fn handle_key_event( - event: KeyEvent, app: &mut AppState, reset_sender: &std::sync::mpsc::Sender, -) -> EventResult { - // debug!("KeyEvent: {:?}", event); - - // TODO: [PASTE] Note that this does NOT support some emojis like flags. This is due to us - // catching PER CHARACTER right now WITH A forced throttle! This means multi-char will not work. - // We can solve this (when we do paste probably) while keeping the throttle (mainly meant for movement) - // by throttling after *bulk+singular* actions, not just singular ones. - - if event.modifiers.is_empty() { - // Required catch for searching - otherwise you couldn't search with q. - if event.code == KeyCode::Char('q') && !app.is_in_search_widget() { - return EventResult::Quit; - } - match event.code { - KeyCode::End => app.skip_to_last(), - KeyCode::Home => app.skip_to_first(), - KeyCode::Up => app.on_up_key(), - KeyCode::Down => app.on_down_key(), - KeyCode::Left => app.on_left_key(), - KeyCode::Right => app.on_right_key(), - KeyCode::Char(caught_char) => app.on_char_key(caught_char), - // KeyCode::Esc => app.on_esc(), - KeyCode::Enter => app.on_enter(), - KeyCode::Tab => app.on_tab(), - KeyCode::Backspace => app.on_backspace(), - KeyCode::Delete => app.on_delete(), - KeyCode::F(1) => app.toggle_ignore_case(), - KeyCode::F(2) => app.toggle_search_whole_word(), - KeyCode::F(3) => app.toggle_search_regex(), - KeyCode::F(5) => app.toggle_tree_mode(), - KeyCode::F(6) => app.toggle_sort(), - KeyCode::F(9) => app.start_killing_process(), - _ => { - return EventResult::NoRedraw; - } - } - } else { - // Otherwise, track the modifier as well... - if let KeyModifiers::ALT = event.modifiers { - match event.code { - KeyCode::Char('c') | KeyCode::Char('C') => app.toggle_ignore_case(), - KeyCode::Char('w') | KeyCode::Char('W') => app.toggle_search_whole_word(), - KeyCode::Char('r') | KeyCode::Char('R') => app.toggle_search_regex(), - KeyCode::Char('h') => app.on_left_key(), - KeyCode::Char('l') => app.on_right_key(), - _ => {} - } - } else if let KeyModifiers::CONTROL = event.modifiers { - if event.code == KeyCode::Char('c') { - return EventResult::Quit; - } - - match event.code { - KeyCode::Char('f') => app.on_slash(), - KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left), - KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right), - KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up), - KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down), - KeyCode::Char('r') => { - if reset_sender.send(ThreadControlEvent::Reset).is_ok() { - app.reset(); - } - } - KeyCode::Char('a') => app.skip_cursor_beginning(), - KeyCode::Char('e') => app.skip_cursor_end(), - KeyCode::Char('u') => app.clear_search(), - KeyCode::Char('w') => app.clear_previous_word(), - KeyCode::Char('h') => app.on_backspace(), - // KeyCode::Char('j') => {}, // Move down - // KeyCode::Char('k') => {}, // Move up - // KeyCode::Char('h') => {}, // Move right - // KeyCode::Char('l') => {}, // Move left - // Can't do now, CTRL+BACKSPACE doesn't work and graphemes - // are hard to iter while truncating last (eloquently). - // KeyCode::Backspace => app.skip_word_backspace(), - _ => { - return EventResult::NoRedraw; - } - } - } else if let KeyModifiers::SHIFT = event.modifiers { - match event.code { - KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left), - KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right), - KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up), - KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down), - KeyCode::Char(caught_char) => app.on_char_key(caught_char), - _ => { - return EventResult::NoRedraw; - } - } - } - } - - EventResult::Redraw -} - pub fn read_config(config_location: Option<&str>) -> error::Result> { let config_path = if let Some(conf_loc) = config_location { Some(PathBuf::from(conf_loc))