Some minor refactoring; added a quick skip for invalid or blank searches
This commit is contained in:
parent
3224adf047
commit
51761400ce
74
src/app.rs
74
src/app.rs
|
@ -63,59 +63,45 @@ impl Default for AppScrollState {
|
||||||
/// AppSearchState only deals with the search's current settings and state.
|
/// AppSearchState only deals with the search's current settings and state.
|
||||||
pub struct AppSearchState {
|
pub struct AppSearchState {
|
||||||
current_search_query: String,
|
current_search_query: String,
|
||||||
searching_pid: bool,
|
pub is_searching_with_pid: bool,
|
||||||
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,
|
||||||
match_word: bool,
|
pub is_invalid_or_blank_search: bool,
|
||||||
use_regex: bool,
|
pub is_ignoring_case: bool,
|
||||||
|
pub is_searching_whole_word: bool,
|
||||||
|
pub is_searching_with_regex: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppSearchState {
|
impl Default for AppSearchState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
AppSearchState {
|
AppSearchState {
|
||||||
current_search_query: String::default(),
|
current_search_query: String::default(),
|
||||||
searching_pid: false,
|
is_searching_with_pid: false,
|
||||||
ignore_case: true,
|
is_ignoring_case: true,
|
||||||
current_regex: BASE_REGEX.clone(),
|
current_regex: BASE_REGEX.clone(),
|
||||||
|
is_invalid_or_blank_search: true,
|
||||||
current_cursor_position: 0,
|
current_cursor_position: 0,
|
||||||
match_word: false,
|
is_searching_whole_word: false,
|
||||||
use_regex: false,
|
is_searching_with_regex: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppSearchState {
|
impl AppSearchState {
|
||||||
pub fn toggle_ignore_case(&mut self) {
|
pub fn toggle_ignore_case(&mut self) {
|
||||||
self.ignore_case = !self.ignore_case;
|
self.is_ignoring_case = !self.is_ignoring_case;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_search_whole_word(&mut self) {
|
pub fn toggle_search_whole_word(&mut self) {
|
||||||
self.match_word = !self.match_word;
|
self.is_searching_whole_word = !self.is_searching_whole_word;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_search_regex(&mut self) {
|
pub fn toggle_search_regex(&mut self) {
|
||||||
self.use_regex = !self.use_regex;
|
self.is_searching_with_regex = !self.is_searching_with_regex;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_search_with_pid(&mut self) {
|
pub fn toggle_search_with_pid(&mut self) {
|
||||||
self.searching_pid = !self.searching_pid;
|
self.is_searching_with_pid = !self.is_searching_with_pid;
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_ignoring_case(&self) -> bool {
|
|
||||||
self.ignore_case
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_searching_whole_word(&self) -> bool {
|
|
||||||
self.match_word
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_searching_with_regex(&self) -> bool {
|
|
||||||
self.use_regex
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_searching_with_pid(&self) -> bool {
|
|
||||||
self.searching_pid
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +157,7 @@ pub struct App {
|
||||||
last_key_press: Instant,
|
last_key_press: Instant,
|
||||||
pub canvas_data: canvas::DisplayableData,
|
pub canvas_data: canvas::DisplayableData,
|
||||||
enable_grouping: bool,
|
enable_grouping: bool,
|
||||||
enable_searching: bool,
|
enable_process_searching: bool,
|
||||||
pub data_collection: DataCollection,
|
pub data_collection: DataCollection,
|
||||||
pub search_state: AppSearchState,
|
pub search_state: AppSearchState,
|
||||||
pub delete_dialog_state: AppDeleteDialogState,
|
pub delete_dialog_state: AppDeleteDialogState,
|
||||||
|
@ -201,7 +187,7 @@ impl App {
|
||||||
last_key_press: Instant::now(),
|
last_key_press: Instant::now(),
|
||||||
canvas_data: canvas::DisplayableData::default(),
|
canvas_data: canvas::DisplayableData::default(),
|
||||||
enable_grouping: false,
|
enable_grouping: false,
|
||||||
enable_searching: false,
|
enable_process_searching: false,
|
||||||
data_collection: DataCollection::default(),
|
data_collection: DataCollection::default(),
|
||||||
search_state: AppSearchState::default(),
|
search_state: AppSearchState::default(),
|
||||||
delete_dialog_state: AppDeleteDialogState::default(),
|
delete_dialog_state: AppDeleteDialogState::default(),
|
||||||
|
@ -222,12 +208,12 @@ impl App {
|
||||||
self.reset_multi_tap_keys();
|
self.reset_multi_tap_keys();
|
||||||
self.help_dialog_state.is_showing_help = false;
|
self.help_dialog_state.is_showing_help = false;
|
||||||
self.delete_dialog_state.is_showing_dd = false;
|
self.delete_dialog_state.is_showing_dd = false;
|
||||||
if self.enable_searching {
|
if self.enable_process_searching {
|
||||||
self.current_widget_selected = WidgetPosition::Process;
|
self.current_widget_selected = WidgetPosition::Process;
|
||||||
self.enable_searching = false;
|
self.enable_process_searching = false;
|
||||||
}
|
}
|
||||||
self.search_state.current_search_query = String::new();
|
self.search_state.current_search_query = String::new();
|
||||||
self.search_state.searching_pid = false;
|
self.search_state.is_searching_with_pid = false;
|
||||||
self.to_delete_process_list = None;
|
self.to_delete_process_list = None;
|
||||||
self.dd_err = None;
|
self.dd_err = None;
|
||||||
}
|
}
|
||||||
|
@ -241,9 +227,9 @@ impl App {
|
||||||
self.delete_dialog_state.is_on_yes = false;
|
self.delete_dialog_state.is_on_yes = false;
|
||||||
self.to_delete_process_list = None;
|
self.to_delete_process_list = None;
|
||||||
self.dd_err = None;
|
self.dd_err = None;
|
||||||
} else if self.enable_searching {
|
} else if self.enable_process_searching {
|
||||||
self.current_widget_selected = WidgetPosition::Process;
|
self.current_widget_selected = WidgetPosition::Process;
|
||||||
self.enable_searching = false;
|
self.enable_process_searching = false;
|
||||||
} else if self.is_expanded {
|
} else if self.is_expanded {
|
||||||
self.is_expanded = false;
|
self.is_expanded = false;
|
||||||
}
|
}
|
||||||
|
@ -273,7 +259,7 @@ impl App {
|
||||||
WidgetPosition::Process => self.toggle_grouping(),
|
WidgetPosition::Process => self.toggle_grouping(),
|
||||||
WidgetPosition::Disk => {}
|
WidgetPosition::Disk => {}
|
||||||
WidgetPosition::ProcessSearch => {
|
WidgetPosition::ProcessSearch => {
|
||||||
if self.search_state.is_searching_with_pid() {
|
if self.search_state.is_searching_with_pid {
|
||||||
self.search_with_name();
|
self.search_with_name();
|
||||||
} else {
|
} else {
|
||||||
self.search_with_pid();
|
self.search_with_pid();
|
||||||
|
@ -292,7 +278,7 @@ impl App {
|
||||||
match self.current_widget_selected {
|
match self.current_widget_selected {
|
||||||
WidgetPosition::Process | WidgetPosition::ProcessSearch => {
|
WidgetPosition::Process | WidgetPosition::ProcessSearch => {
|
||||||
// Toggle on
|
// Toggle on
|
||||||
self.enable_searching = true;
|
self.enable_process_searching = true;
|
||||||
self.current_widget_selected = WidgetPosition::ProcessSearch;
|
self.current_widget_selected = WidgetPosition::ProcessSearch;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -301,7 +287,7 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_searching(&self) -> bool {
|
pub fn is_searching(&self) -> bool {
|
||||||
self.enable_searching
|
self.enable_process_searching
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_in_search_widget(&self) -> bool {
|
pub fn is_in_search_widget(&self) -> bool {
|
||||||
|
@ -315,7 +301,7 @@ impl App {
|
||||||
pub fn search_with_pid(&mut self) {
|
pub fn search_with_pid(&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.search_state.searching_pid = true;
|
self.search_state.is_searching_with_pid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,7 +309,7 @@ impl App {
|
||||||
pub fn search_with_name(&mut self) {
|
pub fn search_with_name(&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.search_state.searching_pid = false;
|
self.search_state.is_searching_with_pid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -344,23 +330,25 @@ impl App {
|
||||||
|
|
||||||
pub fn update_regex(&mut self) {
|
pub fn update_regex(&mut self) {
|
||||||
self.search_state.current_regex = if self.search_state.current_search_query.is_empty() {
|
self.search_state.current_regex = if self.search_state.current_search_query.is_empty() {
|
||||||
|
self.search_state.is_invalid_or_blank_search = true;
|
||||||
BASE_REGEX.clone()
|
BASE_REGEX.clone()
|
||||||
} else {
|
} else {
|
||||||
let mut final_regex_string = self.search_state.current_search_query.clone();
|
let mut final_regex_string = self.search_state.current_search_query.clone();
|
||||||
|
|
||||||
if !self.search_state.is_searching_with_regex() {
|
if !self.search_state.is_searching_with_regex {
|
||||||
final_regex_string = regex::escape(&final_regex_string);
|
final_regex_string = regex::escape(&final_regex_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.search_state.is_searching_whole_word() {
|
if self.search_state.is_searching_whole_word {
|
||||||
final_regex_string = format!("^{}$", final_regex_string);
|
final_regex_string = format!("^{}$", final_regex_string);
|
||||||
}
|
}
|
||||||
if self.search_state.is_ignoring_case() {
|
if self.search_state.is_ignoring_case {
|
||||||
final_regex_string = format!("(?i){}", final_regex_string);
|
final_regex_string = format!("(?i){}", final_regex_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
regex::Regex::new(&final_regex_string)
|
regex::Regex::new(&final_regex_string)
|
||||||
};
|
};
|
||||||
|
self.search_state.is_invalid_or_blank_search = self.search_state.current_regex.is_err();
|
||||||
self.app_scroll_positions
|
self.app_scroll_positions
|
||||||
.process_scroll_state
|
.process_scroll_state
|
||||||
.previous_scroll_position = 0;
|
.previous_scroll_position = 0;
|
||||||
|
|
|
@ -1144,7 +1144,7 @@ impl Painter {
|
||||||
)]
|
)]
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut search_text = vec![if app_state.search_state.is_searching_with_pid() {
|
let mut search_text = vec![if app_state.search_state.is_searching_with_pid {
|
||||||
Text::styled(
|
Text::styled(
|
||||||
"Search by PID (Tab for Name): ",
|
"Search by PID (Tab for Name): ",
|
||||||
self.colours.table_header_style,
|
self.colours.table_header_style,
|
||||||
|
@ -1164,21 +1164,21 @@ impl Painter {
|
||||||
|
|
||||||
let option_row = vec![
|
let option_row = vec![
|
||||||
Text::styled("Match Case (Alt+C)", self.colours.table_header_style),
|
Text::styled("Match Case (Alt+C)", self.colours.table_header_style),
|
||||||
if !app_state.search_state.is_ignoring_case() {
|
if !app_state.search_state.is_ignoring_case {
|
||||||
Text::styled("[*]", self.colours.table_header_style)
|
Text::styled("[*]", self.colours.table_header_style)
|
||||||
} else {
|
} else {
|
||||||
Text::styled("[ ]", self.colours.table_header_style)
|
Text::styled("[ ]", self.colours.table_header_style)
|
||||||
},
|
},
|
||||||
Text::styled(" ", self.colours.table_header_style),
|
Text::styled(" ", self.colours.table_header_style),
|
||||||
Text::styled("Match Whole Word (Alt+W)", self.colours.table_header_style),
|
Text::styled("Match Whole Word (Alt+W)", self.colours.table_header_style),
|
||||||
if app_state.search_state.is_searching_whole_word() {
|
if app_state.search_state.is_searching_whole_word {
|
||||||
Text::styled("[*]", self.colours.table_header_style)
|
Text::styled("[*]", self.colours.table_header_style)
|
||||||
} else {
|
} else {
|
||||||
Text::styled("[ ]", self.colours.table_header_style)
|
Text::styled("[ ]", self.colours.table_header_style)
|
||||||
},
|
},
|
||||||
Text::styled(" ", self.colours.table_header_style),
|
Text::styled(" ", self.colours.table_header_style),
|
||||||
Text::styled("Use Regex (Alt+R)", self.colours.table_header_style),
|
Text::styled("Use Regex (Alt+R)", self.colours.table_header_style),
|
||||||
if app_state.search_state.is_searching_with_regex() {
|
if app_state.search_state.is_searching_with_regex {
|
||||||
Text::styled("[*]", self.colours.table_header_style)
|
Text::styled("[*]", self.colours.table_header_style)
|
||||||
} else {
|
} else {
|
||||||
Text::styled("[ ]", self.colours.table_header_style)
|
Text::styled("[ ]", self.colours.table_header_style)
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -735,7 +735,9 @@ fn update_final_process_list(app: &mut app::App) {
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|process| {
|
.filter(|process| {
|
||||||
if let Ok(matcher) = app.get_current_regex_matcher() {
|
if app.search_state.is_invalid_or_blank_search {
|
||||||
|
true
|
||||||
|
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||||
matcher.is_match(&process.name)
|
matcher.is_match(&process.name)
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
|
@ -747,8 +749,10 @@ fn update_final_process_list(app: &mut app::App) {
|
||||||
.process_data
|
.process_data
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_pid, process)| {
|
.filter(|(_pid, process)| {
|
||||||
if let Ok(matcher) = app.get_current_regex_matcher() {
|
if app.search_state.is_invalid_or_blank_search {
|
||||||
if app.search_state.is_searching_with_pid() {
|
true
|
||||||
|
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||||
|
if app.search_state.is_searching_with_pid {
|
||||||
matcher.is_match(&process.pid.to_string())
|
matcher.is_match(&process.pid.to_string())
|
||||||
} else {
|
} else {
|
||||||
matcher.is_match(&process.name)
|
matcher.is_match(&process.name)
|
||||||
|
|
Loading…
Reference in New Issue