Fix process searching while scrolling... that was broken for a while, I guess

This commit is contained in:
ClementTsang 2020-02-02 00:24:00 -05:00
parent 0bf7f32473
commit 0ab4b7f7cc
3 changed files with 42 additions and 43 deletions

View file

@ -9,7 +9,7 @@ use crate::{canvas, constants, data_conversion::ConvertedProcessData, utils::err
mod process_killer; mod process_killer;
#[derive(Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum WidgetPosition { pub enum WidgetPosition {
Cpu, Cpu,
Mem, Mem,
@ -86,7 +86,7 @@ pub struct App {
enable_searching: bool, enable_searching: bool,
current_search_query: String, current_search_query: String,
searching_pid: bool, searching_pid: bool,
pub use_simple: bool, pub ignore_case: bool,
current_regex: std::result::Result<regex::Regex, regex::Error>, current_regex: std::result::Result<regex::Regex, regex::Error>,
current_cursor_position: usize, current_cursor_position: usize,
pub data_collection: DataCollection, pub data_collection: DataCollection,
@ -132,7 +132,7 @@ impl App {
enable_searching: false, enable_searching: false,
current_search_query: String::default(), current_search_query: String::default(),
searching_pid: false, searching_pid: false,
use_simple: false, ignore_case: false,
current_regex: BASE_REGEX.clone(), //TODO: [OPT] seems like a thing we can switch to lifetimes to avoid cloning current_regex: BASE_REGEX.clone(), //TODO: [OPT] seems like a thing we can switch to lifetimes to avoid cloning
current_cursor_position: 0, current_cursor_position: 0,
data_collection: DataCollection::default(), data_collection: DataCollection::default(),
@ -246,26 +246,28 @@ impl App {
&self.current_search_query &self.current_search_query
} }
pub fn toggle_simple_search(&mut self) { pub fn toggle_ignore_case(&mut self) {
if !self.is_in_dialog() && self.is_searching() { if !self.is_in_dialog() && self.is_searching() {
if let WidgetPosition::ProcessSearch = self.current_widget_selected { if let WidgetPosition::ProcessSearch = self.current_widget_selected {
self.use_simple = !self.use_simple; self.ignore_case = !self.ignore_case;
self.update_regex();
// Update to latest (when simple is on this is not updated)
if !self.use_simple {
self.current_regex = if self.current_search_query.is_empty() {
BASE_REGEX.clone()
} else {
regex::Regex::new(&(self.current_search_query))
};
}
// Force update to process display in GUI
self.update_process_gui = true; self.update_process_gui = true;
} }
} }
} }
fn update_regex(&mut self) {
self.current_regex = if self.current_search_query.is_empty() {
BASE_REGEX.clone()
} else if self.ignore_case {
regex::Regex::new(&(format!("(?i){}", self.current_search_query)))
} else {
regex::Regex::new(&(self.current_search_query))
};
self.previous_process_position = 0;
self.currently_selected_process_position = 0;
}
pub fn get_cursor_position(&self) -> usize { pub fn get_cursor_position(&self) -> usize {
self.current_cursor_position self.current_cursor_position
} }
@ -294,13 +296,7 @@ impl App {
self.current_search_query self.current_search_query
.remove(self.current_cursor_position); .remove(self.current_cursor_position);
if !self.use_simple { self.update_regex();
self.current_regex = if self.current_search_query.is_empty() {
BASE_REGEX.clone()
} else {
regex::Regex::new(&(self.current_search_query))
};
}
self.update_process_gui = true; self.update_process_gui = true;
} }
} }
@ -381,13 +377,8 @@ impl App {
.insert(self.current_cursor_position, caught_char); .insert(self.current_cursor_position, caught_char);
self.current_cursor_position += 1; self.current_cursor_position += 1;
if !self.use_simple { self.update_regex();
self.current_regex = if self.current_search_query.is_empty() {
BASE_REGEX.clone()
} else {
regex::Regex::new(&(self.current_search_query))
};
}
self.update_process_gui = true; self.update_process_gui = true;
} else { } else {
match caught_char { match caught_char {
@ -401,7 +392,7 @@ impl App {
self.second_char = ' '; self.second_char = ' ';
let current_process = Vec::new(); let current_process = Vec::new();
// TODO: FIX THIS SHITTTTTT // TODO: Fix
self.to_delete_process_list = Some(current_process); self.to_delete_process_list = Some(current_process);
self.show_dd = true; self.show_dd = true;
@ -607,14 +598,15 @@ impl App {
match self.current_widget_selected { match self.current_widget_selected {
WidgetPosition::Process => { WidgetPosition::Process => {
self.currently_selected_process_position = self.currently_selected_process_position =
self.data.list_of_processes.len() as i64 - 1 self.canvas_data.finalized_process_data.len() as i64 - 1
} }
WidgetPosition::Temp => { WidgetPosition::Temp => {
self.currently_selected_temperature_position = self.currently_selected_temperature_position =
self.data.temperature_sensors.len() as i64 - 1 self.canvas_data.temp_sensor_data.len() as i64 - 1
} }
WidgetPosition::Disk => { WidgetPosition::Disk => {
self.currently_selected_disk_position = self.data.disks.len() as i64 - 1 self.currently_selected_disk_position =
self.canvas_data.disk_data.len() as i64 - 1
} }
WidgetPosition::Cpu => { WidgetPosition::Cpu => {
self.currently_selected_cpu_table_position = self.currently_selected_cpu_table_position =
@ -667,7 +659,7 @@ impl App {
fn change_process_position(&mut self, num_to_change_by: i64) { fn change_process_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_process_position + num_to_change_by >= 0 if self.currently_selected_process_position + num_to_change_by >= 0
&& self.currently_selected_process_position + num_to_change_by && self.currently_selected_process_position + num_to_change_by
< self.data.list_of_processes.len() as i64 < self.canvas_data.finalized_process_data.len() as i64
{ {
self.currently_selected_process_position += num_to_change_by; self.currently_selected_process_position += num_to_change_by;
} }
@ -676,7 +668,7 @@ impl App {
fn change_temp_position(&mut self, num_to_change_by: i64) { fn change_temp_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_temperature_position + num_to_change_by >= 0 if self.currently_selected_temperature_position + num_to_change_by >= 0
&& self.currently_selected_temperature_position + num_to_change_by && self.currently_selected_temperature_position + num_to_change_by
< self.data.temperature_sensors.len() as i64 < self.canvas_data.temp_sensor_data.len() as i64
{ {
self.currently_selected_temperature_position += num_to_change_by; self.currently_selected_temperature_position += num_to_change_by;
} }
@ -685,7 +677,7 @@ impl App {
fn change_disk_position(&mut self, num_to_change_by: i64) { fn change_disk_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_disk_position + num_to_change_by >= 0 if self.currently_selected_disk_position + num_to_change_by >= 0
&& self.currently_selected_disk_position + num_to_change_by && self.currently_selected_disk_position + num_to_change_by
< self.data.disks.len() as i64 < self.canvas_data.disk_data.len() as i64
{ {
self.currently_selected_disk_position += num_to_change_by; self.currently_selected_disk_position += num_to_change_by;
} }

View file

@ -912,10 +912,10 @@ fn draw_search_field<B: backend::Backend>(
} else { } else {
Text::styled("\nName", Style::default().fg(TABLE_HEADER_COLOUR)) Text::styled("\nName", Style::default().fg(TABLE_HEADER_COLOUR))
}, },
if app_state.use_simple { if app_state.ignore_case {
Text::styled(" (Simple): ", Style::default().fg(TABLE_HEADER_COLOUR)) Text::styled(" (Ignore Case): ", Style::default().fg(TABLE_HEADER_COLOUR))
} else { } else {
Text::styled(" (Regex): ", Style::default().fg(TABLE_HEADER_COLOUR)) Text::styled(": ", Style::default().fg(TABLE_HEADER_COLOUR))
}, },
]; ];
@ -957,13 +957,20 @@ fn draw_processes_table<B: backend::Backend>(
// do so by hiding some elements! // do so by hiding some elements!
let num_rows = i64::from(draw_loc.height) - 5; let num_rows = i64::from(draw_loc.height) - 5;
let start_position = get_start_position( let position = get_start_position(
num_rows, num_rows,
&(app_state.scroll_direction), &(app_state.scroll_direction),
&mut app_state.previous_process_position, &mut app_state.previous_process_position,
app_state.currently_selected_process_position, app_state.currently_selected_process_position,
); );
// Sanity check
let start_position = if position >= process_data.len() as i64 {
std::cmp::max(0, process_data.len() as i64 - 1)
} else {
position
};
let sliced_vec: Vec<ConvertedProcessData> = (&process_data[start_position as usize..]).to_vec(); let sliced_vec: Vec<ConvertedProcessData> = (&process_data[start_position as usize..]).to_vec();
let mut process_counter = 0; let mut process_counter = 0;

View file

@ -131,7 +131,7 @@ fn main() -> error::Result<()> {
// Set default search method // Set default search method
if matches.is_present("CASE_INSENSITIVE_DEFAULT") { if matches.is_present("CASE_INSENSITIVE_DEFAULT") {
app.use_simple = true; app.ignore_case = true;
} }
// Set up up tui and crossterm // Set up up tui and crossterm
@ -256,7 +256,7 @@ fn main() -> error::Result<()> {
} }
} }
// TODO: [SEARCH] Rename "simple" search to just... search without cases... // TODO: [SEARCH] Rename "simple" search to just... search without cases...
KeyCode::Char('s') => app.toggle_simple_search(), KeyCode::Char('s') => app.toggle_ignore_case(),
KeyCode::Char('a') => app.skip_cursor_beginning(), KeyCode::Char('a') => app.skip_cursor_beginning(),
KeyCode::Char('e') => app.skip_cursor_end(), KeyCode::Char('e') => app.skip_cursor_end(),
_ => {} _ => {}