More minor refactoring to generalize the app search state struct.
This commit is contained in:
parent
4dc5a3f6d4
commit
882e5d9289
178
src/app.rs
178
src/app.rs
|
@ -60,14 +60,31 @@ impl Default for AppScrollState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// AppSearchState only deals with the search's current settings and state.
|
/// AppSearchState deals with generic searching (mainly for graph filtering)
|
||||||
pub struct ProcessSearchState {
|
pub struct AppSearchState {
|
||||||
is_enabled: bool,
|
is_enabled: bool,
|
||||||
current_search_query: String,
|
current_search_query: String,
|
||||||
pub is_searching_with_pid: 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 is_invalid_or_blank_search: bool,
|
pub is_invalid_or_blank_search: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AppSearchState {
|
||||||
|
fn default() -> Self {
|
||||||
|
AppSearchState {
|
||||||
|
is_enabled: false,
|
||||||
|
current_search_query: String::default(),
|
||||||
|
current_regex: BASE_REGEX.clone(),
|
||||||
|
current_cursor_position: 0,
|
||||||
|
is_invalid_or_blank_search: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ProcessSearchState only deals with process' search's current settings and state.
|
||||||
|
pub struct ProcessSearchState {
|
||||||
|
pub search_state: AppSearchState,
|
||||||
|
pub is_searching_with_pid: bool,
|
||||||
pub is_ignoring_case: bool,
|
pub is_ignoring_case: bool,
|
||||||
pub is_searching_whole_word: bool,
|
pub is_searching_whole_word: bool,
|
||||||
pub is_searching_with_regex: bool,
|
pub is_searching_with_regex: bool,
|
||||||
|
@ -76,13 +93,9 @@ pub struct ProcessSearchState {
|
||||||
impl Default for ProcessSearchState {
|
impl Default for ProcessSearchState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
ProcessSearchState {
|
ProcessSearchState {
|
||||||
is_enabled: false,
|
search_state: AppSearchState::default(),
|
||||||
current_search_query: String::default(),
|
|
||||||
is_searching_with_pid: false,
|
is_searching_with_pid: false,
|
||||||
is_ignoring_case: true,
|
is_ignoring_case: true,
|
||||||
current_regex: BASE_REGEX.clone(),
|
|
||||||
is_invalid_or_blank_search: true,
|
|
||||||
current_cursor_position: 0,
|
|
||||||
is_searching_whole_word: false,
|
is_searching_whole_word: false,
|
||||||
is_searching_with_regex: false,
|
is_searching_with_regex: false,
|
||||||
}
|
}
|
||||||
|
@ -208,11 +221,11 @@ 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.process_search_state.is_enabled {
|
if self.process_search_state.search_state.is_enabled {
|
||||||
self.current_widget_selected = WidgetPosition::Process;
|
self.current_widget_selected = WidgetPosition::Process;
|
||||||
self.process_search_state.is_enabled = false;
|
self.process_search_state.search_state.is_enabled = false;
|
||||||
}
|
}
|
||||||
self.process_search_state.current_search_query = String::new();
|
self.process_search_state.search_state.current_search_query = String::new();
|
||||||
self.process_search_state.is_searching_with_pid = false;
|
self.process_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;
|
||||||
|
@ -227,9 +240,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.process_search_state.is_enabled {
|
} else if self.process_search_state.search_state.is_enabled {
|
||||||
self.current_widget_selected = WidgetPosition::Process;
|
self.current_widget_selected = WidgetPosition::Process;
|
||||||
self.process_search_state.is_enabled = false;
|
self.process_search_state.search_state.is_enabled = false;
|
||||||
} else if self.is_expanded {
|
} else if self.is_expanded {
|
||||||
self.is_expanded = false;
|
self.is_expanded = false;
|
||||||
}
|
}
|
||||||
|
@ -278,7 +291,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.process_search_state.is_enabled = true;
|
self.process_search_state.search_state.is_enabled = true;
|
||||||
self.current_widget_selected = WidgetPosition::ProcessSearch;
|
self.current_widget_selected = WidgetPosition::ProcessSearch;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -287,7 +300,7 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_searching(&self) -> bool {
|
pub fn is_searching(&self) -> bool {
|
||||||
self.process_search_state.is_enabled
|
self.process_search_state.search_state.is_enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_in_search_widget(&self) -> bool {
|
pub fn is_in_search_widget(&self) -> bool {
|
||||||
|
@ -315,7 +328,7 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_current_search_query(&self) -> &String {
|
pub fn get_current_search_query(&self) -> &String {
|
||||||
&self.process_search_state.current_search_query
|
&self.process_search_state.search_state.current_search_query
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_ignore_case(&mut self) {
|
pub fn toggle_ignore_case(&mut self) {
|
||||||
|
@ -329,28 +342,43 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_regex(&mut self) {
|
pub fn update_regex(&mut self) {
|
||||||
self.process_search_state.current_regex =
|
self.process_search_state.search_state.current_regex = if self
|
||||||
if self.process_search_state.current_search_query.is_empty() {
|
.process_search_state
|
||||||
self.process_search_state.is_invalid_or_blank_search = true;
|
.search_state
|
||||||
BASE_REGEX.clone()
|
.current_search_query
|
||||||
} else {
|
.is_empty()
|
||||||
let mut final_regex_string = self.process_search_state.current_search_query.clone();
|
{
|
||||||
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.is_invalid_or_blank_search = true;
|
||||||
|
BASE_REGEX.clone()
|
||||||
|
} else {
|
||||||
|
let mut final_regex_string = self
|
||||||
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_search_query
|
||||||
|
.clone();
|
||||||
|
|
||||||
if !self.process_search_state.is_searching_with_regex {
|
if !self.process_search_state.is_searching_with_regex {
|
||||||
final_regex_string = regex::escape(&final_regex_string);
|
final_regex_string = regex::escape(&final_regex_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.process_search_state.is_searching_whole_word {
|
if self.process_search_state.is_searching_whole_word {
|
||||||
final_regex_string = format!("^{}$", final_regex_string);
|
final_regex_string = format!("^{}$", final_regex_string);
|
||||||
}
|
}
|
||||||
if self.process_search_state.is_ignoring_case {
|
if self.process_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.process_search_state.is_invalid_or_blank_search =
|
self.process_search_state
|
||||||
self.process_search_state.current_regex.is_err();
|
.search_state
|
||||||
|
.is_invalid_or_blank_search = self
|
||||||
|
.process_search_state
|
||||||
|
.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;
|
||||||
|
@ -360,7 +388,9 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_cursor_position(&self) -> usize {
|
pub fn get_cursor_position(&self) -> usize {
|
||||||
self.process_search_state.current_cursor_position
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position
|
||||||
}
|
}
|
||||||
|
|
||||||
/// One of two functions allowed to run while in a dialog...
|
/// One of two functions allowed to run while in a dialog...
|
||||||
|
@ -391,11 +421,23 @@ impl App {
|
||||||
|
|
||||||
pub fn on_backspace(&mut self) {
|
pub fn on_backspace(&mut self) {
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
if self.process_search_state.current_cursor_position > 0 {
|
if self
|
||||||
self.process_search_state.current_cursor_position -= 1;
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position
|
||||||
|
> 0
|
||||||
|
{
|
||||||
self.process_search_state
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position -= 1;
|
||||||
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
.current_search_query
|
.current_search_query
|
||||||
.remove(self.process_search_state.current_cursor_position);
|
.remove(
|
||||||
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position,
|
||||||
|
);
|
||||||
|
|
||||||
self.update_regex();
|
self.update_regex();
|
||||||
self.update_process_gui = true;
|
self.update_process_gui = true;
|
||||||
|
@ -404,7 +446,7 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_current_regex_matcher(&self) -> &std::result::Result<regex::Regex, regex::Error> {
|
pub fn get_current_regex_matcher(&self) -> &std::result::Result<regex::Regex, regex::Error> {
|
||||||
&self.process_search_state.current_regex
|
&self.process_search_state.search_state.current_regex
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_up_key(&mut self) {
|
pub fn on_up_key(&mut self) {
|
||||||
|
@ -428,8 +470,15 @@ impl App {
|
||||||
pub fn on_left_key(&mut self) {
|
pub fn on_left_key(&mut self) {
|
||||||
if !self.is_in_dialog() {
|
if !self.is_in_dialog() {
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
if self.process_search_state.current_cursor_position > 0 {
|
if self
|
||||||
self.process_search_state.current_cursor_position -= 1;
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position
|
||||||
|
> 0
|
||||||
|
{
|
||||||
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if self.delete_dialog_state.is_showing_dd && !self.delete_dialog_state.is_on_yes {
|
} else if self.delete_dialog_state.is_showing_dd && !self.delete_dialog_state.is_on_yes {
|
||||||
|
@ -440,10 +489,19 @@ impl App {
|
||||||
pub fn on_right_key(&mut self) {
|
pub fn on_right_key(&mut self) {
|
||||||
if !self.is_in_dialog() {
|
if !self.is_in_dialog() {
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
if self.process_search_state.current_cursor_position
|
if self
|
||||||
< self.process_search_state.current_search_query.len()
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position
|
||||||
|
< self
|
||||||
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_search_query
|
||||||
|
.len()
|
||||||
{
|
{
|
||||||
self.process_search_state.current_cursor_position += 1;
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if self.delete_dialog_state.is_showing_dd && self.delete_dialog_state.is_on_yes {
|
} else if self.delete_dialog_state.is_showing_dd && self.delete_dialog_state.is_on_yes {
|
||||||
|
@ -454,7 +512,9 @@ impl App {
|
||||||
pub fn skip_cursor_beginning(&mut self) {
|
pub fn skip_cursor_beginning(&mut self) {
|
||||||
if !self.is_in_dialog() {
|
if !self.is_in_dialog() {
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
self.process_search_state.current_cursor_position = 0;
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -462,8 +522,13 @@ impl App {
|
||||||
pub fn skip_cursor_end(&mut self) {
|
pub fn skip_cursor_end(&mut self) {
|
||||||
if !self.is_in_dialog() {
|
if !self.is_in_dialog() {
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
self.process_search_state.current_cursor_position =
|
self.process_search_state
|
||||||
self.process_search_state.current_search_query.len();
|
.search_state
|
||||||
|
.current_cursor_position = self
|
||||||
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_search_query
|
||||||
|
.len();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -481,11 +546,18 @@ impl App {
|
||||||
self.last_key_press = current_key_press_inst;
|
self.last_key_press = current_key_press_inst;
|
||||||
|
|
||||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||||
self.process_search_state.current_search_query.insert(
|
self.process_search_state
|
||||||
self.process_search_state.current_cursor_position,
|
.search_state
|
||||||
caught_char,
|
.current_search_query
|
||||||
);
|
.insert(
|
||||||
self.process_search_state.current_cursor_position += 1;
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position,
|
||||||
|
caught_char,
|
||||||
|
);
|
||||||
|
self.process_search_state
|
||||||
|
.search_state
|
||||||
|
.current_cursor_position += 1;
|
||||||
|
|
||||||
self.update_regex();
|
self.update_regex();
|
||||||
|
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -735,7 +735,11 @@ fn update_final_process_list(app: &mut app::App) {
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|process| {
|
.filter(|process| {
|
||||||
if app.process_search_state.is_invalid_or_blank_search {
|
if app
|
||||||
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.is_invalid_or_blank_search
|
||||||
|
{
|
||||||
true
|
true
|
||||||
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||||
matcher.is_match(&process.name)
|
matcher.is_match(&process.name)
|
||||||
|
@ -749,7 +753,11 @@ fn update_final_process_list(app: &mut app::App) {
|
||||||
.process_data
|
.process_data
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_pid, process)| {
|
.filter(|(_pid, process)| {
|
||||||
if app.process_search_state.is_invalid_or_blank_search {
|
if app
|
||||||
|
.process_search_state
|
||||||
|
.search_state
|
||||||
|
.is_invalid_or_blank_search
|
||||||
|
{
|
||||||
true
|
true
|
||||||
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||||
if app.process_search_state.is_searching_with_pid {
|
if app.process_search_state.is_searching_with_pid {
|
||||||
|
|
Loading…
Reference in New Issue