diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-05-07 01:09:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-07 01:09:24 -0400 |
commit | a71d991695fb3874d6cb329291cfcfa986e0eee4 (patch) | |
tree | 66512d10ea1ff9d55338048d5a794239ad1152e2 /src | |
parent | 91bb2aa7fc4a008e5d408b95ab83fb96483d36d6 (diff) |
refactor: avoid using max checks, use saturating_sub instead
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 6 | ||||
-rw-r--r-- | src/app/query.rs | 2 | ||||
-rw-r--r-- | src/app/states.rs | 2 | ||||
-rw-r--r-- | src/canvas.rs | 9 | ||||
-rw-r--r-- | src/canvas/dialogs/dd_dialog.rs | 19 | ||||
-rw-r--r-- | src/canvas/dialogs/help_dialog.rs | 8 | ||||
-rw-r--r-- | src/canvas/widgets/basic_table_arrows.rs | 9 | ||||
-rw-r--r-- | src/canvas/widgets/battery_display.rs | 17 | ||||
-rw-r--r-- | src/canvas/widgets/cpu_basic.rs | 12 | ||||
-rw-r--r-- | src/canvas/widgets/cpu_graph.rs | 30 | ||||
-rw-r--r-- | src/canvas/widgets/disk_table.rs | 16 | ||||
-rw-r--r-- | src/canvas/widgets/mem_basic.rs | 4 | ||||
-rw-r--r-- | src/canvas/widgets/mem_graph.rs | 16 | ||||
-rw-r--r-- | src/canvas/widgets/network_graph.rs | 14 | ||||
-rw-r--r-- | src/canvas/widgets/process_table.rs | 30 | ||||
-rw-r--r-- | src/canvas/widgets/temp_table.rs | 17 |
16 files changed, 81 insertions, 130 deletions
@@ -1,4 +1,4 @@ -use std::{cmp::max, collections::HashMap, time::Instant}; +use std::{collections::HashMap, time::Instant}; use unicode_segmentation::GraphemeCursor; use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; @@ -166,7 +166,7 @@ impl App { >= cpu_widget_state.num_cpus_shown as u64 { let new_position = - max(0, cpu_widget_state.num_cpus_shown as i64 - 1) as u64; + cpu_widget_state.num_cpus_shown.saturating_sub(1) as u64; cpu_widget_state.scroll_state.current_scroll_position = new_position; cpu_widget_state.scroll_state.previous_scroll_position = 0; } @@ -184,7 +184,7 @@ impl App { >= cpu_widget_state.num_cpus_shown as u64 { let new_position = - max(0, cpu_widget_state.num_cpus_shown as i64 - 1) as u64; + cpu_widget_state.num_cpus_shown.saturating_sub(1) as u64; cpu_widget_state.scroll_state.current_scroll_position = new_position; cpu_widget_state.scroll_state.previous_scroll_position = 0; } diff --git a/src/app/query.rs b/src/app/query.rs index 863298db..0e98d835 100644 --- a/src/app/query.rs +++ b/src/app/query.rs @@ -412,7 +412,7 @@ impl ProcessQuery for ProcWidgetState { #[derive(Debug)] pub struct Query { - /// Remember, AND > OR, but and must come after or then. + /// Remember, AND > OR, but AND must come after OR when we parse. pub query: And, } diff --git a/src/app/states.rs b/src/app/states.rs index 67ab5a5f..98913e61 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -205,7 +205,7 @@ impl ProcWidgetState { self.process_search_state.search_state.error_message = None; } else { let parsed_query = self.parse_query(); - debug!("PQ: {:#?}", parsed_query); + // debug!("PQ: {:#?}", parsed_query); if let Ok(parsed_query) = parsed_query { self.process_search_state.search_state.query = Some(parsed_query); diff --git a/src/canvas.rs b/src/canvas.rs index a7df9ea9..e141214e 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,5 +1,4 @@ use itertools::izip; -use std::cmp::max; use std::collections::HashMap; use tui::{ @@ -66,7 +65,7 @@ pub struct Painter { layout_constraints: Vec<Vec<Vec<Vec<Constraint>>>>, widget_layout: BottomLayout, derived_widget_draw_locs: Vec<Vec<Vec<Vec<Rect>>>>, - table_height_offset: i64, + table_height_offset: u16, } impl Painter { @@ -150,7 +149,7 @@ impl Painter { layout_constraints, widget_layout, derived_widget_draw_locs: Vec::new(), - table_height_offset: 4 + table_gap as i64, + table_height_offset: 4 + table_gap, } } @@ -209,7 +208,7 @@ impl Painter { terminal.draw(|mut f| { if app_state.help_dialog_state.is_showing_help { let gen_help_len = GENERAL_HELP_TEXT.len() as u16 + 3; - let border_len = (max(0, f.size().height as i64 - gen_help_len as i64)) as u16 / 2; + let border_len = f.size().height.saturating_sub(gen_help_len) / 2; let vertical_dialog_chunk = Layout::default() .direction(Direction::Vertical) .constraints( @@ -245,7 +244,7 @@ impl Painter { self.draw_help_dialog(&mut f, app_state, middle_dialog_chunk[1]); } else if app_state.delete_dialog_state.is_showing_dd { - let bordering = (max(0, f.size().height as i64 - 7) as u16) / 2; + let bordering = f.size().height.saturating_sub(7) / 2; let vertical_dialog_chunk = Layout::default() .direction(Direction::Vertical) .constraints( diff --git a/src/canvas/dialogs/dd_dialog.rs b/src/canvas/dialogs/dd_dialog.rs index 2e952fae..d4b605d6 100644 --- a/src/canvas/dialogs/dd_dialog.rs +++ b/src/canvas/dialogs/dd_dialog.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use tui::{ backend::Backend, layout::{Alignment, Rect}, @@ -61,13 +59,11 @@ impl KillDialog for Painter { }, ]; - let repeat_num = max( - 0, - draw_loc.width as i32 - DD_BASE.chars().count() as i32 - 2, - ); let dd_title = format!( " Confirm Kill Process ─{}─ Esc to close ", - "─".repeat(repeat_num as usize) + "─".repeat( + usize::from(draw_loc.width).saturating_sub(DD_BASE.chars().count() + 2) + ) ); f.render_widget( @@ -102,11 +98,12 @@ impl KillDialog for Painter { dd_err ))]; - let repeat_num = max( - 0, - draw_loc.width as i32 - DD_ERROR_BASE.chars().count() as i32 - 2, + let error_title = format!( + " Error ─{}─ Esc to close ", + "─".repeat( + usize::from(draw_loc.width).saturating_sub(DD_ERROR_BASE.chars().count() + 2) + ) ); - let error_title = format!(" Error ─{}─ Esc to close ", "─".repeat(repeat_num as usize)); f.render_widget( Paragraph::new(dd_text.iter()) diff --git a/src/canvas/dialogs/help_dialog.rs b/src/canvas/dialogs/help_dialog.rs index c75aef10..a11e19e1 100644 --- a/src/canvas/dialogs/help_dialog.rs +++ b/src/canvas/dialogs/help_dialog.rs @@ -1,4 +1,3 @@ -use std::cmp::max; use unicode_width::UnicodeWidthStr; use tui::{ @@ -22,11 +21,10 @@ impl HelpDialog for Painter { fn draw_help_dialog<B: Backend>( &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, ) { - let repeat_num = max( - 0, - draw_loc.width as i32 - HELP_BASE.chars().count() as i32 - 2, + let help_title = format!( + " Help ─{}─ Esc to close ", + "─".repeat(usize::from(draw_loc.width).saturating_sub(HELP_BASE.chars().count() + 2)) ); - let help_title = format!(" Help ─{}─ Esc to close ", "─".repeat(repeat_num as usize)); if app_state.is_force_redraw { // We must also recalculate how many lines are wrapping to properly get scrolling to work on diff --git a/src/canvas/widgets/basic_table_arrows.rs b/src/canvas/widgets/basic_table_arrows.rs index abfb1126..b3a6fcf3 100644 --- a/src/canvas/widgets/basic_table_arrows.rs +++ b/src/canvas/widgets/basic_table_arrows.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use crate::{ app::{ layout_manager::{BottomWidget, BottomWidgetType}, @@ -56,10 +54,9 @@ impl BasicTableArrows for Painter { let left_name = left_table.get_pretty_name(); let right_name = right_table.get_pretty_name(); - let num_spaces = max( - 0, - draw_loc.width as i64 - 2 - 4 - (left_name.len() + right_name.len()) as i64, - ) as usize; + let num_spaces = usize::from(draw_loc.width) + .saturating_sub(6 + left_name.len() + right_name.len()) + as usize; let arrow_text = vec![ Text::Styled(format!("\n◄ {}", left_name).into(), self.colours.text_style), diff --git a/src/canvas/widgets/battery_display.rs b/src/canvas/widgets/battery_display.rs index 8d4c7a5f..943ea9a8 100644 --- a/src/canvas/widgets/battery_display.rs +++ b/src/canvas/widgets/battery_display.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use crate::{ app::App, canvas::{drawing_utils::calculate_basic_use_bars, Painter}, @@ -31,15 +29,12 @@ impl BatteryDisplayWidget for Painter { let is_on_widget = widget_id == app_state.current_widget.widget_id; let title = if app_state.is_expanded { const TITLE_BASE: &str = " Battery ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Battery ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - result_title + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { " Battery ".to_string() }; @@ -74,7 +69,7 @@ impl BatteryDisplayWidget for Painter { .get(battery_widget_state.currently_selected_battery_index) { // Assuming a 50/50 split in width - let bar_length = max(0, (draw_loc.width as i64 - 2) / 2 - 8) as usize; + let bar_length = (draw_loc.width.saturating_sub(2) / 2).saturating_sub(8) as usize; let charge_percentage = battery_details.charge_percentage; let num_bars = calculate_basic_use_bars(charge_percentage, bar_length); let bars = format!( diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index a3299d7f..858b1732 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -1,4 +1,4 @@ -use std::cmp::{max, min}; +use std::cmp::min; use crate::{ app::App, @@ -58,12 +58,10 @@ impl CpuBasicWidget for Painter { .split(draw_loc); // +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds - let margin_space = 2; - let remaining_width = max( - 0, - draw_loc.width as i64 - - ((9 + margin_space) * REQUIRED_COLUMNS - margin_space) as i64, - ) as usize; + const MARGIN_SPACE: usize = 2; + let remaining_width = usize::from(draw_loc.width) + .saturating_sub((9 + MARGIN_SPACE) * REQUIRED_COLUMNS - MARGIN_SPACE) + as usize; let bar_length = remaining_width / REQUIRED_COLUMNS; diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index 1ee9fa5a..73b8a9c0 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -170,14 +170,12 @@ impl CpuGraphWidget for Painter { let title = if app_state.is_expanded && !cpu_widget_state.is_showing_tray { const TITLE_BASE: &str = " CPU ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = - format!(" CPU ─{}─ Esc to go back ", "─".repeat(repeat_num as usize)); - - result_title + format!( + " CPU ─{}─ Esc to go back ", + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { " CPU ".to_string() }; @@ -217,9 +215,8 @@ impl CpuGraphWidget for Painter { cpu_widget_state.is_legend_hidden = false; let cpu_data: &mut [ConvertedCpuData] = &mut app_state.canvas_data.cpu_data; - let num_rows = max(0, i64::from(draw_loc.height) - self.table_height_offset) as u64; let start_position = get_start_position( - num_rows, + draw_loc.height.saturating_sub(self.table_height_offset) as u64, &cpu_widget_state.scroll_state.scroll_direction, &mut cpu_widget_state.scroll_state.previous_scroll_position, cpu_widget_state.scroll_state.current_scroll_position, @@ -304,13 +301,12 @@ impl CpuGraphWidget for Painter { let title = if cpu_widget_state.is_showing_tray { const TITLE_BASE: &str = " Esc to close "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!("{} Esc to close ", "─".repeat(repeat_num as usize)); - - result_title + format!( + "{} Esc to close ", + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { "".to_string() }; diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index fda54d97..a9cabf9b 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -39,9 +39,8 @@ impl DiskTableWidget for Painter { ) { if let Some(disk_widget_state) = app_state.disk_state.widget_states.get_mut(&widget_id) { let disk_data: &mut [Vec<String>] = &mut app_state.canvas_data.disk_data; - let num_rows = max(0, i64::from(draw_loc.height) - self.table_height_offset) as u64; let start_position = get_start_position( - num_rows, + draw_loc.height.saturating_sub(self.table_height_offset) as u64, &disk_widget_state.scroll_state.scroll_direction, &mut disk_widget_state.scroll_state.previous_scroll_position, disk_widget_state.scroll_state.current_scroll_position, @@ -66,15 +65,12 @@ impl DiskTableWidget for Painter { let title = if app_state.is_expanded { const TITLE_BASE: &str = " Disk ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Disk ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - result_title + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else if app_state.app_config_fields.use_basic_mode { String::new() } else { diff --git a/src/canvas/widgets/mem_basic.rs b/src/canvas/widgets/mem_basic.rs index 132fd91d..7c1b749a 100644 --- a/src/canvas/widgets/mem_basic.rs +++ b/src/canvas/widgets/mem_basic.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use crate::{ app::App, canvas::{drawing_utils::*, Painter}, @@ -41,7 +39,7 @@ impl MemBasicWidget for Painter { } // +9 due to 3 + 4 + 2 + 2 columns for the name & space + percentage + bar bounds + margin spacing - let bar_length = max(0, draw_loc.width as i64 - 11) as usize; + let bar_length = draw_loc.width.saturating_sub(11) as usize; let ram_use_percentage = if let Some(mem) = mem_data.last() { mem.1 } else { diff --git a/src/canvas/widgets/mem_graph.rs b/src/canvas/widgets/mem_graph.rs index 697d9688..7224484f 100644 --- a/src/canvas/widgets/mem_graph.rs +++ b/src/canvas/widgets/mem_graph.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use crate::{app::App, canvas::Painter, constants::*}; use tui::{ @@ -93,16 +91,12 @@ impl MemGraphWidget for Painter { let title = if app_state.is_expanded { const TITLE_BASE: &str = " Memory ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Memory ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - - result_title + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { " Memory ".to_string() }; diff --git a/src/canvas/widgets/network_graph.rs b/src/canvas/widgets/network_graph.rs index d4efb3ba..2cc0307f 100644 --- a/src/canvas/widgets/network_graph.rs +++ b/src/canvas/widgets/network_graph.rs @@ -111,16 +111,12 @@ impl NetworkGraphWidget for Painter { let title = if app_state.is_expanded { const TITLE_BASE: &str = " Network ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Network ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - - result_title + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { " Network ".to_string() }; diff --git a/src/canvas/widgets/process_table.rs b/src/canvas/widgets/process_table.rs index 2b1e728f..0be27f71 100644 --- a/src/canvas/widgets/process_table.rs +++ b/src/canvas/widgets/process_table.rs @@ -1,5 +1,3 @@ -use std::cmp::max; - use crate::{ app::{self, App}, canvas::{ @@ -83,11 +81,10 @@ impl ProcessTableWidget for Painter { // hit the process we've currently scrolled to. // We also need to move the list - we can // do so by hiding some elements! - let num_rows = max(0, i64::from(draw_loc.height) - self.table_height_offset) as u64; let is_on_widget = widget_id == app_state.current_widget.widget_id; let position = get_start_position( - num_rows, + draw_loc.height.saturating_sub(self.table_height_offset) as u64, &proc_widget_state.scroll_state.scroll_direction, &mut proc_widget_state.scroll_state.previous_scroll_position, proc_widget_state.scroll_state.current_scroll_position, @@ -96,7 +93,7 @@ impl ProcessTableWidget for Painter { // Sanity check let start_position = if position >= process_data.len() as u64 { - std::cmp::max(0, process_data.len() as i64 - 1) as u64 + process_data.len().saturating_sub(1) as u64 } else { position }; @@ -195,16 +192,13 @@ impl ProcessTableWidget for Painter { .is_enabled { const TITLE_BASE: &str = " Processes ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Processes ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - - result_title + "─".repeat( + usize::from(draw_loc.width) + .saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else { " Processes ".to_string() } @@ -439,11 +433,9 @@ impl ProcessTableWidget for Painter { let title = if draw_border { const TITLE_BASE: &str = " Esc to close "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - format!("{} Esc to close ", "─".repeat(repeat_num as usize)) + let repeat_num = + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2); + format!("{} Esc to close ", "─".repeat(repeat_num)) } else { String::new() }; diff --git a/src/canvas/widgets/temp_table.rs b/src/canvas/widgets/temp_table.rs index 32b35733..69085fca 100644 --- a/src/canvas/widgets/temp_table.rs +++ b/src/canvas/widgets/temp_table.rs @@ -40,9 +40,8 @@ impl TempTableWidget for Painter { if let Some(temp_widget_state) = app_state.temp_state.widget_states.get_mut(&widget_id) { let temp_sensor_data: &mut [Vec<String>] = &mut app_state.canvas_data.temp_sensor_data; - let num_rows = max(0, i64::from(draw_loc.height) - self.table_height_offset) as u64; let start_position = get_start_position( - num_rows, + draw_loc.height.saturating_sub(self.table_height_offset) as u64, &temp_widget_state.scroll_state.scroll_direction, &mut temp_widget_state.scroll_state.previous_scroll_position, temp_widget_state.scroll_state.current_scroll_position, @@ -66,16 +65,12 @@ impl TempTableWidget for Painter { let title = if app_state.is_expanded { const TITLE_BASE: &str = " Temperatures ── Esc to go back "; - let repeat_num = max( - 0, - draw_loc.width as i32 - TITLE_BASE.chars().count() as i32 - 2, - ); - let result_title = format!( + format!( " Temperatures ─{}─ Esc to go back ", - "─".repeat(repeat_num as usize) - ); - - result_title + "─".repeat( + usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2) + ) + ) } else if app_state.app_config_fields.use_basic_mode { String::new() } else { |