Skip to content

Commit

Permalink
bug: Fix inverted battery colours (#331)
Browse files Browse the repository at this point in the history
Fixes colour theming for batteries being flipped.
  • Loading branch information
ClementTsang authored Nov 27, 2020
1 parent 380571c commit 41b8dd6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.3] - 2020-11-26

## Bug Fixes

- [#331](https://github.com/ClementTsang/bottom/pull/331): Fixes custom battery colour levels being inverted.

## [0.5.2] - 2020-11-25

## Bug Fixes
Expand Down
35 changes: 9 additions & 26 deletions src/canvas/canvas_colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ pub struct CanvasColours {
pub text_style: Style,
pub widget_title_style: Style,
pub graph_style: Style,
// Full, Medium, Low
pub battery_bar_styles: Vec<Style>,
pub high_battery_colour: Style,
pub medium_battery_colour: Style,
pub low_battery_colour: Style,
pub invalid_query_style: Style,
pub disabled_text_style: Style,
}
Expand Down Expand Up @@ -65,18 +66,9 @@ impl Default for CanvasColours {
text_style: Style::default().fg(text_colour),
widget_title_style: Style::default().fg(text_colour),
graph_style: Style::default().fg(text_colour),
battery_bar_styles: vec![
Style::default().fg(Color::Red),
Style::default().fg(Color::Yellow),
Style::default().fg(Color::Yellow),
Style::default().fg(Color::Yellow),
Style::default().fg(Color::Green),
Style::default().fg(Color::Green),
Style::default().fg(Color::Green),
Style::default().fg(Color::Green),
Style::default().fg(Color::Green),
Style::default().fg(Color::Green),
],
high_battery_colour: Style::default().fg(Color::Green),
medium_battery_colour: Style::default().fg(Color::Yellow),
low_battery_colour: Style::default().fg(Color::Red),
invalid_query_style: Style::default().fg(tui::style::Color::Red),
disabled_text_style: Style::default().fg(Color::DarkGray),
}
Expand Down Expand Up @@ -293,26 +285,17 @@ impl CanvasColours {
}

pub fn set_high_battery_color(&mut self, colour: &str) -> error::Result<()> {
let style = get_style_from_config(colour)?;
self.battery_bar_styles[0] = style;
self.battery_bar_styles[1] = style;
self.battery_bar_styles[2] = style;
self.battery_bar_styles[3] = style;
self.battery_bar_styles[4] = style;
self.battery_bar_styles[5] = style;
self.high_battery_colour = get_style_from_config(colour)?;
Ok(())
}

pub fn set_medium_battery_color(&mut self, colour: &str) -> error::Result<()> {
let style = get_style_from_config(colour)?;
self.battery_bar_styles[6] = style;
self.battery_bar_styles[7] = style;
self.battery_bar_styles[8] = style;
self.medium_battery_colour = get_style_from_config(colour)?;
Ok(())
}

pub fn set_low_battery_color(&mut self, colour: &str) -> error::Result<()> {
self.battery_bar_styles[9] = get_style_from_config(colour)?;
self.low_battery_colour = get_style_from_config(colour)?;
Ok(())
}
}
19 changes: 6 additions & 13 deletions src/canvas/widgets/battery_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,15 @@ impl BatteryDisplayWidget for Painter {
["Health %", &battery_details.health],
];

let battery_rows = battery_items.iter().enumerate().map(|(itx, item)| {
let battery_rows = battery_items.iter().map(|item| {
Row::StyledData(
item.iter(),
if itx == 0 {
let colour_index = ((charge_percentage
* self.colours.battery_bar_styles.len() as f64)
/ 100.0)
.ceil() as usize
- 1;
*self
.colours
.battery_bar_styles
.get(colour_index)
.unwrap_or(&self.colours.text_style)
if charge_percentage < 10.0 {
self.colours.low_battery_colour
} else if charge_percentage < 50.0 {
self.colours.medium_battery_colour
} else {
self.colours.text_style
self.colours.high_battery_colour
},
)
});
Expand Down

0 comments on commit 41b8dd6

Please sign in to comment.