mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-23 12:43:09 +00:00
Minor refactoring again.
This commit is contained in:
parent
51761400ce
commit
4dc5a3f6d4
3 changed files with 75 additions and 72 deletions
95
src/app.rs
95
src/app.rs
|
@ -61,7 +61,8 @@ impl Default for AppScrollState {
|
|||
}
|
||||
|
||||
/// AppSearchState only deals with the search's current settings and state.
|
||||
pub struct AppSearchState {
|
||||
pub struct ProcessSearchState {
|
||||
is_enabled: bool,
|
||||
current_search_query: String,
|
||||
pub is_searching_with_pid: bool,
|
||||
current_regex: std::result::Result<regex::Regex, regex::Error>,
|
||||
|
@ -72,9 +73,10 @@ pub struct AppSearchState {
|
|||
pub is_searching_with_regex: bool,
|
||||
}
|
||||
|
||||
impl Default for AppSearchState {
|
||||
impl Default for ProcessSearchState {
|
||||
fn default() -> Self {
|
||||
AppSearchState {
|
||||
ProcessSearchState {
|
||||
is_enabled: false,
|
||||
current_search_query: String::default(),
|
||||
is_searching_with_pid: false,
|
||||
is_ignoring_case: true,
|
||||
|
@ -87,7 +89,7 @@ impl Default for AppSearchState {
|
|||
}
|
||||
}
|
||||
|
||||
impl AppSearchState {
|
||||
impl ProcessSearchState {
|
||||
pub fn toggle_ignore_case(&mut self) {
|
||||
self.is_ignoring_case = !self.is_ignoring_case;
|
||||
}
|
||||
|
@ -157,9 +159,8 @@ pub struct App {
|
|||
last_key_press: Instant,
|
||||
pub canvas_data: canvas::DisplayableData,
|
||||
enable_grouping: bool,
|
||||
enable_process_searching: bool,
|
||||
pub data_collection: DataCollection,
|
||||
pub search_state: AppSearchState,
|
||||
pub process_search_state: ProcessSearchState,
|
||||
pub delete_dialog_state: AppDeleteDialogState,
|
||||
pub help_dialog_state: AppHelpDialogState,
|
||||
pub app_config_fields: AppConfigFields,
|
||||
|
@ -187,9 +188,8 @@ impl App {
|
|||
last_key_press: Instant::now(),
|
||||
canvas_data: canvas::DisplayableData::default(),
|
||||
enable_grouping: false,
|
||||
enable_process_searching: false,
|
||||
data_collection: DataCollection::default(),
|
||||
search_state: AppSearchState::default(),
|
||||
process_search_state: ProcessSearchState::default(),
|
||||
delete_dialog_state: AppDeleteDialogState::default(),
|
||||
help_dialog_state: AppHelpDialogState::default(),
|
||||
app_config_fields: AppConfigFields {
|
||||
|
@ -208,12 +208,12 @@ impl App {
|
|||
self.reset_multi_tap_keys();
|
||||
self.help_dialog_state.is_showing_help = false;
|
||||
self.delete_dialog_state.is_showing_dd = false;
|
||||
if self.enable_process_searching {
|
||||
if self.process_search_state.is_enabled {
|
||||
self.current_widget_selected = WidgetPosition::Process;
|
||||
self.enable_process_searching = false;
|
||||
self.process_search_state.is_enabled = false;
|
||||
}
|
||||
self.search_state.current_search_query = String::new();
|
||||
self.search_state.is_searching_with_pid = false;
|
||||
self.process_search_state.current_search_query = String::new();
|
||||
self.process_search_state.is_searching_with_pid = false;
|
||||
self.to_delete_process_list = None;
|
||||
self.dd_err = None;
|
||||
}
|
||||
|
@ -227,9 +227,9 @@ impl App {
|
|||
self.delete_dialog_state.is_on_yes = false;
|
||||
self.to_delete_process_list = None;
|
||||
self.dd_err = None;
|
||||
} else if self.enable_process_searching {
|
||||
} else if self.process_search_state.is_enabled {
|
||||
self.current_widget_selected = WidgetPosition::Process;
|
||||
self.enable_process_searching = false;
|
||||
self.process_search_state.is_enabled = false;
|
||||
} else if self.is_expanded {
|
||||
self.is_expanded = false;
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ impl App {
|
|||
WidgetPosition::Process => self.toggle_grouping(),
|
||||
WidgetPosition::Disk => {}
|
||||
WidgetPosition::ProcessSearch => {
|
||||
if self.search_state.is_searching_with_pid {
|
||||
if self.process_search_state.is_searching_with_pid {
|
||||
self.search_with_name();
|
||||
} else {
|
||||
self.search_with_pid();
|
||||
|
@ -278,7 +278,7 @@ impl App {
|
|||
match self.current_widget_selected {
|
||||
WidgetPosition::Process | WidgetPosition::ProcessSearch => {
|
||||
// Toggle on
|
||||
self.enable_process_searching = true;
|
||||
self.process_search_state.is_enabled = true;
|
||||
self.current_widget_selected = WidgetPosition::ProcessSearch;
|
||||
}
|
||||
_ => {}
|
||||
|
@ -287,7 +287,7 @@ impl App {
|
|||
}
|
||||
|
||||
pub fn is_searching(&self) -> bool {
|
||||
self.enable_process_searching
|
||||
self.process_search_state.is_enabled
|
||||
}
|
||||
|
||||
pub fn is_in_search_widget(&self) -> bool {
|
||||
|
@ -301,7 +301,7 @@ impl App {
|
|||
pub fn search_with_pid(&mut self) {
|
||||
if !self.is_in_dialog() && self.is_searching() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state.is_searching_with_pid = true;
|
||||
self.process_search_state.is_searching_with_pid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -309,19 +309,19 @@ impl App {
|
|||
pub fn search_with_name(&mut self) {
|
||||
if !self.is_in_dialog() && self.is_searching() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state.is_searching_with_pid = false;
|
||||
self.process_search_state.is_searching_with_pid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_search_query(&self) -> &String {
|
||||
&self.search_state.current_search_query
|
||||
&self.process_search_state.current_search_query
|
||||
}
|
||||
|
||||
pub fn toggle_ignore_case(&mut self) {
|
||||
if !self.is_in_dialog() && self.is_searching() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state.toggle_ignore_case();
|
||||
self.process_search_state.toggle_ignore_case();
|
||||
self.update_regex();
|
||||
self.update_process_gui = true;
|
||||
}
|
||||
|
@ -329,26 +329,28 @@ impl App {
|
|||
}
|
||||
|
||||
pub fn update_regex(&mut self) {
|
||||
self.search_state.current_regex = if self.search_state.current_search_query.is_empty() {
|
||||
self.search_state.is_invalid_or_blank_search = true;
|
||||
self.process_search_state.current_regex =
|
||||
if self.process_search_state.current_search_query.is_empty() {
|
||||
self.process_search_state.is_invalid_or_blank_search = true;
|
||||
BASE_REGEX.clone()
|
||||
} else {
|
||||
let mut final_regex_string = self.search_state.current_search_query.clone();
|
||||
let mut final_regex_string = self.process_search_state.current_search_query.clone();
|
||||
|
||||
if !self.search_state.is_searching_with_regex {
|
||||
if !self.process_search_state.is_searching_with_regex {
|
||||
final_regex_string = regex::escape(&final_regex_string);
|
||||
}
|
||||
|
||||
if self.search_state.is_searching_whole_word {
|
||||
if self.process_search_state.is_searching_whole_word {
|
||||
final_regex_string = format!("^{}$", final_regex_string);
|
||||
}
|
||||
if self.search_state.is_ignoring_case {
|
||||
if self.process_search_state.is_ignoring_case {
|
||||
final_regex_string = format!("(?i){}", 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.process_search_state.is_invalid_or_blank_search =
|
||||
self.process_search_state.current_regex.is_err();
|
||||
self.app_scroll_positions
|
||||
.process_scroll_state
|
||||
.previous_scroll_position = 0;
|
||||
|
@ -358,7 +360,7 @@ impl App {
|
|||
}
|
||||
|
||||
pub fn get_cursor_position(&self) -> usize {
|
||||
self.search_state.current_cursor_position
|
||||
self.process_search_state.current_cursor_position
|
||||
}
|
||||
|
||||
/// One of two functions allowed to run while in a dialog...
|
||||
|
@ -389,11 +391,11 @@ impl App {
|
|||
|
||||
pub fn on_backspace(&mut self) {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
if self.search_state.current_cursor_position > 0 {
|
||||
self.search_state.current_cursor_position -= 1;
|
||||
self.search_state
|
||||
if self.process_search_state.current_cursor_position > 0 {
|
||||
self.process_search_state.current_cursor_position -= 1;
|
||||
self.process_search_state
|
||||
.current_search_query
|
||||
.remove(self.search_state.current_cursor_position);
|
||||
.remove(self.process_search_state.current_cursor_position);
|
||||
|
||||
self.update_regex();
|
||||
self.update_process_gui = true;
|
||||
|
@ -402,7 +404,7 @@ impl App {
|
|||
}
|
||||
|
||||
pub fn get_current_regex_matcher(&self) -> &std::result::Result<regex::Regex, regex::Error> {
|
||||
&self.search_state.current_regex
|
||||
&self.process_search_state.current_regex
|
||||
}
|
||||
|
||||
pub fn on_up_key(&mut self) {
|
||||
|
@ -426,8 +428,8 @@ impl App {
|
|||
pub fn on_left_key(&mut self) {
|
||||
if !self.is_in_dialog() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
if self.search_state.current_cursor_position > 0 {
|
||||
self.search_state.current_cursor_position -= 1;
|
||||
if self.process_search_state.current_cursor_position > 0 {
|
||||
self.process_search_state.current_cursor_position -= 1;
|
||||
}
|
||||
}
|
||||
} else if self.delete_dialog_state.is_showing_dd && !self.delete_dialog_state.is_on_yes {
|
||||
|
@ -438,10 +440,10 @@ impl App {
|
|||
pub fn on_right_key(&mut self) {
|
||||
if !self.is_in_dialog() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
if self.search_state.current_cursor_position
|
||||
< self.search_state.current_search_query.len()
|
||||
if self.process_search_state.current_cursor_position
|
||||
< self.process_search_state.current_search_query.len()
|
||||
{
|
||||
self.search_state.current_cursor_position += 1;
|
||||
self.process_search_state.current_cursor_position += 1;
|
||||
}
|
||||
}
|
||||
} else if self.delete_dialog_state.is_showing_dd && self.delete_dialog_state.is_on_yes {
|
||||
|
@ -452,7 +454,7 @@ impl App {
|
|||
pub fn skip_cursor_beginning(&mut self) {
|
||||
if !self.is_in_dialog() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state.current_cursor_position = 0;
|
||||
self.process_search_state.current_cursor_position = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -460,8 +462,8 @@ impl App {
|
|||
pub fn skip_cursor_end(&mut self) {
|
||||
if !self.is_in_dialog() {
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state.current_cursor_position =
|
||||
self.search_state.current_search_query.len();
|
||||
self.process_search_state.current_cursor_position =
|
||||
self.process_search_state.current_search_query.len();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -479,10 +481,11 @@ impl App {
|
|||
self.last_key_press = current_key_press_inst;
|
||||
|
||||
if let WidgetPosition::ProcessSearch = self.current_widget_selected {
|
||||
self.search_state
|
||||
.current_search_query
|
||||
.insert(self.search_state.current_cursor_position, caught_char);
|
||||
self.search_state.current_cursor_position += 1;
|
||||
self.process_search_state.current_search_query.insert(
|
||||
self.process_search_state.current_cursor_position,
|
||||
caught_char,
|
||||
);
|
||||
self.process_search_state.current_cursor_position += 1;
|
||||
|
||||
self.update_regex();
|
||||
|
||||
|
|
|
@ -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.process_search_state.is_searching_with_pid {
|
||||
Text::styled(
|
||||
"Search by PID (Tab for Name): ",
|
||||
self.colours.table_header_style,
|
||||
|
@ -1164,21 +1164,21 @@ impl Painter {
|
|||
|
||||
let option_row = vec![
|
||||
Text::styled("Match Case (Alt+C)", self.colours.table_header_style),
|
||||
if !app_state.search_state.is_ignoring_case {
|
||||
if !app_state.process_search_state.is_ignoring_case {
|
||||
Text::styled("[*]", self.colours.table_header_style)
|
||||
} else {
|
||||
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),
|
||||
if app_state.search_state.is_searching_whole_word {
|
||||
if app_state.process_search_state.is_searching_whole_word {
|
||||
Text::styled("[*]", self.colours.table_header_style)
|
||||
} else {
|
||||
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),
|
||||
if app_state.search_state.is_searching_with_regex {
|
||||
if app_state.process_search_state.is_searching_with_regex {
|
||||
Text::styled("[*]", self.colours.table_header_style)
|
||||
} else {
|
||||
Text::styled("[ ]", self.colours.table_header_style)
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -355,19 +355,19 @@ fn handle_key_event_or_break(
|
|||
match event.code {
|
||||
KeyCode::Char('c') => {
|
||||
if app.is_in_search_widget() {
|
||||
app.search_state.toggle_ignore_case();
|
||||
app.process_search_state.toggle_ignore_case();
|
||||
app.update_regex();
|
||||
}
|
||||
}
|
||||
KeyCode::Char('w') => {
|
||||
if app.is_in_search_widget() {
|
||||
app.search_state.toggle_search_whole_word();
|
||||
app.process_search_state.toggle_search_whole_word();
|
||||
app.update_regex();
|
||||
}
|
||||
}
|
||||
KeyCode::Char('r') => {
|
||||
if app.is_in_search_widget() {
|
||||
app.search_state.toggle_search_regex();
|
||||
app.process_search_state.toggle_search_regex();
|
||||
app.update_regex();
|
||||
}
|
||||
}
|
||||
|
@ -526,11 +526,11 @@ fn enable_app_case_sensitive(
|
|||
matches: &clap::ArgMatches<'static>, config: &Config, app: &mut app::App,
|
||||
) {
|
||||
if matches.is_present("CASE_SENSITIVE") {
|
||||
app.search_state.toggle_ignore_case();
|
||||
app.process_search_state.toggle_ignore_case();
|
||||
} else if let Some(flags) = &config.flags {
|
||||
if let Some(case_sensitive) = flags.case_sensitive {
|
||||
if case_sensitive {
|
||||
app.search_state.toggle_ignore_case();
|
||||
app.process_search_state.toggle_ignore_case();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -540,11 +540,11 @@ fn enable_app_match_whole_word(
|
|||
matches: &clap::ArgMatches<'static>, config: &Config, app: &mut app::App,
|
||||
) {
|
||||
if matches.is_present("WHOLE_WORD") {
|
||||
app.search_state.toggle_search_whole_word();
|
||||
app.process_search_state.toggle_search_whole_word();
|
||||
} else if let Some(flags) = &config.flags {
|
||||
if let Some(whole_word) = flags.whole_word {
|
||||
if whole_word {
|
||||
app.search_state.toggle_search_whole_word();
|
||||
app.process_search_state.toggle_search_whole_word();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -552,11 +552,11 @@ fn enable_app_match_whole_word(
|
|||
|
||||
fn enable_app_use_regex(matches: &clap::ArgMatches<'static>, config: &Config, app: &mut app::App) {
|
||||
if matches.is_present("REGEX_DEFAULT") {
|
||||
app.search_state.toggle_search_regex();
|
||||
app.process_search_state.toggle_search_regex();
|
||||
} else if let Some(flags) = &config.flags {
|
||||
if let Some(regex) = flags.regex {
|
||||
if regex {
|
||||
app.search_state.toggle_search_regex();
|
||||
app.process_search_state.toggle_search_regex();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -735,7 +735,7 @@ fn update_final_process_list(app: &mut app::App) {
|
|||
.clone()
|
||||
.into_iter()
|
||||
.filter(|process| {
|
||||
if app.search_state.is_invalid_or_blank_search {
|
||||
if app.process_search_state.is_invalid_or_blank_search {
|
||||
true
|
||||
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||
matcher.is_match(&process.name)
|
||||
|
@ -749,10 +749,10 @@ fn update_final_process_list(app: &mut app::App) {
|
|||
.process_data
|
||||
.iter()
|
||||
.filter(|(_pid, process)| {
|
||||
if app.search_state.is_invalid_or_blank_search {
|
||||
if app.process_search_state.is_invalid_or_blank_search {
|
||||
true
|
||||
} else if let Ok(matcher) = app.get_current_regex_matcher() {
|
||||
if app.search_state.is_searching_with_pid {
|
||||
if app.process_search_state.is_searching_with_pid {
|
||||
matcher.is_match(&process.pid.to_string())
|
||||
} else {
|
||||
matcher.is_match(&process.name)
|
||||
|
|
Loading…
Reference in a new issue