From 31fb2cb1e6dcff000e93403538d8f1b00660d746 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Sat, 1 Oct 2022 23:28:08 +0000 Subject: [PATCH] fix: u16::try_from for text widths --- src/parse_args/mod.rs | 1 + src/ui/draw_blocks.rs | 43 ++++++++++++++++++++++--------------------- src/ui/mod.rs | 4 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/parse_args/mod.rs b/src/parse_args/mod.rs index 6a4e13f..26b0e20 100644 --- a/src/parse_args/mod.rs +++ b/src/parse_args/mod.rs @@ -4,6 +4,7 @@ use clap::Parser; use tracing::error; #[derive(Parser, Debug, Clone, Copy)] +#[allow(clippy::struct_excessive_bools)] // #[command(help_template = FULL_TEMPLATE)] #[command(version, about)] pub struct CliArgs { diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index 2f1a847..f4708c9 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -168,7 +168,7 @@ pub fn containers( format!( "{}{:>width$}", MARGIN, - i.id.chars().take(8).collect::(), + i.id.get().chars().take(8).collect::(), width = &widths.id.1 ), blue, @@ -343,6 +343,7 @@ fn make_chart<'a, T: Stats + Display>( } /// Draw heading bar at top of program, always visible +#[allow(clippy::too_many_lines)] pub fn heading_bar( area: Rect, columns: &Columns, @@ -407,7 +408,7 @@ pub fn heading_bar( width = width - block.2 ), }; - let count = text.chars().count() as u16; + let count = u16::try_from(text.chars().count()).unwrap_or_default(); let status = Paragraph::new(text) .block(block.0) .alignment(Alignment::Left); @@ -437,12 +438,12 @@ pub fn heading_bar( let suffix = if info_visible { "exit" } else { "show" }; let info_text = format!("( h ) {} help {}", suffix, MARGIN); - let info_width = info_text.chars().count() as u16; + let info_width = info_text.chars().count(); - let column_width = area.width - info_width; + let column_width = usize::from(area.width) - info_width; let column_width = if column_width > 0 { column_width } else { 1 }; let splits = if has_containers { - vec![Constraint::Min(column_width), Constraint::Min(info_width)] + vec![Constraint::Min(column_width.try_into().unwrap_or_default()), Constraint::Min(info_width.try_into().unwrap_or_default())] } else { vec![Constraint::Percentage(100)] }; @@ -541,8 +542,8 @@ pub fn help_box(f: &mut Frame<'_, B>) { .border_style(Style::default().fg(Color::Black)); let area = popup( - lines as u16, - max_line_width as u16, + lines, + max_line_width, f.size(), BoxLocation::MiddleCentre, ); @@ -551,9 +552,9 @@ pub fn help_box(f: &mut Frame<'_, B>) { .direction(Direction::Vertical) .constraints( [ - Constraint::Max(NAME_TEXT.lines().count() as u16), - Constraint::Max(description_text.lines().count() as u16), - Constraint::Max(help_text.lines().count() as u16), + Constraint::Max(NAME_TEXT.lines().count().try_into().unwrap_or_default()), + Constraint::Max(description_text.lines().count().try_into().unwrap_or_default()), + Constraint::Max(help_text.lines().count().try_into().unwrap_or_default()), ] .as_ref(), ) @@ -605,8 +606,8 @@ pub fn error(f: &mut Frame<'_, B>, error: AppError, seconds: Option< .alignment(Alignment::Center); let area = popup( - lines as u16, - max_line_width as u16, + lines, + max_line_width, f.size(), BoxLocation::MiddleCentre, ); @@ -634,8 +635,8 @@ pub fn info(f: &mut Frame<'_, B>, text: String) { .alignment(Alignment::Center); let area = popup( - lines as u16, - max_line_width as u16, + lines, + max_line_width, f.size(), BoxLocation::BottomRight, ); @@ -644,21 +645,21 @@ pub fn info(f: &mut Frame<'_, B>, text: String) { } /// draw a box in the one of the BoxLocations, based on max line width + number of lines -fn popup(text_lines: u16, text_width: u16, r: Rect, box_location: BoxLocation) -> Rect { +fn popup(text_lines: usize, text_width: usize, r: Rect, box_location: BoxLocation) -> Rect { // Make sure blank_space can't be an negative, as will crash - let blank_vertical = if r.height > text_lines { - (r.height - text_lines) / 2 + let blank_vertical = if usize::from(r.height) > text_lines { + (usize::from(r.height) - text_lines) / 2 } else { 1 }; - let blank_horizontal = if r.width > text_width { - (r.width - text_width) / 2 + let blank_horizontal = if usize::from(r.width) > text_width { + (usize::from(r.width) - text_width) / 2 } else { 1 }; - let v_constraints = box_location.get_vertical_constraints(blank_vertical, text_lines); - let h_constraints = box_location.get_horizontal_constraints(blank_horizontal, text_width); + let v_constraints = box_location.get_vertical_constraints(blank_vertical.try_into().unwrap_or_default(), text_lines.try_into().unwrap_or_default()); + let h_constraints = box_location.get_horizontal_constraints(blank_horizontal.try_into().unwrap_or_default(), text_width.try_into().unwrap_or_default()); let indexes = box_location.get_indexes(); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e2dc511..257a70e 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -148,7 +148,7 @@ fn ui( ) { // set max height for container section, needs +4 to deal with docker commands list and borders let height = app_data.lock().get_container_len(); - let height = if height < 12 { (height + 4) as u16 } else { 12 }; + let height = if height < 12 { height + 4 } else { 12 }; let column_widths = app_data.lock().get_width(); let has_containers = !app_data.lock().containers.items.is_empty(); @@ -168,7 +168,7 @@ fn ui( // Split into 3, containers+controls, logs, then graphs let upper_main = Layout::default() .direction(Direction::Vertical) - .constraints([Constraint::Max(height as u16), Constraint::Percentage(50)].as_ref()) + .constraints([Constraint::Max(height.try_into().unwrap_or_default()), Constraint::Percentage(50)].as_ref()) .split(whole_layout[1]); let top_split = if has_containers {