From edc61d428c9045982030cf2b54ea20ef8cd5baab Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Sat, 18 Feb 2023 00:51:13 -0500 Subject: [PATCH] bug: fix selected text bg colour being wrong if only the fg colour was set (#1021) * rename file to be more generic * fix selected text BG colour being wrong by default * update changelog * add test for bug --- CHANGELOG.md | 2 + src/bin/main.rs | 2 +- src/canvas.rs | 4 +- .../{canvas_colours.rs => canvas_styling.rs} | 92 +++++++++++++------ .../colour_utils.rs | 35 +------ src/components/data_table/styling.rs | 2 +- src/options.rs | 2 +- src/widgets/cpu_graph.rs | 2 +- src/widgets/disk_table.rs | 2 +- src/widgets/process_table.rs | 2 +- src/widgets/temperature_table.rs | 2 +- 11 files changed, 82 insertions(+), 65 deletions(-) rename src/canvas/{canvas_colours.rs => canvas_styling.rs} (83%) rename src/canvas/{canvas_colours => canvas_styling}/colour_utils.rs (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97efef00..2c2ad221 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Bug Fixes +- [#1021](https://github.com/ClementTsang/bottom/pull/1021): Fix selected text background colour being wrong if only the foreground colour was set. + ## Features - [#1016](https://github.com/ClementTsang/bottom/pull/1016): Add support for displaying process usernames on Windows. diff --git a/src/bin/main.rs b/src/bin/main.rs index 328cd7a0..946ca1aa 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -19,7 +19,7 @@ use std::{ use anyhow::{Context, Result}; use bottom::{ - canvas::{self, canvas_colours::CanvasColours}, + canvas::{self, canvas_styling::CanvasColours}, constants::*, data_conversion::*, options::*, diff --git a/src/canvas.rs b/src/canvas.rs index b1eca362..9ac75585 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use canvas_colours::*; +use canvas_styling::*; use itertools::izip; use tui::{ backend::Backend, @@ -21,7 +21,7 @@ use crate::{ utils::error::BottomError, }; -pub mod canvas_colours; +pub mod canvas_styling; mod dialogs; mod drawing_utils; mod widgets; diff --git a/src/canvas/canvas_colours.rs b/src/canvas/canvas_styling.rs similarity index 83% rename from src/canvas/canvas_colours.rs rename to src/canvas/canvas_styling.rs index e7996a32..e0de7285 100644 --- a/src/canvas/canvas_colours.rs +++ b/src/canvas/canvas_styling.rs @@ -43,11 +43,15 @@ pub struct CanvasColours { impl Default for CanvasColours { fn default() -> Self { let text_colour = Color::Gray; + let currently_selected_text_colour = Color::Black; + let currently_selected_bg_colour = HIGHLIGHT_COLOUR; CanvasColours { - currently_selected_text_colour: Color::Black, - currently_selected_bg_colour: Color::Cyan, - currently_selected_text_style: Style::default().fg(Color::Black).bg(HIGHLIGHT_COLOUR), + currently_selected_text_colour, + currently_selected_bg_colour, + currently_selected_text_style: Style::default() + .fg(currently_selected_text_colour) + .bg(currently_selected_bg_colour), table_header_style: Style::default().fg(HIGHLIGHT_COLOUR), ram_style: Style::default().fg(FIRST_COLOUR), swap_style: Style::default().fg(SECOND_COLOUR), @@ -240,95 +244,95 @@ impl CanvasColours { } pub fn set_disabled_text_colour(&mut self, colour: &str) -> error::Result<()> { - self.disabled_text_style = get_style_from_config(colour)?; + self.disabled_text_style = str_to_fg(colour)?; Ok(()) } pub fn set_text_colour(&mut self, colour: &str) -> error::Result<()> { - self.text_style = get_style_from_config(colour)?; + self.text_style = str_to_fg(colour)?; Ok(()) } pub fn set_border_colour(&mut self, colour: &str) -> error::Result<()> { - self.border_style = get_style_from_config(colour)?; + self.border_style = str_to_fg(colour)?; Ok(()) } pub fn set_highlighted_border_colour(&mut self, colour: &str) -> error::Result<()> { - self.highlighted_border_style = get_style_from_config(colour)?; + self.highlighted_border_style = str_to_fg(colour)?; Ok(()) } pub fn set_table_header_colour(&mut self, colour: &str) -> error::Result<()> { - self.table_header_style = get_style_from_config(colour)?; + self.table_header_style = str_to_fg(colour)?; // Disabled as it seems to be bugged when I go into full command mode...? It becomes huge lol // self.table_header_style = get_style_from_config(colour)?.modifier(Modifier::BOLD); Ok(()) } pub fn set_ram_colour(&mut self, colour: &str) -> error::Result<()> { - self.ram_style = get_style_from_config(colour)?; + self.ram_style = str_to_fg(colour)?; Ok(()) } pub fn set_swap_colour(&mut self, colour: &str) -> error::Result<()> { - self.swap_style = get_style_from_config(colour)?; + self.swap_style = str_to_fg(colour)?; Ok(()) } pub fn set_arc_colour(&mut self, colour: &str) -> error::Result<()> { - self.arc_style = get_style_from_config(colour)?; + self.arc_style = str_to_fg(colour)?; Ok(()) } pub fn set_gpu_colours(&mut self, colours: &[Cow<'static, str>]) -> error::Result<()> { self.gpu_colour_styles = colours .iter() - .map(|colour| get_style_from_config(colour)) + .map(|colour| str_to_fg(colour)) .collect::>>()?; Ok(()) } pub fn set_rx_colour(&mut self, colour: &str) -> error::Result<()> { - self.rx_style = get_style_from_config(colour)?; + self.rx_style = str_to_fg(colour)?; Ok(()) } pub fn set_tx_colour(&mut self, colour: &str) -> error::Result<()> { - self.tx_style = get_style_from_config(colour)?; + self.tx_style = str_to_fg(colour)?; Ok(()) } pub fn set_rx_total_colour(&mut self, colour: &str) -> error::Result<()> { - self.total_rx_style = get_style_from_config(colour)?; + self.total_rx_style = str_to_fg(colour)?; Ok(()) } pub fn set_tx_total_colour(&mut self, colour: &str) -> error::Result<()> { - self.total_tx_style = get_style_from_config(colour)?; + self.total_tx_style = str_to_fg(colour)?; Ok(()) } pub fn set_avg_cpu_colour(&mut self, colour: &str) -> error::Result<()> { - self.avg_colour_style = get_style_from_config(colour)?; + self.avg_colour_style = str_to_fg(colour)?; Ok(()) } pub fn set_all_cpu_colour(&mut self, colour: &str) -> error::Result<()> { - self.all_colour_style = get_style_from_config(colour)?; + self.all_colour_style = str_to_fg(colour)?; Ok(()) } pub fn set_cpu_colours(&mut self, colours: &[Cow<'static, str>]) -> error::Result<()> { self.cpu_colour_styles = colours .iter() - .map(|colour| get_style_from_config(colour)) + .map(|colour| str_to_fg(colour)) .collect::>>()?; Ok(()) } 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_colour = str_to_colour(colour)?; self.currently_selected_text_style = Style::default() .fg(self.currently_selected_text_colour) .bg(self.currently_selected_bg_colour); @@ -336,7 +340,7 @@ impl CanvasColours { } pub fn set_scroll_entry_bg_color(&mut self, colour: &str) -> error::Result<()> { - self.currently_selected_bg_colour = get_colour_from_config(colour)?; + self.currently_selected_bg_colour = str_to_colour(colour)?; self.currently_selected_text_style = Style::default() .fg(self.currently_selected_text_colour) .bg(self.currently_selected_bg_colour); @@ -344,27 +348,63 @@ impl CanvasColours { } pub fn set_widget_title_colour(&mut self, colour: &str) -> error::Result<()> { - self.widget_title_style = get_style_from_config(colour)?; + self.widget_title_style = str_to_fg(colour)?; Ok(()) } pub fn set_graph_colour(&mut self, colour: &str) -> error::Result<()> { - self.graph_style = get_style_from_config(colour)?; + self.graph_style = str_to_fg(colour)?; Ok(()) } pub fn set_high_battery_color(&mut self, colour: &str) -> error::Result<()> { - self.high_battery_colour = get_style_from_config(colour)?; + self.high_battery_colour = str_to_fg(colour)?; Ok(()) } pub fn set_medium_battery_color(&mut self, colour: &str) -> error::Result<()> { - self.medium_battery_colour = get_style_from_config(colour)?; + self.medium_battery_colour = str_to_fg(colour)?; Ok(()) } pub fn set_low_battery_color(&mut self, colour: &str) -> error::Result<()> { - self.low_battery_colour = get_style_from_config(colour)?; + self.low_battery_colour = str_to_fg(colour)?; Ok(()) } } + +#[cfg(test)] +mod test { + + use tui::style::{Color, Style}; + + use super::CanvasColours; + + #[test] + fn default_selected_colour_works() { + let mut colours = CanvasColours::default(); + + assert_eq!( + colours.currently_selected_text_style, + Style::default() + .fg(colours.currently_selected_text_colour) + .bg(colours.currently_selected_bg_colour), + ); + + colours.set_scroll_entry_text_color("red").unwrap(); + + assert_eq!( + colours.currently_selected_text_style, + Style::default() + .fg(Color::Red) + .bg(colours.currently_selected_bg_colour), + ); + + colours.set_scroll_entry_bg_color("magenta").unwrap(); + + assert_eq!( + colours.currently_selected_text_style, + Style::default().fg(Color::Red).bg(Color::Magenta), + ); + } +} diff --git a/src/canvas/canvas_colours/colour_utils.rs b/src/canvas/canvas_styling/colour_utils.rs similarity index 86% rename from src/canvas/canvas_colours/colour_utils.rs rename to src/canvas/canvas_styling/colour_utils.rs index c5c4e294..89b1b170 100644 --- a/src/canvas/canvas_colours/colour_utils.rs +++ b/src/canvas/canvas_styling/colour_utils.rs @@ -10,7 +10,7 @@ pub const HIGHLIGHT_COLOUR: Color = Color::LightBlue; pub const AVG_COLOUR: Color = Color::Red; pub const ALL_COLOUR: Color = Color::Green; -pub fn convert_hex_to_color(hex: &str) -> error::Result { +fn convert_hex_to_color(hex: &str) -> error::Result { fn hex_err(hex: &str) -> error::Result { Err( error::BottomError::ConfigError(format!( @@ -47,26 +47,13 @@ pub fn convert_hex_to_color(hex: &str) -> error::Result { Ok(Color::Rgb(rgb.0, rgb.1, rgb.2)) } -pub fn get_style_from_config(input_val: &str) -> error::Result