diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-04-06 23:04:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-06 23:04:04 -0400 |
commit | 9127cb14686563101d77a6170d6a75e30981c1d2 (patch) | |
tree | 3cfc41bdb7f1a9494da4f1231f67e0f41ffcdee9 /src/canvas/widgets/cpu_basic.rs | |
parent | 0a63ee46ef872ab9b2e25c193f09c60748b29394 (diff) |
refactor/bug: Array bound checking, fix CPU jump
Diffstat (limited to 'src/canvas/widgets/cpu_basic.rs')
-rw-r--r-- | src/canvas/widgets/cpu_basic.rs | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index 3f674b93..5f5655d6 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -97,34 +97,38 @@ impl CpuBasicWidget for Painter { let mut row_counter = num_cpus; let mut start_index = 0; for (itx, chunk) in chunks.iter().enumerate() { - let to_divide = REQUIRED_COLUMNS - itx; - let how_many_cpus = min( - remaining_height, - (row_counter / to_divide) + (if row_counter % to_divide == 0 { 0 } else { 1 }), - ); - row_counter -= how_many_cpus; - let end_index = min(start_index + how_many_cpus, num_cpus); - let cpu_column: Vec<Text<'_>> = (start_index..end_index) - .map(|cpu_index| { - Text::Styled( - (&cpu_bars[cpu_index]).into(), - self.colours.cpu_colour_styles - [cpu_index as usize % self.colours.cpu_colour_styles.len()], - ) - }) - .collect::<Vec<_>>(); - - start_index += how_many_cpus; - - let margined_loc = Layout::default() - .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(100)].as_ref()) - .horizontal_margin(1) - .split(*chunk); - - Paragraph::new(cpu_column.iter()) - .block(Block::default()) - .render(f, margined_loc[0]); + // Explicitly check... don't want an accidental DBZ or underflow + if REQUIRED_COLUMNS > itx { + let to_divide = REQUIRED_COLUMNS - itx; + let how_many_cpus = min( + remaining_height, + (row_counter / to_divide) + + (if row_counter % to_divide == 0 { 0 } else { 1 }), + ); + row_counter -= how_many_cpus; + let end_index = min(start_index + how_many_cpus, num_cpus); + let cpu_column: Vec<Text<'_>> = (start_index..end_index) + .map(|cpu_index| { + Text::Styled( + (&cpu_bars[cpu_index]).into(), + self.colours.cpu_colour_styles + [cpu_index as usize % self.colours.cpu_colour_styles.len()], + ) + }) + .collect::<Vec<_>>(); + + start_index += how_many_cpus; + + let margined_loc = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(100)].as_ref()) + .horizontal_margin(1) + .split(*chunk); + + Paragraph::new(cpu_column.iter()) + .block(Block::default()) + .render(f, margined_loc[0]); + } } } } |