From c0a8c347e12ae7924ed78ee9c35f2acd7152193e Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 18 Nov 2020 20:00:31 -0500 Subject: [PATCH] bug: remove buggy auto-generated CPU colour implementation (#308) Removes the random automatically generated colours for the CPU metrics. This was not supported in all terminal emulators, and would cause some of them to break (namely macOS Terminal). Instead we'll default to colours we can be more certain will work and loop through them as required. Users can still override these colours with their own. --- CHANGELOG.md | 2 + src/canvas.rs | 1 - src/canvas/canvas_colours.rs | 21 +++----- src/canvas/canvas_colours/colour_utils.rs | 60 ++++------------------- src/canvas/widgets/cpu_graph.rs | 15 +++--- src/constants.rs | 2 - 6 files changed, 27 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c25a651..9ddeff40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#296](https://github.com/ClementTsang/bottom/pull/296): Removes an accidental extra comma in one of the headers in the disk widget. +- [#308](https://github.com/ClementTsang/bottom/pull/308): Removes the automatically generated CPU colours method. + ## [0.4.7] - 2020-08-26 ### Bug Fixes diff --git a/src/canvas.rs b/src/canvas.rs index 7af29f33..9bd4f96d 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -197,7 +197,6 @@ impl Painter { } else { painter.generate_colour_scheme(colour_scheme)?; } - painter.colours.generate_remaining_cpu_colours(); painter.complete_painter_init(); Ok(painter) diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_colours.rs index a42eb88c..fd8d4eb0 100644 --- a/src/canvas/canvas_colours.rs +++ b/src/canvas/canvas_colours.rs @@ -1,4 +1,4 @@ -use crate::{constants::*, options::ConfigColours, utils::error}; +use crate::{options::ConfigColours, utils::error}; use anyhow::Context; use colour_utils::*; use tui::style::{Color, Style}; @@ -48,7 +48,7 @@ impl Default for CanvasColours { total_tx_style: Style::default().fg(STANDARD_FOURTH_COLOUR), all_colour_style: Style::default().fg(ALL_COLOUR), avg_colour_style: Style::default().fg(AVG_COLOUR), - cpu_colour_styles: Vec::new(), + cpu_colour_styles: colour_utils::get_default_cpu_colours(), border_style: Style::default().fg(text_colour), highlighted_border_style: Style::default().fg(STANDARD_HIGHLIGHT_COLOUR), text_style: Style::default().fg(text_colour), @@ -248,22 +248,13 @@ impl CanvasColours { } pub fn set_cpu_colours(&mut self, colours: &[String]) -> error::Result<()> { - let max_amount = std::cmp::min(colours.len(), NUM_COLOURS); - for (itx, colour) in colours.iter().enumerate() { - if itx >= max_amount { - break; - } - self.cpu_colour_styles.push(get_style_from_config(colour)?); - } + self.cpu_colour_styles = colours + .iter() + .map(|colour| get_style_from_config(colour)) + .collect::>>()?; Ok(()) } - pub fn generate_remaining_cpu_colours(&mut self) { - let remaining_num_colours = NUM_COLOURS.saturating_sub(self.cpu_colour_styles.len()); - self.cpu_colour_styles - .extend(gen_n_styles(remaining_num_colours)); - } - pub fn set_scroll_entry_text_color(&mut self, colour: &str) -> error::Result<()> { self.currently_selected_text_colour = get_colour_from_config(colour)?; self.currently_selected_text_style = Style::default() diff --git a/src/canvas/canvas_colours/colour_utils.rs b/src/canvas/canvas_colours/colour_utils.rs index d104d483..048f9317 100644 --- a/src/canvas/canvas_colours/colour_utils.rs +++ b/src/canvas/canvas_colours/colour_utils.rs @@ -3,9 +3,8 @@ use std::collections::HashMap; use tui::style::{Color, Style}; -use crate::utils::{error, gen_util::*}; +use crate::utils::error; -const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long) pub const STANDARD_FIRST_COLOUR: Color = Color::LightMagenta; pub const STANDARD_SECOND_COLOUR: Color = Color::LightYellow; @@ -41,60 +40,21 @@ lazy_static! { .collect(); } -/// Generates random colours. Strategy found from -/// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ -pub fn gen_n_styles(num_to_gen: usize) -> Vec