refactor: Deal with some cognitive complexity

This commit is contained in:
Clement Tsang 2020-04-06 00:40:55 -04:00 committed by GitHub
parent c2eaaed8b7
commit 0a63ee46ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 98 deletions

View File

@ -1 +0,0 @@
cognitive-complexity-threshold = 35

View File

@ -1620,38 +1620,40 @@ impl App {
} }
} }
} else if self.is_expanded { } else if self.is_expanded {
if self.app_config_fields.left_legend { self.handle_left_expanded_cpu_movement();
if let BottomWidgetType::Cpu = self.current_widget.widget_type { }
if let Some(current_widget) =
self.widget_map.get(&self.current_widget.widget_id) self.reset_multi_tap_keys();
}
fn handle_left_expanded_cpu_movement(&mut self) {
if self.app_config_fields.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
.cpu_state
.widget_states
.get(&self.current_widget.widget_id)
{ {
if let Some(cpu_widget_state) = self if !cpu_widget_state.is_legend_hidden {
.cpu_state if let Some(new_widget_id) = current_widget.left_neighbour {
.widget_states if let Some(new_widget) = self.widget_map.get(&new_widget_id) {
.get(&self.current_widget.widget_id) self.current_widget = new_widget.clone();
{
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) { } else if let BottomWidgetType::CpuLegend = self.current_widget.widget_type {
if let Some(new_widget_id) = current_widget.left_neighbour { if let Some(current_widget) = self.widget_map.get(&self.current_widget.widget_id) {
if let Some(new_widget) = self.widget_map.get(&new_widget_id) { if let Some(new_widget_id) = current_widget.left_neighbour {
self.current_widget = new_widget.clone(); if let Some(new_widget) = self.widget_map.get(&new_widget_id) {
} self.current_widget = new_widget.clone();
} }
} }
} }
} }
self.reset_multi_tap_keys();
} }
pub fn move_widget_selection_right(&mut self) { pub fn move_widget_selection_right(&mut self) {
@ -1719,11 +1721,31 @@ impl App {
} }
} }
} else if self.is_expanded { } else if self.is_expanded {
if self.app_config_fields.left_legend { self.handle_right_expanded_cpu_movement();
if let BottomWidgetType::CpuLegend = self.current_widget.widget_type { }
if let Some(current_widget) =
self.widget_map.get(&self.current_widget.widget_id) self.reset_multi_tap_keys();
{ }
fn handle_right_expanded_cpu_movement(&mut self) {
if self.app_config_fields.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
.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_id) = current_widget.right_neighbour {
if let Some(new_widget) = self.widget_map.get(&new_widget_id) { if let Some(new_widget) = self.widget_map.get(&new_widget_id) {
self.current_widget = new_widget.clone(); self.current_widget = new_widget.clone();
@ -1731,26 +1753,8 @@ impl App {
} }
} }
} }
} 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
.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();
}
}
}
}
}
} }
} }
self.reset_multi_tap_keys();
} }
pub fn move_widget_selection_up(&mut self) { pub fn move_widget_selection_up(&mut self) {

View File

@ -1,7 +1,7 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use crate::{ use crate::{
app::{self, App}, app::{self, App, ProcWidgetState},
canvas::{ canvas::{
drawing_utils::{ drawing_utils::{
get_search_start_position, get_start_position, get_variable_intrinsic_widths, get_search_start_position, get_start_position, get_variable_intrinsic_widths,
@ -18,7 +18,7 @@ use tui::{
widgets::{Block, Borders, Paragraph, Row, Table, Text, Widget}, widgets::{Block, Borders, Paragraph, Row, Table, Text, Widget},
}; };
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::{GraphemeIndices, UnicodeSegmentation};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
pub trait ProcessTableWidget { pub trait ProcessTableWidget {
@ -251,24 +251,74 @@ impl ProcessTableWidget for Painter {
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, draw_border: bool, &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, draw_border: bool,
widget_id: u64, widget_id: u64,
) { ) {
if let Some(proc_widget_state) = fn get_prompt_text<'a>(proc_widget_state: &ProcWidgetState) -> &'a str {
app_state.proc_state.widget_states.get_mut(&(widget_id - 1))
{
let pid_search_text = "Search by PID (Tab for Name): "; let pid_search_text = "Search by PID (Tab for Name): ";
let name_search_text = "Search by Name (Tab for PID): "; let name_search_text = "Search by Name (Tab for PID): ";
let grouped_search_text = "Search by Name: "; let grouped_search_text = "Search by Name: ";
let num_columns = draw_loc.width as usize;
let is_on_widget = widget_id == app_state.current_widget.widget_id; if proc_widget_state.is_grouped {
let chosen_text = if proc_widget_state.is_grouped {
grouped_search_text grouped_search_text
} else if proc_widget_state.process_search_state.is_searching_with_pid { } else if proc_widget_state.process_search_state.is_searching_with_pid {
pid_search_text pid_search_text
} else { } else {
name_search_text name_search_text
}; }
}
fn build_query<'a>(
is_on_widget: bool, grapheme_indices: GraphemeIndices<'a>, start_position: usize,
cursor_position: usize, query: &str, currently_selected_text_style: tui::style::Style,
text_style: tui::style::Style,
) -> Vec<Text<'a>> {
let mut current_grapheme_posn = 0;
if is_on_widget {
let mut res = grapheme_indices
.filter_map(|grapheme| {
current_grapheme_posn += UnicodeWidthStr::width(grapheme.1);
if current_grapheme_posn <= start_position {
None
} else {
let styled = if grapheme.0 == cursor_position {
Text::styled(grapheme.1, currently_selected_text_style)
} else {
Text::styled(grapheme.1, text_style)
};
Some(styled)
}
})
.collect::<Vec<_>>();
if cursor_position >= query.len() {
res.push(Text::styled(" ", currently_selected_text_style))
}
res
} else {
// This is easier - we just need to get a range of graphemes, rather than
// dealing with possibly inserting a cursor (as none is shown!)
grapheme_indices
.filter_map(|grapheme| {
current_grapheme_posn += UnicodeWidthStr::width(grapheme.1);
if current_grapheme_posn <= start_position {
None
} else {
let styled = Text::styled(grapheme.1, text_style);
Some(styled)
}
})
.collect::<Vec<_>>()
}
}
if let Some(proc_widget_state) =
app_state.proc_state.widget_states.get_mut(&(widget_id - 1))
{
let chosen_text = get_prompt_text(&proc_widget_state);
let is_on_widget = widget_id == app_state.current_widget.widget_id;
let num_columns = draw_loc.width as usize;
let small_mode = chosen_text.len() != min(num_columns / 2, chosen_text.len()); let small_mode = chosen_text.len() != min(num_columns / 2, chosen_text.len());
let search_title: &str = if !small_mode { let search_title: &str = if !small_mode {
chosen_text chosen_text
@ -303,48 +353,15 @@ impl ProcessTableWidget for Painter {
let query = proc_widget_state.get_current_search_query().as_str(); let query = proc_widget_state.get_current_search_query().as_str();
let grapheme_indices = UnicodeSegmentation::grapheme_indices(query, true); let grapheme_indices = UnicodeSegmentation::grapheme_indices(query, true);
let mut current_grapheme_posn = 0; let query_with_cursor: Vec<Text<'_>> = build_query(
let query_with_cursor: Vec<Text<'_>> = if is_on_widget { is_on_widget,
let mut res = grapheme_indices grapheme_indices,
.filter_map(|grapheme| { start_position,
current_grapheme_posn += UnicodeWidthStr::width(grapheme.1); cursor_position,
query,
if current_grapheme_posn <= start_position { self.colours.currently_selected_text_style,
None self.colours.text_style,
} else { );
let styled = if grapheme.0 == cursor_position {
Text::styled(grapheme.1, self.colours.currently_selected_text_style)
} else {
Text::styled(grapheme.1, self.colours.text_style)
};
Some(styled)
}
})
.collect::<Vec<_>>();
if cursor_position >= query.len() {
res.push(Text::styled(
" ",
self.colours.currently_selected_text_style,
))
}
res
} else {
// This is easier - we just need to get a range of graphemes, rather than
// dealing with possibly inserting a cursor (as none is shown!)
grapheme_indices
.filter_map(|grapheme| {
current_grapheme_posn += UnicodeWidthStr::width(grapheme.1);
if current_grapheme_posn <= start_position {
None
} else {
let styled = Text::styled(grapheme.1, self.colours.text_style);
Some(styled)
}
})
.collect::<Vec<_>>()
};
// Text options shamelessly stolen from VS Code. // Text options shamelessly stolen from VS Code.
let case_style = if !proc_widget_state.process_search_state.is_ignoring_case { let case_style = if !proc_widget_state.process_search_state.is_ignoring_case {