Support average cpu colour separately from others. Redid documentation.

This commit is contained in:
ClementTsang 2020-02-23 00:30:00 -05:00
parent b3f61b25ae
commit a756c44c66
6 changed files with 69 additions and 34 deletions

View file

@ -14,19 +14,24 @@ One use of a config file is to set boot flags to execute without having to state
Another use is to set colours, under the `[colors]`. The following labels are customizable with hex colour code strings: Another use is to set colours, under the `[colors]`. The following labels are customizable with hex colour code strings:
- Table header colours (`table_header_color="#ffffff"`). | Labels | Details | Example |
- Every CPU core colour as an array (`cpu_core_colors=["#ffffff", "#000000", "#111111"]`). | ------------------------------- | ---------------------------------------------- | --------------------------------------------------- |
- bottom will look at 216 (let's be realistic here) colours at most, and in order. | Table header colours | Colour of table headers | `table_header_color="#ffffff"` |
- If not enough colours are provided for the number of threads on the CPU, then the rest will be automatically generated. | CPU colour per core | Colour of each core. Read in order. | `cpu_core_colors=["#ffffff", "#000000", "#111111"]` |
- RAM and SWAP colours (`ram_color="#ffffff"`, `swap_color="#111111"`). | | Looks at 256 colours at most. |
- RX and TX colours (`rx_color="#ffffff"`, `tx_color="#111111"`). | Average CPU colour | The average CPU color | `avg_cpu_color="#d3869b"` |
- Widget title colour (`widget_title_color="#ffffff"`). | RAM | The colour RAM will use | `ram_color="#ffffff"` |
- General widget border colour (`border_color="#ffffff"`). | SWAP | The colour SWAP will use | `swap_color="#111111"` |
- Current widget border colour (`highlighted_border_color="#ffffff"`). | RX | The colour rx will use | `rx_color="#ffffff"` |
- Text colour (`text_color="#ffffff"`). | TX | The colour tx will use | `tx_color="#111111"` |
- Label and graph colour (`graph_color="#ffffff"`). | Widget title colour | The colour of the label each widget has | `widget_title_color="#ffffff"` |
- Cursor colour (`cursor_color="#ffffff"`). | Border colour | The colour of the border of unselected widgets | `border_color="#ffffff"` |
- Current selected scroll entry colour (`scroll_entry_text_color="#282828"`, `scroll_entry_bg_color="#458588"`). | Selected border colour | The colour of the border of selected widgets | `highlighted_border_color="#ffffff"` |
| Text colour | The colour of most text | `text_color="#ffffff"` |
| Graph colour | The colour of the lines and text of the graph | `graph_color="#ffffff"` |
| Cursor colour | The cursor's colour | `cursor_color="#ffffff"` |
| Selected text colour | The colour of text that is selected | `scroll_entry_text_color="#282828"` |
| Selected text background colour | The background colour of text that is selected | `scroll_entry_bg_color="#458588"` |
Note some colours may not be compatible with the terminal you are using. For example, macOS's default Terminal does not play nice with many colours. Note some colours may not be compatible with the terminal you are using. For example, macOS's default Terminal does not play nice with many colours.

View file

@ -5,7 +5,7 @@
# This group of options represents a command-line flag/option. Flags explicitly # This group of options represents a command-line flag/option. Flags explicitly
# added when running (ie: btm -a) will override this config file if an option # added when running (ie: btm -a) will override this config file if an option
# is also set here. # is also set here.
#[flags] [flags]
#avg_cpu = true #avg_cpu = true
#dot_marker = false #dot_marker = false
@ -41,7 +41,7 @@
# macOS default Terminal does NOT like custom colours and it will glitch out. # macOS default Terminal does NOT like custom colours and it will glitch out.
# #
# The default options here are based on gruvbox: https://github.com/morhetz/gruvbox # The default options here are based on gruvbox: https://github.com/morhetz/gruvbox
#[colors] [colors]
# Represents the colour of table headers (processes, CPU, disks, temperature). # Represents the colour of table headers (processes, CPU, disks, temperature).
#table_header_color="#458588" #table_header_color="#458588"
@ -49,6 +49,9 @@
# Represents the colour of the label each widget has. # Represents the colour of the label each widget has.
#widget_title_color="#cc241d" #widget_title_color="#cc241d"
# Represents the average CPU color
#avg_cpu_color="#d3869b"
# Represents the colour the core will use in the CPU legend and graph. # Represents the colour the core will use in the CPU legend and graph.
#cpu_core_colors=["#cc241d", "#98971a"] #cpu_core_colors=["#cc241d", "#98971a"]
@ -70,7 +73,7 @@
# Represents the colour of the border of selected widgets. # Represents the colour of the border of selected widgets.
#highlighted_border_color="#fe8019" #highlighted_border_color="#fe8019"
# Represents the colour of the border of most text that is otherwise not covered. # Represents the colour of most text.
#text_color="#ebdbb2" #text_color="#ebdbb2"
# Represents the colour of text that is selected. # Represents the colour of text that is selected.
@ -81,3 +84,6 @@
# Represents the colour of the lines and text of the graph. # Represents the colour of the lines and text of the graph.
#graph_color="#ebdbb2" #graph_color="#ebdbb2"
# Represents the cursor's colour.
#cursor_color="#458588"

View file

@ -562,8 +562,12 @@ impl Painter {
Marker::Braille Marker::Braille
}) })
.style( .style(
self.colours.cpu_colour_styles if app_state.app_config_fields.show_average_cpu && itx == 0 {
[itx % self.colours.cpu_colour_styles.len()], self.colours.avg_colour_style
} else {
self.colours.cpu_colour_styles
[itx % self.colours.cpu_colour_styles.len()]
},
) )
.data(&cpu.cpu_data[..]), .data(&cpu.cpu_data[..]),
) )
@ -668,6 +672,8 @@ impl Painter {
.current_scroll_position - start_position .current_scroll_position - start_position
{ {
self.colours.currently_selected_text_style self.colours.currently_selected_text_style
} else if app_state.app_config_fields.show_average_cpu && itx == 0 {
self.colours.avg_colour_style
} else { } else {
self.colours.cpu_colour_styles[itx self.colours.cpu_colour_styles[itx
+ start_position as usize + start_position as usize
@ -675,8 +681,13 @@ impl Painter {
} }
} }
_ => { _ => {
self.colours.cpu_colour_styles[itx if app_state.app_config_fields.show_average_cpu && itx == 0 {
+ start_position as usize % self.colours.cpu_colour_styles.len()] self.colours.avg_colour_style
} else {
self.colours.cpu_colour_styles[itx
+ start_position as usize
% self.colours.cpu_colour_styles.len()]
}
} }
}, },
) )

View file

@ -13,6 +13,7 @@ pub struct CanvasColours {
pub swap_style: Style, pub swap_style: Style,
pub rx_style: Style, pub rx_style: Style,
pub tx_style: Style, pub tx_style: Style,
pub avg_colour_style: Style,
pub cpu_colour_styles: Vec<Style>, pub cpu_colour_styles: Vec<Style>,
pub border_style: Style, pub border_style: Style,
pub highlighted_border_style: Style, pub highlighted_border_style: Style,
@ -36,6 +37,7 @@ impl Default for CanvasColours {
swap_style: Style::default().fg(STANDARD_SECOND_COLOUR), swap_style: Style::default().fg(STANDARD_SECOND_COLOUR),
rx_style: Style::default().fg(STANDARD_FIRST_COLOUR), rx_style: Style::default().fg(STANDARD_FIRST_COLOUR),
tx_style: Style::default().fg(STANDARD_SECOND_COLOUR), tx_style: Style::default().fg(STANDARD_SECOND_COLOUR),
avg_colour_style: Style::default().fg(AVG_COLOUR),
cpu_colour_styles: Vec::new(), cpu_colour_styles: Vec::new(),
border_style: Style::default().fg(text_colour), border_style: Style::default().fg(text_colour),
highlighted_border_style: Style::default().fg(Color::LightBlue), highlighted_border_style: Style::default().fg(Color::LightBlue),
@ -48,37 +50,39 @@ impl Default for CanvasColours {
impl CanvasColours { impl CanvasColours {
pub fn set_text_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_text_colour(&mut self, hex: &str) -> error::Result<()> {
self.text_style = Style::default().fg(convert_hex_to_color(hex)?); self.text_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_border_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_border_colour(&mut self, hex: &str) -> error::Result<()> {
self.border_style = Style::default().fg(convert_hex_to_color(hex)?); self.border_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_highlighted_border_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_highlighted_border_colour(&mut self, hex: &str) -> error::Result<()> {
self.highlighted_border_style = Style::default().fg(convert_hex_to_color(hex)?); self.highlighted_border_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_table_header_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_table_header_colour(&mut self, hex: &str) -> error::Result<()> {
self.table_header_style = Style::default() self.table_header_style = get_style_from_hex(hex)?.modifier(Modifier::BOLD);
.fg(convert_hex_to_color(hex)?)
.modifier(Modifier::BOLD);
Ok(()) Ok(())
} }
pub fn set_ram_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_ram_colour(&mut self, hex: &str) -> error::Result<()> {
self.ram_style = Style::default().fg(convert_hex_to_color(hex)?); self.ram_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_swap_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_swap_colour(&mut self, hex: &str) -> error::Result<()> {
self.swap_style = Style::default().fg(convert_hex_to_color(hex)?); self.swap_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_rx_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_rx_colour(&mut self, hex: &str) -> error::Result<()> {
self.rx_style = Style::default().fg(convert_hex_to_color(hex)?); self.rx_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_tx_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_tx_colour(&mut self, hex: &str) -> error::Result<()> {
self.tx_style = Style::default().fg(convert_hex_to_color(hex)?); self.tx_style = get_style_from_hex(hex)?;
Ok(())
}
pub fn set_avg_cpu_colour(&mut self, hex: &str) -> error::Result<()> {
self.avg_colour_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_cpu_colours(&mut self, hex_colours: &[String]) -> error::Result<()> { pub fn set_cpu_colours(&mut self, hex_colours: &[String]) -> error::Result<()> {
@ -114,12 +118,12 @@ impl CanvasColours {
} }
pub fn set_widget_title_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_widget_title_colour(&mut self, hex: &str) -> error::Result<()> {
self.widget_title_style = Style::default().fg(convert_hex_to_color(hex)?); self.widget_title_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
pub fn set_graph_colour(&mut self, hex: &str) -> error::Result<()> { pub fn set_graph_colour(&mut self, hex: &str) -> error::Result<()> {
self.graph_style = Style::default().fg(convert_hex_to_color(hex)?); self.graph_style = get_style_from_hex(hex)?;
Ok(()) Ok(())
} }
} }

View file

@ -4,6 +4,7 @@ use tui::style::{Color, Style};
const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long) 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_FIRST_COLOUR: Color = Color::LightMagenta;
pub const STANDARD_SECOND_COLOUR: Color = Color::LightYellow; pub const STANDARD_SECOND_COLOUR: Color = Color::LightYellow;
pub const AVG_COLOUR: Color = Color::Red;
/// Generates random colours. Strategy found from /// Generates random colours. Strategy found from
/// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ /// https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
@ -43,14 +44,13 @@ pub fn gen_n_styles(num_to_gen: i32) -> Vec<Style> {
Style::default().fg(Color::LightCyan), Style::default().fg(Color::LightCyan),
Style::default().fg(Color::LightGreen), Style::default().fg(Color::LightGreen),
Style::default().fg(Color::LightBlue), Style::default().fg(Color::LightBlue),
Style::default().fg(Color::Red),
Style::default().fg(Color::Cyan), Style::default().fg(Color::Cyan),
Style::default().fg(Color::Green), Style::default().fg(Color::Green),
Style::default().fg(Color::Blue), Style::default().fg(Color::Blue),
]; ];
let mut h: f32 = 0.4; // We don't need random colours... right? let mut h: f32 = 0.4; // We don't need random colours... right?
for _i in 0..(num_to_gen - 10) { for _i in 0..(num_to_gen - 9) {
h = gen_hsv(h); h = gen_hsv(h);
let result = hsv_to_rgb(h, 0.5, 0.95); let result = hsv_to_rgb(h, 0.5, 0.95);
colour_vec.push(Style::default().fg(Color::Rgb(result.0, result.1, result.2))); colour_vec.push(Style::default().fg(Color::Rgb(result.0, result.1, result.2)));
@ -78,3 +78,7 @@ pub fn convert_hex_to_color(hex: &str) -> error::Result<Color> {
let rgb = convert_hex_to_rgb(hex)?; let rgb = convert_hex_to_rgb(hex)?;
Ok(Color::Rgb(rgb.0, rgb.1, rgb.2)) Ok(Color::Rgb(rgb.0, rgb.1, rgb.2))
} }
pub fn get_style_from_hex(hex: &str) -> error::Result<Style> {
Ok(Style::default().fg(convert_hex_to_color(hex)?))
}

View file

@ -82,6 +82,7 @@ struct ConfigFlags {
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
struct ConfigColours { struct ConfigColours {
table_header_color: Option<String>, table_header_color: Option<String>,
avg_cpu_color: Option<String>,
cpu_core_colors: Option<Vec<String>>, cpu_core_colors: Option<Vec<String>>,
ram_color: Option<String>, ram_color: Option<String>,
swap_color: Option<String>, swap_color: Option<String>,
@ -692,6 +693,10 @@ fn generate_config_colours(config: &Config, painter: &mut canvas::Painter) -> er
painter.colours.set_text_colour(text_color)?; painter.colours.set_text_colour(text_color)?;
} }
if let Some(avg_cpu_color) = &colours.avg_cpu_color {
painter.colours.set_avg_cpu_colour(avg_cpu_color)?;
}
if let Some(cpu_core_colors) = &(colours.cpu_core_colors) { if let Some(cpu_core_colors) = &(colours.cpu_core_colors) {
painter.colours.set_cpu_colours(cpu_core_colors)?; painter.colours.set_cpu_colours(cpu_core_colors)?;
} }