diff --git a/Cargo.toml b/Cargo.toml index 8f99a4e7..6f6bb9e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ exclude = [ ] edition = "2024" # The oldest version I've tested that should still build - note this is not an official MSRV! -rust-version = "1.85" +rust-version = "1.95.0" [lib] test = true diff --git a/src/app.rs b/src/app.rs index 34b369c0..bd6b9022 100644 --- a/src/app.rs +++ b/src/app.rs @@ -243,13 +243,12 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) + && (pws.is_search_enabled() || pws.is_sort_open) { - if pws.is_search_enabled() || pws.is_sort_open { - pws.proc_search.search_state.is_enabled = false; - pws.is_sort_open = false; - self.is_force_redraw = true; - return; - } + pws.proc_search.search_state.is_enabled = false; + pws.is_sort_open = false; + self.is_force_redraw = true; + return; } } BottomWidgetType::ProcSearch => { @@ -257,13 +256,12 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id - 1) + && pws.is_search_enabled() { - if pws.is_search_enabled() { - pws.proc_search.search_state.is_enabled = false; - self.move_widget_selection(&WidgetDirection::Up); - self.is_force_redraw = true; - return; - } + pws.proc_search.search_state.is_enabled = false; + self.move_widget_selection(&WidgetDirection::Up); + self.is_force_redraw = true; + return; } } BottomWidgetType::ProcSort => { @@ -271,13 +269,12 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id - 2) + && pws.is_sort_open { - if pws.is_sort_open { - pws.is_sort_open = false; - self.move_widget_selection(&WidgetDirection::Right); - self.is_force_redraw = true; - return; - } + pws.is_sort_open = false; + self.move_widget_selection(&WidgetDirection::Right); + self.is_force_redraw = true; + return; } } _ => {} @@ -318,16 +315,14 @@ impl App { pub fn on_tab(&mut self) { // Allow usage whilst only in processes - if !self.ignore_normal_keybinds() { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self - .states - .proc_state - .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.toggle_tab(); - } - } + if !self.ignore_normal_keybinds() + && let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self + .states + .proc_state + .get_mut_widget_state(self.current_widget.widget_id) + { + proc_widget_state.toggle_tab(); } } @@ -426,11 +421,11 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.is_search_enabled() { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state.proc_search.search_toggle_ignore_case(); - proc_widget_state.update_query(); - } + proc_widget_state.proc_search.search_toggle_ignore_case(); + proc_widget_state.update_query(); } } @@ -441,11 +436,11 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.is_search_enabled() { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state.proc_search.search_toggle_whole_word(); - proc_widget_state.update_query(); - } + proc_widget_state.proc_search.search_toggle_whole_word(); + proc_widget_state.update_query(); } } @@ -456,11 +451,11 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.is_search_enabled() { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state.proc_search.search_toggle_regex(); - proc_widget_state.update_query(); - } + proc_widget_state.proc_search.search_toggle_regex(); + proc_widget_state.update_query(); } } @@ -502,12 +497,11 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id - 1) + && proc_widget_state.is_search_enabled() { - if proc_widget_state.is_search_enabled() { - proc_widget_state.proc_search.search_state.is_enabled = false; - self.move_widget_selection(&WidgetDirection::Up); - self.is_force_redraw = true; - } + proc_widget_state.proc_search.search_state.is_enabled = false; + self.move_widget_selection(&WidgetDirection::Up); + self.is_force_redraw = true; } } BottomWidgetType::ProcSort => { @@ -543,17 +537,16 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.proc_search.search_state.is_enabled { - if is_in_search_widget && proc_widget_state.proc_search.search_state.is_enabled - { - proc_widget_state - .proc_search - .search_state - .input_field_state - .delete_at_cursor(); + proc_widget_state + .proc_search + .search_state + .input_field_state + .delete_at_cursor(); - proc_widget_state.update_query(); - } + proc_widget_state.update_query(); } } BottomWidgetType::Proc => { @@ -576,16 +569,16 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.proc_search.search_state.is_enabled { - if is_in_search_widget && proc_widget_state.proc_search.search_state.is_enabled { - proc_widget_state - .proc_search - .search_state - .input_field_state - .delete_behind_cursor(); + proc_widget_state + .proc_search + .search_state + .input_field_state + .delete_behind_cursor(); - proc_widget_state.update_query(); - } + proc_widget_state.update_query(); } } } @@ -637,14 +630,13 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id - 1) + && is_in_search_widget { - if is_in_search_widget { - proc_widget_state - .proc_search - .search_state - .input_field_state - .move_left(); - } + proc_widget_state + .proc_search + .search_state + .input_field_state + .move_left(); } } BottomWidgetType::Battery => { @@ -689,32 +681,28 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id - 1) + && is_in_search_widget { - if is_in_search_widget { - proc_widget_state - .proc_search - .search_state - .input_field_state - .move_right(); - } + proc_widget_state + .proc_search + .search_state + .input_field_state + .move_right(); } } BottomWidgetType::Battery => { #[cfg(feature = "battery")] { let battery_count = self.data_store.get_data().battery_harvest.len(); - if battery_count > 1 { - if let Some(battery_widget_state) = self + if battery_count > 1 + && let Some(battery_widget_state) = self .states .battery_state .get_mut_widget_state(self.current_widget.widget_id) - { - if battery_widget_state.currently_selected_battery_index - < battery_count - 1 - { - battery_widget_state.currently_selected_battery_index += 1; - } - } + && battery_widget_state.currently_selected_battery_index + < battery_count - 1 + { + battery_widget_state.currently_selected_battery_index += 1; } } } @@ -727,14 +715,13 @@ impl App { pub fn on_space_key(&mut self) { if !self.is_in_dialog() { - if self.current_widget.widget_type == BottomWidgetType::Proc { - if let Some(proc_widget_state) = self + if self.current_widget.widget_type == BottomWidgetType::Proc + && let Some(proc_widget_state) = self .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.toggle_current_tree_branch_entry(); - } + { + proc_widget_state.toggle_current_tree_branch_entry(); } } else if self.help_dialog_state.is_help_searching() { self.on_char_key(' '); @@ -748,16 +735,16 @@ impl App { let current = &mut self.help_dialog_state.scroll_state.current_scroll_index; let amount = self.help_dialog_state.height; *current = current.saturating_sub(amount); - } else if self.current_widget.widget_type.is_widget_table() { - if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( + } else if self.current_widget.widget_type.is_widget_table() + && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, - ) { - let border_offset = u16::from(self.is_drawing_border()); - let header_offset = self.header_offset(&self.current_widget); - let height = brc_y - tlc_y - 2 * border_offset - header_offset; - self.change_position_count(-(height as i64)); - } + ) + { + let border_offset = u16::from(self.is_drawing_border()); + let header_offset = self.header_offset(&self.current_widget); + let height = brc_y - tlc_y - 2 * border_offset - header_offset; + self.change_position_count(-(height as i64)); } } @@ -769,16 +756,16 @@ impl App { let amount = self.help_dialog_state.height; self.help_scroll_to_or_max(current + amount); - } else if self.current_widget.widget_type.is_widget_table() { - if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( + } else if self.current_widget.widget_type.is_widget_table() + && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, - ) { - let border_offset = u16::from(self.is_drawing_border()); - let header_offset = self.header_offset(&self.current_widget); - let height = brc_y - tlc_y - 2 * border_offset - header_offset; - self.change_position_count(height as i64); - } + ) + { + let border_offset = u16::from(self.is_drawing_border()); + let header_offset = self.header_offset(&self.current_widget); + let height = brc_y - tlc_y - 2 * border_offset - header_offset; + self.change_position_count(height as i64); } } @@ -788,16 +775,16 @@ impl App { let amount = self.help_dialog_state.height / 2; *current = current.saturating_sub(amount); - } else if self.current_widget.widget_type.is_widget_table() { - if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( + } else if self.current_widget.widget_type.is_widget_table() + && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, - ) { - let border_offset = u16::from(self.is_drawing_border()); - let header_offset = self.header_offset(&self.current_widget); - let height = brc_y - tlc_y - 2 * border_offset - header_offset; - self.change_position_count(-(height as i64) / 2); - } + ) + { + let border_offset = u16::from(self.is_drawing_border()); + let header_offset = self.header_offset(&self.current_widget); + let height = brc_y - tlc_y - 2 * border_offset - header_offset; + self.change_position_count(-(height as i64) / 2); } } @@ -807,16 +794,16 @@ impl App { let amount = self.help_dialog_state.height / 2; self.help_scroll_to_or_max(current + amount); - } else if self.current_widget.widget_type.is_widget_table() { - if let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( + } else if self.current_widget.widget_type.is_widget_table() + && let (Some((_tlc_x, tlc_y)), Some((_brc_x, brc_y))) = ( &self.current_widget.top_left_corner, &self.current_widget.bottom_right_corner, - ) { - let border_offset = u16::from(self.is_drawing_border()); - let header_offset = self.header_offset(&self.current_widget); - let height = brc_y - tlc_y - 2 * border_offset - header_offset; - self.change_position_count(height as i64 / 2); - } + ) + { + let border_offset = u16::from(self.is_drawing_border()); + let header_offset = self.header_offset(&self.current_widget); + let height = brc_y - tlc_y - 2 * border_offset - header_offset; + self.change_position_count(height as i64 / 2); } } @@ -826,23 +813,22 @@ impl App { .search_input_state .skip_to_beginning(); self.is_force_redraw = true; - } else 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 - .states - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget { - proc_widget_state - .proc_search - .search_state - .input_field_state - .skip_to_beginning(); - } - } + } else if !self.ignore_normal_keybinds() + && 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 + .states + .proc_state + .widget_states + .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + { + proc_widget_state + .proc_search + .search_state + .input_field_state + .skip_to_beginning(); } } } @@ -851,23 +837,22 @@ impl App { if self.help_dialog_state.is_help_searching() { self.help_dialog_state.search_input_state.skip_to_end(); self.is_force_redraw = true; - } else 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 - .states - .proc_state - .widget_states - .get_mut(&(self.current_widget.widget_id - 1)) - { - if is_in_search_widget { - proc_widget_state - .proc_search - .search_state - .input_field_state - .skip_to_end(); - } - } + } else if !self.ignore_normal_keybinds() + && 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 + .states + .proc_state + .widget_states + .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + { + proc_widget_state + .proc_search + .search_state + .input_field_state + .skip_to_end(); } } } @@ -876,15 +861,14 @@ impl App { if self.help_dialog_state.is_help_searching() { self.help_dialog_state.search_input_state = Default::default(); self.is_force_redraw = true; - } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) - { - proc_widget_state.clear_search(); - } + { + proc_widget_state.clear_search(); } } @@ -894,21 +878,20 @@ impl App { .search_input_state .delete_previous_word(); self.is_force_redraw = true; - } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + } else if let BottomWidgetType::ProcSearch = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) - { - proc_widget_state - .proc_search - .search_state - .input_field_state - .delete_previous_word(); + { + proc_widget_state + .proc_search + .search_state + .input_field_state + .delete_previous_word(); - proc_widget_state.update_query(); - } + proc_widget_state.update_query(); } } @@ -939,18 +922,18 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.is_search_enabled() { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state - .proc_search - .search_state - .input_field_state - .insert_char(caught_char); + proc_widget_state + .proc_search + .search_state + .input_field_state + .insert_char(caught_char); - proc_widget_state.update_query(); + proc_widget_state.update_query(); - return; - } + return; } } self.handle_char(caught_char); @@ -999,36 +982,35 @@ impl App { .proc_state .widget_states .get(&self.current_widget.widget_id) + && let Some(current) = pws.table.current_item() { - if let Some(current) = pws.table.current_item() { - let id = current.id.to_string(); - if let Some(pids) = pws - .id_pid_map - .get(&id) - .cloned() - .or_else(|| Some(vec![current.pid])) - { - let current_process = (id, pids); + let id = current.id.to_string(); + if let Some(pids) = pws + .id_pid_map + .get(&id) + .cloned() + .or_else(|| Some(vec![current.pid])) + { + let current_process = (id, pids); - let use_simple_selection = { - cfg_if::cfg_if! { - if #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] { - !self.app_config_fields.is_advanced_kill - } else { - true - } + let use_simple_selection = { + cfg_if::cfg_if! { + if #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] { + !self.app_config_fields.is_advanced_kill + } else { + true } - }; + } + }; - self.process_kill_dialog.start_process_kill( - current_process.0, - current_process.1, - use_simple_selection, - ); + self.process_kill_dialog.start_process_kill( + current_process.0, + current_process.1, + use_simple_selection, + ); - // TODO: I don't think most of this is needed. - self.is_determining_widget_boundary = true; - } + // TODO: I don't think most of this is needed. + self.is_determining_widget_boundary = true; } } } @@ -1042,16 +1024,17 @@ impl App { '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; + if let Some(second_char) = self.second_char + && self.awaiting_second_char + && second_char == 'd' + { + is_first_d = false; + self.awaiting_second_char = false; + self.second_char = None; - self.reset_multi_tap_keys(); + self.reset_multi_tap_keys(); - self.kill_current_process(); - } + self.kill_current_process(); } if is_first_d { @@ -1068,13 +1051,14 @@ impl App { } '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 let Some(second_char) = self.second_char + && 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 { @@ -1087,14 +1071,13 @@ impl App { 'j' => self.on_down_key(), 'f' => self.data_store.toggle_frozen(), 'c' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + if let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.select_column(ProcWidgetColumn::Cpu); - } + { + proc_widget_state.select_column(ProcWidgetColumn::Cpu); } } 'm' => { @@ -1132,14 +1115,13 @@ impl App { } } 'P' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + if let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.toggle_command(); - } + { + proc_widget_state.toggle_command(); } } 'n' => { @@ -1161,26 +1143,24 @@ impl App { } #[cfg(feature = "gpu")] 'M' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + if let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.select_column(ProcWidgetColumn::GpuMem); - } + { + proc_widget_state.select_column(ProcWidgetColumn::GpuMem); } } #[cfg(feature = "gpu")] 'C' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + if let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) - { - proc_widget_state.select_column(ProcWidgetColumn::GpuUtil); - } + { + proc_widget_state.select_column(ProcWidgetColumn::GpuUtil); } } '?' => { @@ -1257,24 +1237,24 @@ impl App { '%' => self.toggle_percentages(), #[cfg(target_os = "linux")] 'z' => { - if let BottomWidgetType::Proc = self.current_widget.widget_type { - if let Some(proc_widget_state) = self + if let BottomWidgetType::Proc = self.current_widget.widget_type + && let Some(proc_widget_state) = self .states .proc_state .widget_states .get_mut(&self.current_widget.widget_id) - { - proc_widget_state.toggle_k_thread(); - } + { + proc_widget_state.toggle_k_thread(); } } _ => {} } - if let Some(second_char) = self.second_char { - if self.awaiting_second_char && caught_char != second_char { - self.awaiting_second_char = false; - } + if let Some(second_char) = self.second_char + && self.awaiting_second_char + && caught_char != second_char + { + self.awaiting_second_char = false; } } @@ -1326,316 +1306,296 @@ impl App { 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.states.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 - .states - .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.states.basic_table_widget_state + }) && 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.states.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 + .states + .proc_state + .widget_states + .get(&(new_widget_id - 2)) { - 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(); - } - } + 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, + } && 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.states.basic_table_widget_state + } + } else { + self.current_widget = new_widget.clone(); + } + + if let Some(basic_table_widget_state) = + &mut self.states.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 + && 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.states.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 { - // 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; - } + 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(); - } + 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(), } + _ => 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 - .states - .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) + } + _ 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 + .states + .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 + && 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 + .states + .proc_state + .widget_states + .get(&(new_widget_id - *offset)) + { + match &new_widget.widget_type { + BottomWidgetType::ProcSearch => + { + #[allow(clippy::collapsible_match)] + if !proc_widget_state.is_search_enabled() { + if let Some(next_neighbour_id) = + option_next_neighbour_id + && 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 - .states - .proc_state - .widget_states - .get(&(new_widget_id - *offset)) - { - match &new_widget.widget_type { - BottomWidgetType::ProcSearch => - { - #[allow(clippy::collapsible_match)] - if !proc_widget_state.is_search_enabled() { - if let Some(next_neighbour_id) = - option_next_neighbour_id - && 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 => - { - #[allow(clippy::collapsible_match)] - if !proc_widget_state.is_sort_open { - if let Some(next_neighbour_id) = - option_next_neighbour_id - && 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(); - } - } - _ => { + } else { self.current_widget = new_widget.clone(); } } + BottomWidgetType::ProcSort => { + #[allow(clippy::collapsible_match)] + if !proc_widget_state.is_sort_open { + if let Some(next_neighbour_id) = + option_next_neighbour_id + && 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 - .states - .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(); + _ => { + self.current_widget = new_widget.clone(); + } + } + } else { + // Reflect + match &new_widget.widget_type { + BottomWidgetType::CpuLegend => { + if let Some(cpu_widget_state) = self + .states + .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 + .states + .proc_state + .widget_states + .get(&(new_widget_id - *offset)) + { + match &new_widget.widget_type { + BottomWidgetType::ProcSearch => + { + #[allow(clippy::collapsible_match)] + 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(); } - } else { + } + BottomWidgetType::ProcSort => { + #[allow(clippy::collapsible_match)] + 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(); } } } - BottomWidgetType::ProcSearch - | BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .states - .proc_state - .widget_states - .get(&(new_widget_id - *offset)) - { - match &new_widget.widget_type { - BottomWidgetType::ProcSearch => - { - #[allow(clippy::collapsible_match)] - 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 => - { - #[allow(clippy::collapsible_match)] - 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(); - } + } + _ => { + self.current_widget = new_widget.clone(); } } } } - _ => { - // Cannot be hidden, does not special treatment. - 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 - .states - .cpu_state - .widget_states - .get(&(self.current_widget.widget_id - *offset)) - { - if cpu_widget_state.is_legend_hidden { + 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 + .states + .cpu_state + .widget_states + .get(&(self.current_widget.widget_id - *offset)) + && cpu_widget_state.is_legend_hidden + { + reflection_dir = Some(parent_direction.clone()); + } + } + BottomWidgetType::ProcSearch | BottomWidgetType::ProcSort => { + if let Some(proc_widget_state) = self + .states + .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::ProcSearch | BottomWidgetType::ProcSort => { - if let Some(proc_widget_state) = self - .states - .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()); - } - _ => {} + 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); - } + if let Some(ref_dir) = &reflection_dir { + self.move_widget_selection_logic(ref_dir); } } } else { @@ -1643,16 +1603,13 @@ impl App { 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) = + if let BottomWidgetType::ProcSearch = self.current_widget.widget_type + && 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(); - } - } - } + && let Some(new_widget_id) = current_widget.up_neighbour + && 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 { @@ -1662,18 +1619,14 @@ impl App { 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.states.proc_state.get_widget_state(widget_id) - { - if proc_widget_state.is_search_enabled() { - self.current_widget = new_widget.clone(); - } - } - } - } + if let Some(current_widget) = self.widget_map.get(&widget_id) + && let Some(new_widget_id) = current_widget.down_neighbour + && let Some(new_widget) = self.widget_map.get(&new_widget_id) + && let Some(proc_widget_state) = + self.states.proc_state.get_widget_state(widget_id) + && proc_widget_state.is_search_enabled() + { + self.current_widget = new_widget.clone(); } } _ => {} @@ -1684,84 +1637,67 @@ impl App { 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 + if let Some(new_widget_id) = self.current_widget.left_neighbour + && let Some(proc_widget_state) = self .states .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(); // TODO: Could I remove this clone w/ static references? - } - } - } + && proc_widget_state.is_sort_open + && let Some(proc_sort_widget) = self.widget_map.get(&new_widget_id) + { + self.current_widget = proc_sort_widget.clone(); // TODO: Could I remove this clone w/ static references? } } else if self.app_config_fields.cpu_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 - .states - .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(); - } - } + if let BottomWidgetType::Cpu = self.current_widget.widget_type + && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) + && let Some(cpu_widget_state) = self + .states + .cpu_state + .widget_states + .get(&self.current_widget.widget_id) + && !cpu_widget_state.is_legend_hidden + && let Some(new_widget_id) = current_widget.left_neighbour + && 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 + && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) + && let Some(new_widget_id) = current_widget.left_neighbour + && 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(); - } + if let Some(new_widget_id) = self.current_widget.right_neighbour + && 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.cpu_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 - .states - .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(); - } - } - } - } + if let BottomWidgetType::CpuLegend = self.current_widget.widget_type + && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) + && let Some(new_widget_id) = current_widget.right_neighbour + && 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 + && let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) + && let Some(cpu_widget_state) = self + .states + .cpu_state + .widget_states + .get(&self.current_widget.widget_id) + && !cpu_widget_state.is_legend_hidden + && let Some(new_widget_id) = current_widget.right_neighbour + && let Some(new_widget) = self.widget_map.get(&new_widget_id) + { + self.current_widget = new_widget.clone(); } } @@ -1859,10 +1795,9 @@ impl App { .states .disk_state .get_mut_widget_state(self.current_widget.widget_id) + && !self.data_store.get_data().disk_harvest.is_empty() { - if !self.data_store.get_data().disk_harvest.is_empty() { - disk_widget_state.table.scroll_to_last(); - } + disk_widget_state.table.scroll_to_last(); } } BottomWidgetType::CpuLegend => { @@ -2124,65 +2059,55 @@ impl App { // Short circuit if we're in basic table... we might have to handle the basic // table arrow case here... - if let Some(bt) = &mut self.states.basic_table_widget_state { - if let ( + if let Some(bt) = &mut self.states.basic_table_widget_state + && let ( Some((left_tlc_x, left_tlc_y)), Some((left_brc_x, left_brc_y)), Some((right_tlc_x, right_tlc_y)), Some((right_brc_x, right_brc_y)), ) = (bt.left_tlc, bt.left_brc, bt.right_tlc, bt.right_brc) - { - if (x >= left_tlc_x && y >= left_tlc_y) && (x < left_brc_x && y < left_brc_y) { - // Case for the left "button" in the simple arrow. - if let Some(new_widget) = - self.widget_map.get(&(bt.currently_displayed_widget_id)) - { - // We have to move to the current table widget first... - self.current_widget = new_widget.clone(); + { + if (x >= left_tlc_x && y >= left_tlc_y) && (x < left_brc_x && y < left_brc_y) { + // Case for the left "button" in the simple arrow. + if let Some(new_widget) = self.widget_map.get(&(bt.currently_displayed_widget_id)) { + // We have to move to the current table widget first... + self.current_widget = new_widget.clone(); - if let BottomWidgetType::Proc = &new_widget.widget_type { - if let Some(proc_widget_state) = self - .states - .proc_state - .get_widget_state(new_widget.widget_id) - { - if proc_widget_state.is_sort_open { - self.move_widget_selection(&WidgetDirection::Left); - } - } - } + if let BottomWidgetType::Proc = &new_widget.widget_type + && let Some(proc_widget_state) = self + .states + .proc_state + .get_widget_state(new_widget.widget_id) + && proc_widget_state.is_sort_open + { self.move_widget_selection(&WidgetDirection::Left); - return; - } - } else if (x >= right_tlc_x && y >= right_tlc_y) - && (x < right_brc_x && y < right_brc_y) - { - // Case for the right "button" in the simple arrow. - if let Some(new_widget) = - self.widget_map.get(&(bt.currently_displayed_widget_id)) - { - // We have to move to the current table widget first... - self.current_widget = new_widget.clone(); - - if let BottomWidgetType::ProcSort = &new_widget.widget_type { - if let Some(proc_widget_state) = self - .states - .proc_state - .get_widget_state(new_widget.widget_id - 2) - { - if proc_widget_state.is_sort_open { - self.move_widget_selection(&WidgetDirection::Right); - } - } - } - } - self.move_widget_selection(&WidgetDirection::Right); - // Bit extra logic to ensure you always land on a proc widget, not the sort - if let BottomWidgetType::ProcSort = &self.current_widget.widget_type { - self.move_widget_selection(&WidgetDirection::Right); } + self.move_widget_selection(&WidgetDirection::Left); return; } + } else if (x >= right_tlc_x && y >= right_tlc_y) && (x < right_brc_x && y < right_brc_y) + { + // Case for the right "button" in the simple arrow. + if let Some(new_widget) = self.widget_map.get(&(bt.currently_displayed_widget_id)) { + // We have to move to the current table widget first... + self.current_widget = new_widget.clone(); + + if let BottomWidgetType::ProcSort = &new_widget.widget_type + && let Some(proc_widget_state) = self + .states + .proc_state + .get_widget_state(new_widget.widget_id - 2) + && proc_widget_state.is_sort_open + { + self.move_widget_selection(&WidgetDirection::Right); + } + } + self.move_widget_selection(&WidgetDirection::Right); + // Bit extra logic to ensure you always land on a proc widget, not the sort + if let BottomWidgetType::ProcSort = &self.current_widget.widget_type { + self.move_widget_selection(&WidgetDirection::Right); + } + return; } } @@ -2196,32 +2121,31 @@ impl App { for (new_widget_id, widget) in &self.widget_map { if let (Some((tlc_x, tlc_y)), Some((brc_x, brc_y))) = (widget.top_left_corner, widget.bottom_right_corner) + && (x >= tlc_x && y >= tlc_y) + && (x < brc_x && y < brc_y) + && let Some(new_widget) = self.widget_map.get(new_widget_id) { - if (x >= tlc_x && y >= tlc_y) && (x < brc_x && y < brc_y) { - if let Some(new_widget) = self.widget_map.get(new_widget_id) { - self.current_widget = new_widget.clone(); - match &self.current_widget.widget_type { - BottomWidgetType::Temp - | BottomWidgetType::Proc - | BottomWidgetType::ProcSort - | BottomWidgetType::Disk - | BottomWidgetType::Battery => { - if let Some(basic_table_widget_state) = - &mut self.states.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(); - } - } - _ => {} + self.current_widget = new_widget.clone(); + match &self.current_widget.widget_type { + BottomWidgetType::Temp + | BottomWidgetType::Proc + | BottomWidgetType::ProcSort + | BottomWidgetType::Disk + | BottomWidgetType::Battery => { + if let Some(basic_table_widget_state) = + &mut self.states.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(); } - - failed_to_get = false; - break; } + _ => {} } + + failed_to_get = false; + break; } } @@ -2257,27 +2181,25 @@ impl App { .states .proc_state .get_widget_state(self.current_widget.widget_id) - { - if let Some(visual_index) = + && let Some(visual_index) = proc_widget_state.table.ratatui_selected() - { - let is_tree_mode = matches!( - proc_widget_state.mode, - ProcWidgetMode::Tree { .. } - ); - let change = - offset_clicked_entry as i64 - visual_index as i64; + { + let is_tree_mode = matches!( + proc_widget_state.mode, + ProcWidgetMode::Tree { .. } + ); + let change = + offset_clicked_entry as i64 - visual_index as i64; - self.change_process_position(change); + self.change_process_position(change); - // If in tree mode, also check to see if this click is - // on - // the same entry as the already selected one - if it - // is, - // then we minimize. - if is_tree_mode && change == 0 { - self.toggle_collapsing_process_branch(); - } + // If in tree mode, also check to see if this click is + // on + // the same entry as the already selected one - if it + // is, + // then we minimize. + if is_tree_mode && change == 0 { + self.toggle_collapsing_process_branch(); } } } @@ -2287,14 +2209,12 @@ impl App { .states .proc_state .get_widget_state(self.current_widget.widget_id - 2) - { - if let Some(visual_index) = + && let Some(visual_index) = proc_widget_state.sort_table.ratatui_selected() - { - self.change_process_sort_position( - offset_clicked_entry as i64 - visual_index as i64, - ); - } + { + self.change_process_sort_position( + offset_clicked_entry as i64 - visual_index as i64, + ); } } BottomWidgetType::CpuLegend => { @@ -2302,14 +2222,12 @@ impl App { .states .cpu_state .get_widget_state(self.current_widget.widget_id - 1) - { - if let Some(visual_index) = + && let Some(visual_index) = cpu_widget_state.table.ratatui_selected() - { - self.change_cpu_legend_position( - offset_clicked_entry as i64 - visual_index as i64, - ); - } + { + self.change_cpu_legend_position( + offset_clicked_entry as i64 - visual_index as i64, + ); } } BottomWidgetType::Temp => { @@ -2317,14 +2235,12 @@ impl App { .states .temp_state .get_widget_state(self.current_widget.widget_id) - { - if let Some(visual_index) = + && let Some(visual_index) = temp_widget_state.table.ratatui_selected() - { - self.change_temp_position( - offset_clicked_entry as i64 - visual_index as i64, - ); - } + { + self.change_temp_position( + offset_clicked_entry as i64 - visual_index as i64, + ); } } BottomWidgetType::Disk => { @@ -2332,14 +2248,12 @@ impl App { .states .disk_state .get_widget_state(self.current_widget.widget_id) - { - if let Some(visual_index) = + && let Some(visual_index) = disk_widget_state.table.ratatui_selected() - { - self.change_disk_position( - offset_clicked_entry as i64 - visual_index as i64, - ); - } + { + self.change_disk_position( + offset_clicked_entry as i64 - visual_index as i64, + ); } } _ => {} @@ -2355,10 +2269,9 @@ impl App { .states .proc_state .get_mut_widget_state(self.current_widget.widget_id) + && state.table.try_select_location(x, y).is_some() { - if state.table.try_select_location(x, y).is_some() { - state.force_data_update(); - } + state.force_data_update(); } } BottomWidgetType::Temp => { @@ -2366,10 +2279,9 @@ impl App { .states .temp_state .get_mut_widget_state(self.current_widget.widget_id) + && temp.table.try_select_location(x, y).is_some() { - if temp.table.try_select_location(x, y).is_some() { - temp.force_data_update(); - } + temp.force_data_update(); } } BottomWidgetType::Disk => { @@ -2377,10 +2289,9 @@ impl App { .states .disk_state .get_mut_widget_state(self.current_widget.widget_id) + && disk.table.try_select_location(x, y).is_some() { - if disk.table.try_select_location(x, y).is_some() { - disk.force_data_update(); - } + disk.force_data_update(); } } _ => (), @@ -2394,25 +2305,22 @@ impl App { .states .battery_state .get_mut_widget_state(self.current_widget.widget_id) + && let Some(tab_spacing) = &battery_widget_state.tab_click_locs { - if let Some(tab_spacing) = &battery_widget_state.tab_click_locs { - for (itx, ((tlc_x, tlc_y), (brc_x, brc_y))) in - tab_spacing.iter().enumerate() - { - if (x >= *tlc_x && y >= *tlc_y) && (x <= *brc_x && y <= *brc_y) - { - let num_batteries = - self.data_store.get_data().battery_harvest.len(); - if itx >= num_batteries { - // range check to keep within current data - battery_widget_state.currently_selected_battery_index = - num_batteries - 1; - } else { - battery_widget_state.currently_selected_battery_index = - itx; - } - break; + for (itx, ((tlc_x, tlc_y), (brc_x, brc_y))) in + tab_spacing.iter().enumerate() + { + if (x >= *tlc_x && y >= *tlc_y) && (x <= *brc_x && y <= *brc_y) { + let num_batteries = + self.data_store.get_data().battery_harvest.len(); + if itx >= num_batteries { + // range check to keep within current data + battery_widget_state.currently_selected_battery_index = + num_batteries - 1; + } else { + battery_widget_state.currently_selected_battery_index = itx; } + break; } } } @@ -2451,16 +2359,16 @@ impl App { .proc_state .widget_states .get_mut(&(self.current_widget.widget_id - 1)) + && is_in_search_widget + && proc_widget_state.is_search_enabled() { - if is_in_search_widget && proc_widget_state.is_search_enabled() { - proc_widget_state - .proc_search - .search_state - .input_field_state - .insert_string(paste); + proc_widget_state + .proc_search + .search_state + .input_field_state + .insert_string(paste); - proc_widget_state.update_query(); - } + proc_widget_state.update_query(); } } } diff --git a/src/app/data/store.rs b/src/app/data/store.rs index f8e1fe75..cfa08ac8 100644 --- a/src/app/data/store.rs +++ b/src/app/data/store.rs @@ -92,11 +92,11 @@ impl StoredData { // We must adjust all the network values to their selected type (defaults to // bits). - if matches!(settings.network_unit_type, DataUnit::Byte) { - if let Some(network) = &mut data.network { - network.rx /= 8; - network.tx /= 8; - } + if matches!(settings.network_unit_type, DataUnit::Byte) + && let Some(network) = &mut data.network + { + network.rx /= 8; + network.tx /= 8; } if !settings.use_basic_mode { @@ -150,17 +150,17 @@ impl StoredData { }) .unwrap_or_default(); - if let Some(disks) = data.disks { - if let Some(io) = data.io { - self.eat_disks(disks, io, harvested_time); + if let Some(disks) = data.disks + && let Some(io) = data.io + { + self.eat_disks(disks, io, harvested_time); - if used_widgets.use_disk_io_graph { - self.time_series_data.update_disk_io( - &self.disk_harvest, - &filters.disk_io_graph_filter, - settings.disk_io_graph_show_unmounted, - ); - } + if used_widgets.use_disk_io_graph { + self.time_series_data.update_disk_io( + &self.disk_harvest, + &filters.disk_io_graph_filter, + settings.disk_io_graph_show_unmounted, + ); } } @@ -252,22 +252,22 @@ impl StoredData { }; let (mut io_read_rate_bytes, mut io_write_rate_bytes) = (None, None); - if let Some(Some(io_device)) = io_device { - if let Some(prev_io) = self.prev_io.get_mut(itx) { - io_read_rate_bytes = Some( - ((io_device.read_bytes.saturating_sub(prev_io.0)) as f64 - / time_since_last_harvest) - .round() as u64, - ); + if let Some(Some(io_device)) = io_device + && let Some(prev_io) = self.prev_io.get_mut(itx) + { + io_read_rate_bytes = Some( + ((io_device.read_bytes.saturating_sub(prev_io.0)) as f64 + / time_since_last_harvest) + .round() as u64, + ); - io_write_rate_bytes = Some( - ((io_device.write_bytes.saturating_sub(prev_io.1)) as f64 - / time_since_last_harvest) - .round() as u64, - ); + io_write_rate_bytes = Some( + ((io_device.write_bytes.saturating_sub(prev_io.1)) as f64 + / time_since_last_harvest) + .round() as u64, + ); - *prev_io = (io_device.read_bytes, io_device.write_bytes); - } + *prev_io = (io_device.read_bytes, io_device.write_bytes); } let summed_total_bytes = match (device.used_space, device.free_space) { diff --git a/src/app/layout_manager.rs b/src/app/layout_manager.rs index 9f7cbafe..e596a1c8 100644 --- a/src/app/layout_manager.rs +++ b/src/app/layout_manager.rs @@ -179,129 +179,124 @@ impl BottomLayout { if let Some(current_col) = current_row .1 .get(&(col_width_percentage_start, col_width_percentage_end)) - { - if let Some(current_col_row) = current_col.1.get(&( + && let Some(current_col_row) = current_col.1.get(&( col_row_height_percentage_start, col_row_height_percentage_end, - )) { - if let Some(to_left_widget) = current_col_row - .1 - .range( - ..( - widget_width_percentage_start, - widget_width_percentage_start, - ), - ) - .next_back() - { - widget.left_neighbour = Some(*to_left_widget.1); - } + )) + { + if let Some(to_left_widget) = current_col_row + .1 + .range( + ..( + widget_width_percentage_start, + widget_width_percentage_start, + ), + ) + .next_back() + { + widget.left_neighbour = Some(*to_left_widget.1); + } - // Right - if let Some(to_right_neighbour) = current_col_row - .1 - .range( - ( - widget_width_percentage_end, - widget_width_percentage_end, - ).., - ) - .next() - { - widget.right_neighbour = Some(*to_right_neighbour.1); - } + // Right + if let Some(to_right_neighbour) = current_col_row + .1 + .range( + ( + widget_width_percentage_end, + widget_width_percentage_end, + ).., + ) + .next() + { + widget.right_neighbour = Some(*to_right_neighbour.1); } } - if widget.left_neighbour.is_none() { - if let Some(to_left_col) = current_row + if widget.left_neighbour.is_none() + && let Some(to_left_col) = current_row .1 .range( ..(col_width_percentage_start, col_width_percentage_start), ) .next_back() - { - // Check left in same row - let mut current_best_distance = 0; - let mut current_best_widget_id = widget.widget_id; + { + // Check left in same row + let mut current_best_distance = 0; + let mut current_best_widget_id = widget.widget_id; - for widget_position in &(to_left_col.1).1 { - let candidate_start = (widget_position.0).0; - let candidate_end = (widget_position.0).1; + for widget_position in &(to_left_col.1).1 { + let candidate_start = (widget_position.0).0; + let candidate_end = (widget_position.0).1; - if is_intersecting( + if is_intersecting( + ( + col_row_height_percentage_start, + col_row_height_percentage_end, + ), + (candidate_start, candidate_end), + ) { + let candidate_distance = get_distance( ( col_row_height_percentage_start, col_row_height_percentage_end, ), (candidate_start, candidate_end), - ) { - let candidate_distance = get_distance( - ( - col_row_height_percentage_start, - col_row_height_percentage_end, - ), - (candidate_start, candidate_end), - ); + ); - if current_best_distance < candidate_distance { - if let Some(new_best_widget) = - (widget_position.1).1.iter().next_back() - { - current_best_distance = candidate_distance + 1; - current_best_widget_id = *(new_best_widget.1); - } - } + if current_best_distance < candidate_distance + && let Some(new_best_widget) = + (widget_position.1).1.iter().next_back() + { + current_best_distance = candidate_distance + 1; + current_best_widget_id = *(new_best_widget.1); } } - if current_best_distance > 0 { - widget.left_neighbour = Some(current_best_widget_id); - } + } + if current_best_distance > 0 { + widget.left_neighbour = Some(current_best_widget_id); } } - if widget.right_neighbour.is_none() { - if let Some(to_right_col) = current_row + if widget.right_neighbour.is_none() + && let Some(to_right_col) = current_row .1 .range((col_width_percentage_end, col_width_percentage_end)..) .next() - { - // Check right in same row - let mut current_best_distance = 0; - let mut current_best_widget_id = widget.widget_id; + { + // Check right in same row + let mut current_best_distance = 0; + let mut current_best_widget_id = widget.widget_id; - for widget_position in &(to_right_col.1).1 { - let candidate_start = (widget_position.0).0; - let candidate_end = (widget_position.0).1; + for widget_position in &(to_right_col.1).1 { + let candidate_start = (widget_position.0).0; + let candidate_end = (widget_position.0).1; - if is_intersecting( + if is_intersecting( + ( + col_row_height_percentage_start, + col_row_height_percentage_end, + ), + (candidate_start, candidate_end), + ) { + let candidate_distance = get_distance( ( col_row_height_percentage_start, col_row_height_percentage_end, ), (candidate_start, candidate_end), - ) { - let candidate_distance = get_distance( - ( - col_row_height_percentage_start, - col_row_height_percentage_end, - ), - (candidate_start, candidate_end), - ); + ); - if current_best_distance < candidate_distance { - if let Some(new_best_widget) = - (widget_position.1).1.iter().next() - { - current_best_distance = candidate_distance + 1; - current_best_widget_id = *(new_best_widget.1); - } - } + if current_best_distance < candidate_distance + && let Some(new_best_widget) = + (widget_position.1).1.iter().next() + { + current_best_distance = candidate_distance + 1; + current_best_widget_id = *(new_best_widget.1); } } - if current_best_distance > 0 { - widget.right_neighbour = Some(current_best_widget_id); - } + } + if current_best_distance > 0 { + widget.right_neighbour = Some(current_best_widget_id); } } diff --git a/src/canvas.rs b/src/canvas.rs index d382866d..f9b31638 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -303,11 +303,11 @@ impl Painter { // bars... let cpu_height = { let c = (actual_cpu_data_len / 4) as u16 - + u16::from(actual_cpu_data_len % 4 != 0) + + u16::from(!actual_cpu_data_len.is_multiple_of(4)) + u16::from( app_state.app_config_fields.show_average_cpu && app_state.app_config_fields.dedicated_average_row - && actual_cpu_data_len.saturating_sub(1) % 4 != 0, + && !actual_cpu_data_len.saturating_sub(1).is_multiple_of(4), ); if c <= 1 { 1 } else { c } diff --git a/src/canvas/components/search_input.rs b/src/canvas/components/search_input.rs index a8834c32..b43b8bd2 100644 --- a/src/canvas/components/search_input.rs +++ b/src/canvas/components/search_input.rs @@ -80,11 +80,11 @@ pub fn render_search_input( input_vec.extend(query_spans); - if let Some(hint) = state.hint { - if state.input_field_state.current_query().is_empty() { - input_vec.push(Span::styled(" ", styles.text_style)); - input_vec.push(Span::styled(hint, styles.hint_style)); - } + if let Some(hint) = state.hint + && state.input_field_state.current_query().is_empty() + { + input_vec.push(Span::styled(" ", styles.text_style)); + input_vec.push(Span::styled(hint, styles.hint_style)); } let input_line = Line::from(input_vec); diff --git a/src/canvas/components/time_series/vendored.rs b/src/canvas/components/time_series/vendored.rs index 272418ec..4b152c6f 100644 --- a/src/canvas/components/time_series/vendored.rs +++ b/src/canvas/components/time_series/vendored.rs @@ -838,13 +838,12 @@ impl> Widget for TimeChart<'_, F> { } } - if let Some(y) = layout.axis_x { - if let Some(x) = layout.axis_y { - if let Some(cell) = buf.cell_mut((x, y)) { - cell.set_symbol(symbols::line::BOTTOM_LEFT) - .set_style(self.x_axis.style); - } - } + if let Some(y) = layout.axis_x + && let Some(x) = layout.axis_y + && let Some(cell) = buf.cell_mut((x, y)) + { + cell.set_symbol(symbols::line::BOTTOM_LEFT) + .set_style(self.x_axis.style); } let x_bounds = self.x_axis.bounds.get_bounds(); @@ -860,42 +859,42 @@ impl> Widget for TimeChart<'_, F> { }) .render(graph_area, buf); - if let Some((x, y)) = layout.title_x { - if let Some(title) = self.x_axis.title.as_ref() { - let width = graph_area - .right() - .saturating_sub(x) - .min(title.width() as u16); - buf.set_style( - Rect { - x, - y, - width, - height: 1, - }, - original_style, - ); - buf.set_line(x, y, title, width); - } + if let Some((x, y)) = layout.title_x + && let Some(title) = self.x_axis.title.as_ref() + { + let width = graph_area + .right() + .saturating_sub(x) + .min(title.width() as u16); + buf.set_style( + Rect { + x, + y, + width, + height: 1, + }, + original_style, + ); + buf.set_line(x, y, title, width); } - if let Some((x, y)) = layout.title_y { - if let Some(title) = self.y_axis.title.as_ref() { - let width = graph_area - .right() - .saturating_sub(x) - .min(title.width() as u16); - buf.set_style( - Rect { - x, - y, - width, - height: 1, - }, - original_style, - ); - buf.set_line(x, y, title, width); - } + if let Some((x, y)) = layout.title_y + && let Some(title) = self.y_axis.title.as_ref() + { + let width = graph_area + .right() + .saturating_sub(x) + .min(title.width() as u16); + buf.set_style( + Rect { + x, + y, + width, + height: 1, + }, + original_style, + ); + buf.set_line(x, y, title, width); } if let Some(legend_area) = layout.legend_area { diff --git a/src/canvas/dialogs/process_kill_dialog.rs b/src/canvas/dialogs/process_kill_dialog.rs index 5bfb3181..75360dd0 100644 --- a/src/canvas/dialogs/process_kill_dialog.rs +++ b/src/canvas/dialogs/process_kill_dialog.rs @@ -242,28 +242,25 @@ impl ProcessKillDialog { ButtonState::Signals { state, .. } => { use crate::utils::process_killer; - if let Some(selected) = state.selected() { - if selected != 0 { - // On Linux, we need to skip 32 and 33. - let signal = if cfg!(target_os = "linux") - && (selected == 32 || selected == 33) - { + if let Some(selected) = state.selected() + && selected != 0 + { + // On Linux, we need to skip 32 and 33. + let signal = + if cfg!(target_os = "linux") && (selected == 32 || selected == 33) { selected + 2 } else { selected }; - for pid in pids { - if let Err(err) = - process_killer::kill_process_given_pid(pid, signal) - { - self.state = ProcessKillDialogState::Error { - process_name, - pid: Some(pid), - err: err.to_string(), - }; - return; - } + for pid in pids { + if let Err(err) = process_killer::kill_process_given_pid(pid, signal) { + self.state = ProcessKillDialogState::Error { + process_name, + pid: Some(pid), + err: err.to_string(), + }; + return; } } } @@ -315,52 +312,51 @@ impl ProcessKillDialog { 'l' => self.on_right_key(), '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => { #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] - if let Some(value) = c.to_digit(10) { - if let ProcessKillDialogState::Selecting(ProcessKillSelectingInner { + if let Some(value) = c.to_digit(10) + && let ProcessKillDialogState::Selecting(ProcessKillSelectingInner { button_state: ButtonState::Signals { state, .. }, .. }) = &mut self.state - { - if let Some((prev, last_press)) = self.last_char { - if prev.is_ascii_digit() && last_press.elapsed() <= MAX_KEY_TIMEOUT { - let current = state.selected().unwrap_or(0); - let new = { - let new = current * 10 + value as usize; + { + if let Some((prev, last_press)) = self.last_char { + if prev.is_ascii_digit() && last_press.elapsed() <= MAX_KEY_TIMEOUT { + let current = state.selected().unwrap_or(0); + let new = { + let new = current * 10 + value as usize; - // Note that 32 and 33 are skipped on linux. - if cfg!(target_os = "linux") { - if new == 32 || new == 33 { - value as usize - } else if new >= 34 { - new - 2 - } else { - new - } + // Note that 32 and 33 are skipped on linux. + if cfg!(target_os = "linux") { + if new == 32 || new == 33 { + value as usize + } else if new >= 34 { + new - 2 } else { new } - }; - - if new >= SIGNAL_TEXT.len() { - // If the new value is too large, then just assume we instead - // want the value itself. - state.select(Some(value as usize)); - self.last_char = Some((c, Instant::now())); } else { - state.select(Some(new)); - self.last_char = None; + new } - } else { + }; + + if new >= SIGNAL_TEXT.len() { + // If the new value is too large, then just assume we instead + // want the value itself. state.select(Some(value as usize)); self.last_char = Some((c, Instant::now())); + } else { + state.select(Some(new)); + self.last_char = None; } } else { state.select(Some(value as usize)); self.last_char = Some((c, Instant::now())); } - - return; // Needed to avoid accidentally clearing last_char. + } else { + state.select(Some(value as usize)); + self.last_char = Some((c, Instant::now())); } + + return; // Needed to avoid accidentally clearing last_char. } } 'g' => { diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index aded2b1e..8c9d248e 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -50,33 +50,31 @@ impl Painter { let show_decimal = app_state.app_config_fields.show_cpu_decimal; if app_state.app_config_fields.dedicated_average_row && app_state.app_config_fields.show_average_cpu - { - if let Some((index, avg)) = cpu_data + && let Some((index, avg)) = cpu_data .iter() .find_position(|&datum| matches!(datum.data_type, CpuDataType::Avg)) - { - let (outer, inner, ratio, style) = self.cpu_info(avg, show_decimal); - let [cores_loc, mut avg_loc] = - Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(draw_loc); + { + let (outer, inner, ratio, style) = self.cpu_info(avg, show_decimal); + let [cores_loc, mut avg_loc] = + Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(draw_loc); - // The cores section all have horizontal margin, so to line up with the cores we - // need to add some margin ourselves. - avg_loc.x += 1; - avg_loc.width -= 2; + // The cores section all have horizontal margin, so to line up with the cores we + // need to add some margin ourselves. + avg_loc.x += 1; + avg_loc.width -= 2; - f.render_widget( - PipeGauge::default() - .gauge_style(style) - .label_style(style) - .inner_label(inner) - .start_label(outer) - .ratio(ratio.into()), - avg_loc, - ); - avg_row_count += 1; - avg_index = index; - draw_loc = cores_loc; - } + f.render_widget( + PipeGauge::default() + .gauge_style(style) + .label_style(style) + .inner_label(inner) + .start_label(outer) + .ratio(ratio.into()), + avg_loc, + ); + avg_row_count += 1; + avg_index = index; + draw_loc = cores_loc; } if draw_loc.height > 0 { @@ -119,7 +117,8 @@ impl Painter { let to_divide = REQUIRED_COLUMNS - itx; let num_taken = min( remaining_height, - (row_counter / to_divide) + usize::from(row_counter % to_divide != 0), + (row_counter / to_divide) + + usize::from(!row_counter.is_multiple_of(to_divide)), ); row_counter -= num_taken; let chunk = (&mut gauge_info).take(num_taken); diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index 09adc715..eaa4c064 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -42,12 +42,12 @@ impl Painter { } // Update draw loc in widget map - if app_state.should_get_widget_bounds() { - if let Some(bottom_widget) = app_state.widget_map.get_mut(&widget_id) { - bottom_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); - bottom_widget.bottom_right_corner = - Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); - } + if app_state.should_get_widget_bounds() + && let Some(bottom_widget) = app_state.widget_map.get_mut(&widget_id) + { + bottom_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); + bottom_widget.bottom_right_corner = + Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); } } else { let graph_width = draw_loc.width - legend_width; diff --git a/src/canvas/widgets/disk_io_graph.rs b/src/canvas/widgets/disk_io_graph.rs index 4bac8437..a2357eec 100644 --- a/src/canvas/widgets/disk_io_graph.rs +++ b/src/canvas/widgets/disk_io_graph.rs @@ -93,15 +93,11 @@ impl Painter { // Compute y_max across all visible series. let mut visible_sources: Vec<&_> = Vec::new(); for name in &device_names { - if show_read { - if let Some(data) = read_data.get(*name) { - visible_sources.push(data); - } + if show_read && let Some(data) = read_data.get(*name) { + visible_sources.push(data); } - if show_write { - if let Some(data) = write_data.get(*name) { - visible_sources.push(data); - } + if show_write && let Some(data) = write_data.get(*name) { + visible_sources.push(data); } } let y_max = widget_state.graph.y_max(visible_sources.into_iter(), times); @@ -230,12 +226,12 @@ impl Painter { ); } - if app_state.should_get_widget_bounds() { - if let Some(widget) = app_state.widget_map.get_mut(&widget_id) { - widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); - widget.bottom_right_corner = - Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); - } + if app_state.should_get_widget_bounds() + && let Some(widget) = app_state.widget_map.get_mut(&widget_id) + { + widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); + widget.bottom_right_corner = + Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); } } } diff --git a/src/canvas/widgets/mem_basic.rs b/src/canvas/widgets/mem_basic.rs index 45ae41ff..7b2c300f 100644 --- a/src/canvas/widgets/mem_basic.rs +++ b/src/canvas/widgets/mem_basic.rs @@ -179,12 +179,12 @@ impl Painter { }); // Update draw loc in widget map - if app_state.should_get_widget_bounds() { - if let Some(widget) = app_state.widget_map.get_mut(&widget_id) { - widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); - widget.bottom_right_corner = - Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); - } + if app_state.should_get_widget_bounds() + && let Some(widget) = app_state.widget_map.get_mut(&widget_id) + { + widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); + widget.bottom_right_corner = + Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); } } } diff --git a/src/canvas/widgets/network_basic.rs b/src/canvas/widgets/network_basic.rs index 0ffc6b04..7d02c943 100644 --- a/src/canvas/widgets/network_basic.rs +++ b/src/canvas/widgets/network_basic.rs @@ -278,12 +278,12 @@ impl Painter { } // Update draw loc in widget map - if app_state.should_get_widget_bounds() { - if let Some(widget) = app_state.widget_map.get_mut(&widget_id) { - widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); - widget.bottom_right_corner = - Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); - } + if app_state.should_get_widget_bounds() + && let Some(widget) = app_state.widget_map.get_mut(&widget_id) + { + widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); + widget.bottom_right_corner = + Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); } } } diff --git a/src/canvas/widgets/temperature_graph.rs b/src/canvas/widgets/temperature_graph.rs index f6854dd7..d2c0e2ba 100644 --- a/src/canvas/widgets/temperature_graph.rs +++ b/src/canvas/widgets/temperature_graph.rs @@ -98,12 +98,12 @@ impl Painter { } // Update draw loc in widget map. - if app_state.should_get_widget_bounds() { - if let Some(temperature_graph_widget) = app_state.widget_map.get_mut(&widget_id) { - temperature_graph_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); - temperature_graph_widget.bottom_right_corner = - Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); - } + if app_state.should_get_widget_bounds() + && let Some(temperature_graph_widget) = app_state.widget_map.get_mut(&widget_id) + { + temperature_graph_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); + temperature_graph_widget.bottom_right_corner = + Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); } } } diff --git a/src/collection.rs b/src/collection.rs index 9135b254..334b7d49 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -472,14 +472,14 @@ impl DataCollector { #[inline] fn update_processes(&mut self) { - if self.widgets_to_harvest.use_proc { - if let Ok(mut process_list) = self.get_processes() { - // NB: To avoid duplicate sorts on rerenders/events, we sort the processes by - // PID here. We also want to avoid re-sorting *again* later on - // if we're sorting by PID, since we already did it here! - process_list.sort_unstable_by_key(|p| p.pid); - self.data.list_of_processes = Some(process_list); - } + if self.widgets_to_harvest.use_proc + && let Ok(mut process_list) = self.get_processes() + { + // NB: To avoid duplicate sorts on rerenders/events, we sort the processes by + // PID here. We also want to avoid re-sorting *again* later on + // if we're sorting by PID, since we already did it here! + process_list.sort_unstable_by_key(|p| p.pid); + self.data.list_of_processes = Some(process_list); } } diff --git a/src/collection/amd.rs b/src/collection/amd.rs index 5d097457..910185f0 100644 --- a/src/collection/amd.rs +++ b/src/collection/amd.rs @@ -348,75 +348,75 @@ pub fn get_amd_vecs(widgets_to_harvest: &UsedWidgets, prev_time: Instant) -> Opt .unwrap_or(amd_gpu_marketing::AMDGPU_DEFAULT_NAME.to_string()); if let Some(mem) = get_amd_vram(&device_path) { - if widgets_to_harvest.use_mem { - if let Some(total_bytes) = NonZeroU64::new(mem.total) { - mem_vec.push(( - device_name.clone(), - MemData { - total_bytes, - used_bytes: mem.used, - }, - )); - } + if widgets_to_harvest.use_mem + && let Some(total_bytes) = NonZeroU64::new(mem.total) + { + mem_vec.push(( + device_name.clone(), + MemData { + total_bytes, + used_bytes: mem.used, + }, + )); } total_mem += mem.total } - if widgets_to_harvest.use_proc { - if let Some(procs) = get_amd_fdinfo(&device_path) { - PREV_PROC_DATA.with_borrow_mut(|prev_proc_data| { - let prev_fdinfo = prev_proc_data.entry(device_path).or_default(); - let mut seen_pids = IntHashSet::default(); + if widgets_to_harvest.use_proc + && let Some(procs) = get_amd_fdinfo(&device_path) + { + PREV_PROC_DATA.with_borrow_mut(|prev_proc_data| { + let prev_fdinfo = prev_proc_data.entry(device_path).or_default(); + let mut seen_pids = IntHashSet::default(); - let mut procs_map = IntHashMap::default(); - for (proc_pid, proc_usage) in procs { - seen_pids.insert(proc_pid); - if let Some(prev_usage) = prev_fdinfo.get_mut(&proc_pid) { - // calculate deltas - let gfx_usage = - diff_usage(prev_usage.gfx_usage, proc_usage.gfx_usage, &interval); - let dma_usage = - diff_usage(prev_usage.dma_usage, proc_usage.dma_usage, &interval); - let enc_usage = - diff_usage(prev_usage.enc_usage, proc_usage.enc_usage, &interval); - let dec_usage = - diff_usage(prev_usage.dec_usage, proc_usage.dec_usage, &interval); - let uvd_usage = - diff_usage(prev_usage.uvd_usage, proc_usage.uvd_usage, &interval); - let vcn_usage = - diff_usage(prev_usage.vcn_usage, proc_usage.vcn_usage, &interval); - let vpe_usage = - diff_usage(prev_usage.vpe_usage, proc_usage.vpe_usage, &interval); + let mut procs_map = IntHashMap::default(); + for (proc_pid, proc_usage) in procs { + seen_pids.insert(proc_pid); + if let Some(prev_usage) = prev_fdinfo.get_mut(&proc_pid) { + // calculate deltas + let gfx_usage = + diff_usage(prev_usage.gfx_usage, proc_usage.gfx_usage, &interval); + let dma_usage = + diff_usage(prev_usage.dma_usage, proc_usage.dma_usage, &interval); + let enc_usage = + diff_usage(prev_usage.enc_usage, proc_usage.enc_usage, &interval); + let dec_usage = + diff_usage(prev_usage.dec_usage, proc_usage.dec_usage, &interval); + let uvd_usage = + diff_usage(prev_usage.uvd_usage, proc_usage.uvd_usage, &interval); + let vcn_usage = + diff_usage(prev_usage.vcn_usage, proc_usage.vcn_usage, &interval); + let vpe_usage = + diff_usage(prev_usage.vpe_usage, proc_usage.vpe_usage, &interval); - // combined usage - let gpu_util_wide = gfx_usage - + dma_usage - + enc_usage - + dec_usage - + uvd_usage - + vcn_usage - + vpe_usage; + // combined usage + let gpu_util_wide = gfx_usage + + dma_usage + + enc_usage + + dec_usage + + uvd_usage + + vcn_usage + + vpe_usage; - let gpu_util: u32 = gpu_util_wide.try_into().unwrap_or(0); + let gpu_util: u32 = gpu_util_wide.try_into().unwrap_or(0); - if gpu_util > 0 || proc_usage.vram_usage > 0 { - procs_map.insert(proc_pid, (proc_usage.vram_usage, gpu_util)); - } - - *prev_usage = proc_usage; - } else { - prev_fdinfo.insert(proc_pid, proc_usage); + if gpu_util > 0 || proc_usage.vram_usage > 0 { + procs_map.insert(proc_pid, (proc_usage.vram_usage, gpu_util)); } - } - prev_fdinfo.retain(|k, _| seen_pids.contains(k)); - - if !procs_map.is_empty() { - proc_vec.push(procs_map); + *prev_usage = proc_usage; + } else { + prev_fdinfo.insert(proc_pid, proc_usage); } - }); - } + } + + prev_fdinfo.retain(|k, _| seen_pids.contains(k)); + + if !procs_map.is_empty() { + proc_vec.push(procs_map); + } + }); } } diff --git a/src/collection/disks/unix.rs b/src/collection/disks/unix.rs index 696c29a2..c1d7b641 100644 --- a/src/collection/disks/unix.rs +++ b/src/collection/disks/unix.rs @@ -41,10 +41,10 @@ pub fn get_disk_usage(collector: &DataCollector) -> anyhow::Result anyhow::Result> { // `lines()`. while let Ok(bytes) = reader.read_line(&mut line) { if bytes > 0 { - if let Ok(partition) = Partition::from_str(&line) { - if partition.fs_type().is_physical() { - results.push(partition); - } + if let Ok(partition) = Partition::from_str(&line) + && partition.fs_type().is_physical() + { + results.push(partition); } line.clear(); diff --git a/src/collection/disks/unix/linux/unmounted.rs b/src/collection/disks/unix/linux/unmounted.rs index a117d4c8..0085f1d5 100644 --- a/src/collection/disks/unix/linux/unmounted.rs +++ b/src/collection/disks/unix/linux/unmounted.rs @@ -43,20 +43,19 @@ fn parse_unmounted_disks(mut reader: R, mounted: &HashSet) - let blocks = parts.nth(2).and_then(|b| b.parse::().ok()); let name = parts.next(); - if let (Some(blocks), Some(name)) = (blocks, name) { - if !(mounted.contains(name) + if let (Some(blocks), Some(name)) = (blocks, name) + && !(mounted.contains(name) || name.starts_with("loop") || name.starts_with("ram") || name.starts_with("zram")) - { - disks.push(DiskHarvest { - name: format!("/dev/{name}"), - mount_point: String::new(), - free_space: None, - used_space: None, - total_space: Some(blocks * PARTITION_BLOCK_SIZE), - }); - } + { + disks.push(DiskHarvest { + name: format!("/dev/{name}"), + mount_point: String::new(), + free_space: None, + used_space: None, + total_space: Some(blocks * PARTITION_BLOCK_SIZE), + }); } line.clear(); diff --git a/src/collection/disks/zfs_io_counters.rs b/src/collection/disks/zfs_io_counters.rs index 20c4adba..caf9a436 100644 --- a/src/collection/disks/zfs_io_counters.rs +++ b/src/collection/disks/zfs_io_counters.rs @@ -108,19 +108,17 @@ pub fn zfs_io_stats() -> anyhow::Result> { "nwritten" => { if let Some((_type, val)) = value.trim_start().rsplit_once(' ') + && let Ok(number) = val.parse::() { - if let Ok(number) = val.parse::() { - write = number; - } + write = number; } } "nread" => { if let Some((_type, val)) = value.trim_start().rsplit_once(' ') + && let Ok(number) = val.parse::() { - if let Ok(number) = val.parse::() { - read = number; - } + read = number; } } _ => {} diff --git a/src/collection/linux/cgroups.rs b/src/collection/linux/cgroups.rs index fbabb4f4..0136f423 100644 --- a/src/collection/linux/cgroups.rs +++ b/src/collection/linux/cgroups.rs @@ -13,10 +13,10 @@ fn read_stat_key(path: &str, key: &str) -> Option { // TODO: Maybe check if this is worth it for the files we read. let file = fs::File::open(path).ok()?; for line in std::io::BufReader::new(file).lines().map_while(Result::ok) { - if let Some(rest) = line.strip_prefix(key) { - if let Some(val) = rest.strip_prefix(' ') { - return val.trim().parse().ok(); - } + if let Some(rest) = line.strip_prefix(key) + && let Some(val) = rest.strip_prefix(' ') + { + return val.trim().parse().ok(); } } diff --git a/src/collection/nvidia.rs b/src/collection/nvidia.rs index a17f29e8..f971aa1b 100644 --- a/src/collection/nvidia.rs +++ b/src/collection/nvidia.rs @@ -57,18 +57,17 @@ pub fn get_nvidia_vecs( for i in 0..num_gpu { if let Ok(device) = nvml.device_by_index(i) { if let Ok(name) = device.name() { - if widgets_to_harvest.use_mem { - if let Ok(mem) = device.memory_info() { - if let Some(total_bytes) = NonZeroU64::new(mem.total) { - mem_vec.push(( - name.clone(), - MemData { - total_bytes, - used_bytes: mem.used, - }, - )); - } - } + if widgets_to_harvest.use_mem + && let Ok(mem) = device.memory_info() + && let Some(total_bytes) = NonZeroU64::new(mem.total) + { + mem_vec.push(( + name.clone(), + MemData { + total_bytes, + used_bytes: mem.used, + }, + )); } if (widgets_to_harvest.use_temp || widgets_to_harvest.use_temp_graph) diff --git a/src/collection/temperature/linux.rs b/src/collection/temperature/linux.rs index b7f6acaf..19ac59e0 100644 --- a/src/collection/temperature/linux.rs +++ b/src/collection/temperature/linux.rs @@ -62,24 +62,24 @@ fn get_hwmon_candidates() -> (HashSet, usize) { if let Ok(read_dir) = Path::new("/sys/devices/platform").read_dir() { for entry in read_dir.flatten() { - if entry.file_name().to_string_lossy().starts_with("coretemp.") { - if let Ok(read_dir) = entry.path().join("hwmon").read_dir() { - for entry in read_dir.flatten() { - let path = entry.path(); + if entry.file_name().to_string_lossy().starts_with("coretemp.") + && let Ok(read_dir) = entry.path().join("hwmon").read_dir() + { + for entry in read_dir.flatten() { + let path = entry.path(); - if path.join("temp1_input").exists() { - // It's possible that there are dupes (represented by symlinks) - the - // easy way is to just substitute the parent - // directory and check if the hwmon - // variant exists already in a set. - // - // For more info, see https://github.com/giampaolo/psutil/pull/1822/files - if let Some(child) = path.file_name() { - let to_check_path = Path::new("/sys/class/hwmon").join(child); + if path.join("temp1_input").exists() { + // It's possible that there are dupes (represented by symlinks) - the + // easy way is to just substitute the parent + // directory and check if the hwmon + // variant exists already in a set. + // + // For more info, see https://github.com/giampaolo/psutil/pull/1822/files + if let Some(child) = path.file_name() { + let to_check_path = Path::new("/sys/class/hwmon").join(child); - if !dirs.contains(&to_check_path) { - dirs.insert(path); - } + if !dirs.contains(&to_check_path) { + dirs.insert(path); } } } @@ -326,15 +326,14 @@ fn hwmon_temperatures(filter: &Option, graph_filter: &Option) -> // TODO: It's possible we may want to move the filter check further up to avoid // probing hwmon if not needed? - if Filter::optional_should_keep(filter, &name) - || Filter::optional_should_keep(graph_filter, &name) + if (Filter::optional_should_keep(filter, &name) + || Filter::optional_should_keep(graph_filter, &name)) + && let Ok(temp_celsius) = parse_temp(&temp_path) { - if let Ok(temp_celsius) = parse_temp(&temp_path) { - temperatures.push(TempSensorData { - name, - temperature: Some(temp_celsius), - }); - } + temperatures.push(TempSensorData { + name, + temperature: Some(temp_celsius), + }); } } } diff --git a/src/lib.rs b/src/lib.rs index cd0e5931..affec976 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,63 +158,59 @@ fn create_input_thread( loop { // We don't block. - if let Some(is_terminated) = cancellation_token.try_check() { - if is_terminated { - break; - } + if let Some(is_terminated) = cancellation_token.try_check() + && is_terminated + { + break; } - if let Ok(poll) = poll(Duration::from_millis(20)) { - if poll { - if let Ok(event) = read() { - match event { - Event::Resize(_, _) => { - // TODO: Might want to debounce this in the future, or take into - // account the actual resize values. - // Maybe we want to keep the current implementation in case the - // resize event might not fire... - // not sure. + if let Ok(poll) = poll(Duration::from_millis(20)) + && poll + && let Ok(event) = read() + { + match event { + Event::Resize(_, _) => { + // TODO: Might want to debounce this in the future, or take into + // account the actual resize values. + // Maybe we want to keep the current implementation in case the + // resize event might not fire... + // not sure. - if sender.send(BottomEvent::Resize).is_err() { - break; - } - } - Event::Paste(paste) => { - if sender.send(BottomEvent::PasteEvent(paste)).is_err() { - break; - } - } - Event::Key(key) - if !keys_disabled && key.kind == KeyEventKind::Press => - { - // For now, we only care about key down events. This may change in - // the future. - if sender.send(BottomEvent::KeyInput(key)).is_err() { - break; - } - } - Event::Mouse(mouse) => match mouse.kind { - MouseEventKind::Moved | MouseEventKind::Drag(..) => {} - MouseEventKind::ScrollDown | MouseEventKind::ScrollUp => { - if Instant::now().duration_since(mouse_timer).as_millis() >= 20 - { - if sender.send(BottomEvent::MouseInput(mouse)).is_err() { - break; - } - mouse_timer = Instant::now(); - } - } - _ => { - if sender.send(BottomEvent::MouseInput(mouse)).is_err() { - break; - } - } - }, - Event::Key(_) => {} - Event::FocusGained => {} - Event::FocusLost => {} + if sender.send(BottomEvent::Resize).is_err() { + break; } } + Event::Paste(paste) => { + if sender.send(BottomEvent::PasteEvent(paste)).is_err() { + break; + } + } + Event::Key(key) if !keys_disabled && key.kind == KeyEventKind::Press => { + // For now, we only care about key down events. This may change in + // the future. + if sender.send(BottomEvent::KeyInput(key)).is_err() { + break; + } + } + Event::Mouse(mouse) => match mouse.kind { + MouseEventKind::Moved | MouseEventKind::Drag(..) => {} + MouseEventKind::ScrollDown | MouseEventKind::ScrollUp => { + if Instant::now().duration_since(mouse_timer).as_millis() >= 20 { + if sender.send(BottomEvent::MouseInput(mouse)).is_err() { + break; + } + mouse_timer = Instant::now(); + } + } + _ => { + if sender.send(BottomEvent::MouseInput(mouse)).is_err() { + break; + } + } + }, + Event::Key(_) => {} + Event::FocusGained => {} + Event::FocusLost => {} } } } @@ -258,10 +254,10 @@ fn create_collection_thread( loop { // Check once at the very top... don't block though. - if let Some(is_terminated) = cancellation_token.try_check() { - if is_terminated { - break; - } + if let Some(is_terminated) = cancellation_token.try_check() + && is_terminated + { + break; } if let Ok(message) = control_receiver.try_recv() { @@ -276,10 +272,10 @@ fn create_collection_thread( data_collector.update_data(); // Yet another check to bail if needed... do not block! - if let Some(is_terminated) = cancellation_token.try_check() { - if is_terminated { - break; - } + if let Some(is_terminated) = cancellation_token.try_check() + && is_terminated + { + break; } let event = BottomEvent::Update(Box::from(data_collector.data)); diff --git a/src/options.rs b/src/options.rs index cf538b21..616402ac 100644 --- a/src/options.rs +++ b/src/options.rs @@ -142,13 +142,13 @@ fn get_config_path(override_config_path: Option<&Path>) -> Option { let mut old_home_path = home_path; old_home_path.push(".config/"); old_home_path.push(DEFAULT_CONFIG_FILE_LOCATION); - if let Ok(res) = old_home_path.try_exists() { - if res { - // We used to create it at `/DEFAULT_CONFIG_FILE_PATH`, but changed it - // to be more correct later. However, for legacy reasons, if it already exists, - // use the old one. - return Some(old_home_path); - } + if let Ok(res) = old_home_path.try_exists() + && res + { + // We used to create it at `/DEFAULT_CONFIG_FILE_PATH`, but changed it + // to be more correct later. However, for legacy reasons, if it already exists, + // use the old one. + return Some(old_home_path); } } @@ -157,28 +157,26 @@ fn get_config_path(override_config_path: Option<&Path>) -> Option { path }); - if cfg!(target_os = "macos") { - if let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") { - if !xdg_config_path.is_empty() { - // If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the - // Library-based path for a config and it exists, then use that - // instead for backwards-compatibility. - if let Some(old_macos_path) = &config_path { - if let Ok(res) = old_macos_path.try_exists() { - if res { - return config_path; - } - } - } - - // Otherwise, try and use the XDG_CONFIG_HOME-based path. - let mut cfg_path = PathBuf::new(); - cfg_path.push(xdg_config_path); - cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION); - - return Some(cfg_path); - } + if cfg!(target_os = "macos") + && let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") + && !xdg_config_path.is_empty() + { + // If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the + // Library-based path for a config and it exists, then use that + // instead for backwards-compatibility. + if let Some(old_macos_path) = &config_path + && let Ok(res) = old_macos_path.try_exists() + && res + { + return config_path; } + + // Otherwise, try and use the XDG_CONFIG_HOME-based path. + let mut cfg_path = PathBuf::new(); + cfg_path.push(xdg_config_path); + cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION); + + return Some(cfg_path); } config_path @@ -530,32 +528,32 @@ pub(crate) fn init_app(args: BottomArgs, config: Config) -> Result<(App, BottomL for col_row in &col.children { for widget in &col_row.children { widget_map.insert(widget.widget_id, widget.clone()); - if let Some(default_widget_type) = &default_widget_type_option { - if !is_custom_layout || use_basic_mode { - match widget.widget_type { - BasicCpu => { - if let Cpu = *default_widget_type { - initial_widget_id = widget.widget_id; - initial_widget_type = Cpu; - } + if let Some(default_widget_type) = &default_widget_type_option + && (!is_custom_layout || use_basic_mode) + { + match widget.widget_type { + BasicCpu => { + if let Cpu = *default_widget_type { + initial_widget_id = widget.widget_id; + initial_widget_type = Cpu; } - BasicMem => { - if let Mem = *default_widget_type { - initial_widget_id = widget.widget_id; - initial_widget_type = Cpu; - } + } + BasicMem => { + if let Mem = *default_widget_type { + initial_widget_id = widget.widget_id; + initial_widget_type = Cpu; } - BasicNet => { - if let Net = *default_widget_type { - initial_widget_id = widget.widget_id; - initial_widget_type = Cpu; - } + } + BasicNet => { + if let Net = *default_widget_type { + initial_widget_id = widget.widget_id; + initial_widget_type = Cpu; } - _ => { - if *default_widget_type == widget.widget_type { - initial_widget_id = widget.widget_id; - initial_widget_type = widget.widget_type.clone(); - } + } + _ => { + if *default_widget_type == widget.widget_type { + initial_widget_id = widget.widget_id; + initial_widget_type = widget.widget_type.clone(); } } } @@ -977,10 +975,10 @@ fn get_temperature(args: &BottomArgs, config: &Config) -> OptionResult bool { return false; } else if let Some(cpu) = &config.cpu { return !cpu.hide_avg_cpu.unwrap_or(false); - } else if let Some(flags) = &config.flags { - if let Some(hide) = flags.hide_avg_cpu { - deprecated_warning("hide_avg_cpu", "cpu.hide_avg_cpu"); - return !hide; - } + } else if let Some(flags) = &config.flags + && let Some(hide) = flags.hide_avg_cpu + { + deprecated_warning("hide_avg_cpu", "cpu.hide_avg_cpu"); + return !hide; } true @@ -1102,20 +1100,19 @@ fn get_default_widget_and_count( fn get_use_battery(args: &BottomArgs, config: &Config) -> bool { // TODO: Move this so it's dynamic in the app itself and automatically hide if // there are no batteries? - if let Ok(battery_manager) = Manager::new() { - if let Ok(batteries) = battery_manager.batteries() { - if batteries.count() == 0 { - return false; - } - } + if let Ok(battery_manager) = Manager::new() + && let Ok(batteries) = battery_manager.batteries() + && batteries.count() == 0 + { + return false; } if args.battery.battery { return true; - } else if let Some(flags) = &config.flags { - if let Some(battery) = flags.battery { - return battery; - } + } else if let Some(flags) = &config.flags + && let Some(battery) = flags.battery + { + return battery; } false @@ -1220,13 +1217,13 @@ fn get_network_unit_type(args: &BottomArgs, config: &Config) -> DataUnit { if use_bytes { return DataUnit::Byte; } - } else if let Some(flags) = &config.flags { - if let Some(network_use_bytes) = flags.network_use_bytes { - deprecated_warning("network_use_bytes", "network_graph.use_bytes"); + } else if let Some(flags) = &config.flags + && let Some(network_use_bytes) = flags.network_use_bytes + { + deprecated_warning("network_use_bytes", "network_graph.use_bytes"); - if network_use_bytes { - return DataUnit::Byte; - } + if network_use_bytes { + return DataUnit::Byte; } } @@ -1240,13 +1237,13 @@ fn get_network_scale_type(args: &BottomArgs, config: &Config) -> AxisScaling { if use_log { return AxisScaling::Log; } - } else if let Some(flags) = &config.flags { - if let Some(network_use_log) = flags.network_use_log { - deprecated_warning("network_use_log", "network_graph.use_log"); + } else if let Some(flags) = &config.flags + && let Some(network_use_log) = flags.network_use_log + { + deprecated_warning("network_use_log", "network_graph.use_log"); - if network_use_log { - return AxisScaling::Log; - } + if network_use_log { + return AxisScaling::Log; } } diff --git a/src/options/config/layout.rs b/src/options/config/layout.rs index 99f24581..573d2004 100644 --- a/src/options/config/layout.rs +++ b/src/options/config/layout.rs @@ -86,10 +86,10 @@ impl Row { } } else { // Check default flag - if let Some(default_widget_flag) = widget.default { - if default_widget_flag { - *default_widget_id = *iter_id; - } + if let Some(default_widget_flag) = widget.default + && default_widget_flag + { + *default_widget_id = *iter_id; } } @@ -144,10 +144,10 @@ impl Row { } } else { // Check default flag - if let Some(default_widget_flag) = widget.default { - if default_widget_flag { - *default_widget_id = *iter_id; - } + if let Some(default_widget_flag) = widget.default + && default_widget_flag + { + *default_widget_id = *iter_id; } } diff --git a/src/options/config/process.rs b/src/options/config/process.rs index 46e2a380..a8c511d5 100644 --- a/src/options/config/process.rs +++ b/src/options/config/process.rs @@ -8,11 +8,8 @@ use crate::widgets::ProcColumn; #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))] pub(crate) struct ProcessesConfig { /// A list of process widget columns. - /// - // TODO: make this more composable(?) in the future, we might need to - // rethink how it's done for custom widgets #[serde(default)] - pub columns: Vec, + pub columns: Vec, // TODO: make this more composable(?) in the future, we might need to rethink how it's done for custom widgets /// The default sort column. #[serde(default)] diff --git a/src/options/config/style.rs b/src/options/config/style.rs index 84f4d1f9..bbd1b49c 100644 --- a/src/options/config/style.rs +++ b/src/options/config/style.rs @@ -261,10 +261,10 @@ impl Styles { selected_border_colour ); - if let Some(widgets) = &config.widgets { - if let Some(widget_borders) = widgets.widget_border_type { - self.border_type = widget_borders.into(); - } + if let Some(widgets) = &config.widgets + && let Some(widget_borders) = widgets.widget_border_type + { + self.border_type = widget_borders.into(); } Ok(()) diff --git a/src/utils/input.rs b/src/utils/input.rs index 45cf8913..0f8e9629 100644 --- a/src/utils/input.rs +++ b/src/utils/input.rs @@ -113,11 +113,11 @@ impl InputFieldState { let mut index = 0; for i in 0..(cursor_index + 1) { - if let Some(r) = self.size_mappings.get(&i) { - if r.start + available_width >= cursor_range.end { - index = i; - break; - } + if let Some(r) = self.size_mappings.get(&i) + && r.start + available_width >= cursor_range.end + { + index = i; + break; } } @@ -130,11 +130,11 @@ impl InputFieldState { if cursor_range.start < start_range.end { let mut index = 0; for i in cursor_index..(self.current_query.len()) { - if let Some(r) = self.size_mappings.get(&i) { - if r.start + available_width >= cursor_range.end { - index = i; - break; - } + if let Some(r) = self.size_mappings.get(&i) + && r.start + available_width >= cursor_range.end + { + index = i; + break; } } index diff --git a/src/widgets/process_table.rs b/src/widgets/process_table.rs index e2bcd074..f01dd0ca 100644 --- a/src/widgets/process_table.rs +++ b/src/widgets/process_table.rs @@ -881,38 +881,38 @@ impl ProcWidgetState { } pub fn toggle_mem_percentage(&mut self) { - if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::Mem) { - if let Some(mem) = self.get_mut_proc_col(index) { - match mem { - ProcColumn::MemValue => { - *mem = ProcColumn::MemPercent; - } - ProcColumn::MemPercent => { - *mem = ProcColumn::MemValue; - } - _ => unreachable!(), + if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::Mem) + && let Some(mem) = self.get_mut_proc_col(index) + { + match mem { + ProcColumn::MemValue => { + *mem = ProcColumn::MemPercent; } - - self.sort_table.set_data(self.column_text()); - self.force_data_update(); + ProcColumn::MemPercent => { + *mem = ProcColumn::MemValue; + } + _ => unreachable!(), } + + self.sort_table.set_data(self.column_text()); + self.force_data_update(); } #[cfg(feature = "gpu")] - if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::GpuMem) { - if let Some(mem) = self.get_mut_proc_col(index) { - match mem { - ProcColumn::GpuMemValue => { - *mem = ProcColumn::GpuMemPercent; - } - ProcColumn::GpuMemPercent => { - *mem = ProcColumn::GpuMemValue; - } - _ => unreachable!(), + if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::GpuMem) + && let Some(mem) = self.get_mut_proc_col(index) + { + match mem { + ProcColumn::GpuMemValue => { + *mem = ProcColumn::GpuMemPercent; } - - self.sort_table.set_data(self.column_text()); - self.force_data_update(); + ProcColumn::GpuMemPercent => { + *mem = ProcColumn::GpuMemValue; + } + _ => unreachable!(), } + + self.sort_table.set_data(self.column_text()); + self.force_data_update(); } } @@ -938,24 +938,24 @@ impl ProcWidgetState { /// Marks the selected column as hidden, and automatically resets the /// selected column to the default sort index and order. fn hide_column(&mut self, column: ProcWidgetColumn) { - if let Some(index) = self.column_mapping.get_index_of(&column) { - if let Some(col) = self.table.columns.get_mut(index) { - col.set_hidden(true); + if let Some(index) = self.column_mapping.get_index_of(&column) + && let Some(col) = self.table.columns.get_mut(index) + { + col.set_hidden(true); - if self.table.sort_index() == index { - self.table.set_sort_index(self.default_sort_index); - self.table.set_order(self.default_sort_order); - } + if self.table.sort_index() == index { + self.table.set_sort_index(self.default_sort_index); + self.table.set_order(self.default_sort_order); } } } /// Marks the selected column as shown. fn show_column(&mut self, column: ProcWidgetColumn) { - if let Some(index) = self.column_mapping.get_index_of(&column) { - if let Some(col) = self.table.columns.get_mut(index) { - col.set_hidden(false); - } + if let Some(index) = self.column_mapping.get_index_of(&column) + && let Some(col) = self.table.columns.get_mut(index) + { + col.set_hidden(false); } } @@ -969,29 +969,29 @@ impl ProcWidgetState { } pub fn collapse_current_tree_branch_entry(&mut self) { - if let ProcWidgetMode::Tree(collapsed) = &mut self.mode { - if let Some(process) = self.table.current_item() { - collapsed.collapse(process.pid); - self.force_data_update(); - } + if let ProcWidgetMode::Tree(collapsed) = &mut self.mode + && let Some(process) = self.table.current_item() + { + collapsed.collapse(process.pid); + self.force_data_update(); } } pub fn expand_current_tree_branch_entry(&mut self) { - if let ProcWidgetMode::Tree(collapsed) = &mut self.mode { - if let Some(process) = self.table.current_item() { - collapsed.expand(process.pid); - self.force_data_update(); - } + if let ProcWidgetMode::Tree(collapsed) = &mut self.mode + && let Some(process) = self.table.current_item() + { + collapsed.expand(process.pid); + self.force_data_update(); } } pub fn toggle_current_tree_branch_entry(&mut self) { - if let ProcWidgetMode::Tree(collapsed) = &mut self.mode { - if let Some(process) = self.table.current_item() { - collapsed.toggle(process.pid); - self.force_data_update(); - } + if let ProcWidgetMode::Tree(collapsed) = &mut self.mode + && let Some(process) = self.table.current_item() + { + collapsed.toggle(process.pid); + self.force_data_update(); } } @@ -999,30 +999,29 @@ impl ProcWidgetState { if let Some(index) = self .column_mapping .get_index_of(&ProcWidgetColumn::ProcNameOrCommand) + && let Some(col) = self.table.columns.get_mut(index) { - if let Some(col) = self.table.columns.get_mut(index) { - let inner = col.inner_mut(); - match inner { - ProcColumn::Name => { - *inner = ProcColumn::Command; - if let ColumnWidthBounds::Soft { max_percentage, .. } = col.bounds_mut() { - *max_percentage = Some(0.5); - } + let inner = col.inner_mut(); + match inner { + ProcColumn::Name => { + *inner = ProcColumn::Command; + if let ColumnWidthBounds::Soft { max_percentage, .. } = col.bounds_mut() { + *max_percentage = Some(0.5); } - ProcColumn::Command => { - *inner = ProcColumn::Name; - if let ColumnWidthBounds::Soft { max_percentage, .. } = col.bounds_mut() { - *max_percentage = match self.mode { - ProcWidgetMode::Tree { .. } => Some(0.5), - ProcWidgetMode::Grouped | ProcWidgetMode::Normal => Some(0.3), - }; - } - } - _ => unreachable!(), } - self.sort_table.set_data(self.column_text()); - self.force_rerender_and_update(); + ProcColumn::Command => { + *inner = ProcColumn::Name; + if let ColumnWidthBounds::Soft { max_percentage, .. } = col.bounds_mut() { + *max_percentage = match self.mode { + ProcWidgetMode::Tree { .. } => Some(0.5), + ProcWidgetMode::Grouped | ProcWidgetMode::Normal => Some(0.3), + }; + } + } + _ => unreachable!(), } + self.sort_table.set_data(self.column_text()); + self.force_rerender_and_update(); } } @@ -1038,37 +1037,35 @@ impl ProcWidgetState { /// State columns should be re-enabled, and the mode switched to /// [`ProcWidgetMode::Normal`]. pub fn toggle_tab(&mut self) { - if !matches!(self.mode, ProcWidgetMode::Tree { .. }) { - if let Some(index) = self + if !matches!(self.mode, ProcWidgetMode::Tree { .. }) + && let Some(index) = self .column_mapping .get_index_of(&ProcWidgetColumn::PidOrCount) - { - if let Some(sort_col) = self.table.columns.get_mut(index) { - let col = sort_col.inner_mut(); - match col { - ProcColumn::Pid => { - *col = ProcColumn::Count; - sort_col.default_order = SortOrder::Descending; + && let Some(sort_col) = self.table.columns.get_mut(index) + { + let col = sort_col.inner_mut(); + match col { + ProcColumn::Pid => { + *col = ProcColumn::Count; + sort_col.default_order = SortOrder::Descending; - self.hide_column(ProcWidgetColumn::User); - self.hide_column(ProcWidgetColumn::State); - self.mode = ProcWidgetMode::Grouped; - } - ProcColumn::Count => { - *col = ProcColumn::Pid; - sort_col.default_order = SortOrder::Ascending; - - self.show_column(ProcWidgetColumn::User); - self.show_column(ProcWidgetColumn::State); - self.mode = ProcWidgetMode::Normal; - } - _ => unreachable!(), - } - - self.sort_table.set_data(self.column_text()); - self.force_rerender_and_update(); + self.hide_column(ProcWidgetColumn::User); + self.hide_column(ProcWidgetColumn::State); + self.mode = ProcWidgetMode::Grouped; } + ProcColumn::Count => { + *col = ProcColumn::Pid; + sort_col.default_order = SortOrder::Ascending; + + self.show_column(ProcWidgetColumn::User); + self.show_column(ProcWidgetColumn::State); + self.mode = ProcWidgetMode::Normal; + } + _ => unreachable!(), } + + self.sort_table.set_data(self.column_text()); + self.force_rerender_and_update(); } } diff --git a/src/widgets/process_table/query/prefix.rs b/src/widgets/process_table/query/prefix.rs index 10ce87f1..9c4f273e 100644 --- a/src/widgets/process_table/query/prefix.rs +++ b/src/widgets/process_table/query/prefix.rs @@ -392,36 +392,36 @@ impl QueryProcessor for Prefix { } } - if let Some(condition) = condition { - if let Some(read_value) = value { - // Note that the values *might* have a unit or need to be parsed - // differently based on the - // prefix type! + if let Some(condition) = condition + && let Some(read_value) = value + { + // Note that the values *might* have a unit or need to be parsed + // differently based on the + // prefix type! - // TODO: Support this without spaces? + // TODO: Support this without spaces? - let mut value = read_value; + let mut value = read_value; - match prefix_type { - PrefixType::MemBytes - | PrefixType::ReadPerSecond - | PrefixType::WritePerSecond - | PrefixType::TotalRead - | PrefixType::TotalWrite => { - process_prefix_units(query, &mut value); - } - #[cfg(feature = "gpu")] - PrefixType::GpuMemoryBytes => { - process_prefix_units(query, &mut value); - } - _ => {} + match prefix_type { + PrefixType::MemBytes + | PrefixType::ReadPerSecond + | PrefixType::WritePerSecond + | PrefixType::TotalRead + | PrefixType::TotalWrite => { + process_prefix_units(query, &mut value); } - - return Ok(Prefix::Attribute(new_numerical_attribute( - prefix_type, - NumericalQuery { condition, value }, - )?)); + #[cfg(feature = "gpu")] + PrefixType::GpuMemoryBytes => { + process_prefix_units(query, &mut value); + } + _ => {} } + + return Ok(Prefix::Attribute(new_numerical_attribute( + prefix_type, + NumericalQuery { condition, value }, + )?)); } } }