| 1 | //! Config options around styling. |
| 2 | |
| 3 | mod battery; |
| 4 | mod borders; |
| 5 | mod cpu; |
| 6 | mod disk_io_graph; |
| 7 | mod graphs; |
| 8 | mod memory; |
| 9 | mod network; |
| 10 | mod tables; |
| 11 | mod temp_graph; |
| 12 | mod themes; |
| 13 | mod utils; |
| 14 | mod widgets; |
| 15 | |
| 16 | use std::borrow::Cow; |
| 17 | |
| 18 | use battery::BatteryStyle; |
| 19 | use cpu::CpuStyle; |
| 20 | use disk_io_graph::DiskIoGraphStyle; |
| 21 | use graphs::GraphStyle; |
| 22 | use memory::MemoryStyle; |
| 23 | use network::NetworkStyle; |
| 24 | use ratatui::{style::Style, widgets::BorderType}; |
| 25 | use serde::{Deserialize, Serialize}; |
| 26 | use tables::TableStyle; |
| 27 | use temp_graph::TempGraphStyle; |
| 28 | use utils::{opt, set_colour, set_colour_list, set_style}; |
| 29 | use widgets::WidgetStyle; |
| 30 | |
| 31 | use super::Config; |
| 32 | use crate::options::{ |
| 33 | OptionError, OptionResult, args::BottomArgs, config::style::utils::set_bg_colour, |
| 34 | }; |
| 35 | |
| 36 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 37 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 38 | #[cfg_attr(test, derive(PartialEq, Eq))] |
| 39 | pub(crate) struct ColourStr(Cow<'static, str>); |
| 40 | |
| 41 | /// A style for text. |
| 42 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 43 | #[serde(untagged)] |
| 44 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 45 | #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))] |
| 46 | pub(crate) enum TextStyleConfig { |
| 47 | Colour(ColourStr), |
| 48 | TextStyle { |
| 49 | /// A built-in ANSI colour, RGB hex, or RGB colour code. |
| 50 | #[serde(alias = "color")] |
| 51 | colour: Option<ColourStr>, |
| 52 | |
| 53 | /// A built-in ANSI colour, RGB hex, or RGB colour code. |
| 54 | #[serde(alias = "bg_color")] |
| 55 | bg_colour: Option<ColourStr>, |
| 56 | |
| 57 | /// Whether to make this text bolded or not. If not set, |
| 58 | /// will default to built-in defaults. |
| 59 | bold: Option<bool>, |
| 60 | |
| 61 | /// Whether to make this text italicized or not. If not set, |
| 62 | /// will default to built-in defaults. |
| 63 | italics: Option<bool>, |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | /// Style-related configs. |
| 68 | #[derive(Clone, Debug, Default, Deserialize, Serialize)] |
| 69 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 70 | #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))] |
| 71 | pub(crate) struct StyleConfig { |
| 72 | /// A built-in theme. |
| 73 | /// |
| 74 | /// If this is and a custom colour are both set, in the config file, |
| 75 | /// the custom colour scheme will be prioritized first. If a theme |
| 76 | /// is set in the command-line args, however, it will always be |
| 77 | /// prioritized first. |
| 78 | pub(crate) theme: Option<Cow<'static, str>>, |
| 79 | |
| 80 | /// Styling for the CPU widget. |
| 81 | pub(crate) cpu: Option<CpuStyle>, |
| 82 | |
| 83 | /// Styling for the memory widget. |
| 84 | pub(crate) memory: Option<MemoryStyle>, |
| 85 | |
| 86 | /// Styling for the network widget. |
| 87 | pub(crate) network: Option<NetworkStyle>, |
| 88 | |
| 89 | /// Styling for the disk I/O graph widget. |
| 90 | pub(crate) disk_io_graph: Option<DiskIoGraphStyle>, |
| 91 | |
| 92 | /// Styling for the temperature graph widget. |
| 93 | pub(crate) temp_graph: Option<TempGraphStyle>, |
| 94 | |
| 95 | /// Styling for the battery widget. |
| 96 | pub(crate) battery: Option<BatteryStyle>, |
| 97 | |
| 98 | /// Styling for table widgets. |
| 99 | pub(crate) tables: Option<TableStyle>, |
| 100 | |
| 101 | /// Styling for graph widgets. |
| 102 | pub(crate) graphs: Option<GraphStyle>, |
| 103 | |
| 104 | /// Styling for general widgets. |
| 105 | pub(crate) widgets: Option<WidgetStyle>, |
| 106 | } |
| 107 | |
| 108 | /// The actual internal representation of the configured styles. |
| 109 | #[derive(Debug)] |
| 110 | pub struct Styles { |
| 111 | pub(crate) ram_style: Style, |
| 112 | #[cfg(not(target_os = "windows"))] |
| 113 | pub(crate) cache_style: Style, |
| 114 | pub(crate) swap_style: Style, |
| 115 | #[cfg(feature = "zfs")] |
| 116 | pub(crate) arc_style: Style, |
| 117 | #[cfg(feature = "gpu")] |
| 118 | pub(crate) gpu_colours: Vec<Style>, |
| 119 | pub(crate) rx_style: Style, |
| 120 | pub(crate) tx_style: Style, |
| 121 | pub(crate) total_rx_style: Style, |
| 122 | pub(crate) total_tx_style: Style, |
| 123 | pub(crate) all_cpu_colour: Style, |
| 124 | pub(crate) avg_cpu_colour: Style, |
| 125 | pub(crate) cpu_colour_styles: Vec<Style>, |
| 126 | pub(crate) temp_graph_colour_styles: Vec<Style>, |
| 127 | pub(crate) disk_io_read_colour_styles: Vec<Style>, |
| 128 | pub(crate) disk_io_write_colour_styles: Vec<Style>, |
| 129 | pub(crate) border_style: Style, |
| 130 | pub(crate) highlighted_border_style: Style, |
| 131 | pub(crate) text_style: Style, |
| 132 | pub(crate) selected_text_style: Style, |
| 133 | pub(crate) table_header_style: Style, |
| 134 | pub(crate) widget_title_style: Style, |
| 135 | pub(crate) general_widget_style: Style, |
| 136 | pub(crate) graph_style: Style, |
| 137 | pub(crate) graph_legend_style: Style, |
| 138 | pub(crate) high_battery: Style, |
| 139 | pub(crate) medium_battery: Style, |
| 140 | pub(crate) low_battery: Style, |
| 141 | pub(crate) invalid_query_style: Style, |
| 142 | pub(crate) disabled_text_style: Style, |
| 143 | #[cfg(target_os = "linux")] |
| 144 | pub(crate) thread_text_style: Style, |
| 145 | pub(crate) border_type: BorderType, |
| 146 | } |
| 147 | |
| 148 | impl Default for Styles { |
| 149 | fn default() -> Self { |
| 150 | Self::default_palette() |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl Styles { |
| 155 | pub fn new(args: &BottomArgs, config: &Config) -> anyhow::Result<Self> { |
| 156 | let mut palette = match &args.style.theme { |
| 157 | Some(theme) => Self::from_theme(theme)?, |
| 158 | None => match config.styles.as_ref().and_then(|s| s.theme.as_ref()) { |
| 159 | Some(theme) => Self::from_theme(theme)?, |
| 160 | None => Self::default(), |
| 161 | }, |
| 162 | }; |
| 163 | |
| 164 | // Apply theme from config on top. |
| 165 | if let Some(config_style) = &config.styles { |
| 166 | palette.set_styles_from_config(config_style)?; |
| 167 | } |
| 168 | |
| 169 | Ok(palette) |
| 170 | } |
| 171 | |
| 172 | fn from_theme(theme: &str) -> anyhow::Result<Self> { |
| 173 | let lower_case = theme.to_lowercase(); |
| 174 | match lower_case.as_str() { |
| 175 | "default" => Ok(Self::default_palette()), |
| 176 | "default-light" => Ok(Self::default_light_palette()), |
| 177 | "gruvbox" => Ok(Self::gruvbox_palette()), |
| 178 | "gruvbox-light" => Ok(Self::gruvbox_light_palette()), |
| 179 | "nord" => Ok(Self::nord_palette()), |
| 180 | "nord-light" => Ok(Self::nord_light_palette()), |
| 181 | _ => Err(OptionError::other(format!( |
| 182 | "'{theme}' is an invalid built-in colour scheme." |
| 183 | )) |
| 184 | .into()), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | fn set_styles_from_config(&mut self, config: &StyleConfig) -> OptionResult<()> { |
| 189 | // CPU |
| 190 | set_colour!(self.avg_cpu_colour, config.cpu, avg_entry_colour); |
| 191 | set_colour!(self.all_cpu_colour, config.cpu, all_entry_colour); |
| 192 | set_colour_list!(self.cpu_colour_styles, config.cpu, cpu_core_colours); |
| 193 | |
| 194 | // Temperature graph |
| 195 | set_colour_list!( |
| 196 | self.temp_graph_colour_styles, |
| 197 | config.temp_graph, |
| 198 | temp_graph_colour_styles |
| 199 | ); |
| 200 | |
| 201 | // Disk I/O graph |
| 202 | set_colour_list!( |
| 203 | self.disk_io_read_colour_styles, |
| 204 | config.disk_io_graph, |
| 205 | read_colours |
| 206 | ); |
| 207 | set_colour_list!( |
| 208 | self.disk_io_write_colour_styles, |
| 209 | config.disk_io_graph, |
| 210 | write_colours |
| 211 | ); |
| 212 | |
| 213 | // Memory |
| 214 | set_colour!(self.ram_style, config.memory, ram_colour); |
| 215 | set_colour!(self.swap_style, config.memory, swap_colour); |
| 216 | |
| 217 | #[cfg(not(target_os = "windows"))] |
| 218 | set_colour!(self.cache_style, config.memory, cache_colour); |
| 219 | |
| 220 | #[cfg(feature = "zfs")] |
| 221 | set_colour!(self.arc_style, config.memory, arc_colour); |
| 222 | |
| 223 | #[cfg(feature = "gpu")] |
| 224 | set_colour_list!(self.gpu_colours, config.memory, gpu_colours); |
| 225 | |
| 226 | // Network |
| 227 | set_colour!(self.rx_style, config.network, rx_colour); |
| 228 | set_colour!(self.tx_style, config.network, tx_colour); |
| 229 | set_colour!(self.total_rx_style, config.network, rx_total_colour); |
| 230 | set_colour!(self.total_tx_style, config.network, tx_total_colour); |
| 231 | |
| 232 | // Battery |
| 233 | set_colour!(self.high_battery, config.battery, high_battery_colour); |
| 234 | set_colour!(self.medium_battery, config.battery, medium_battery_colour); |
| 235 | set_colour!(self.low_battery, config.battery, low_battery_colour); |
| 236 | |
| 237 | // Tables |
| 238 | set_style!(self.table_header_style, config.tables, headers); |
| 239 | |
| 240 | // Widget graphs |
| 241 | set_colour!(self.graph_style, config.graphs, graph_colour); |
| 242 | set_style!(self.graph_legend_style, config.graphs, legend_text); |
| 243 | |
| 244 | // General widget text. |
| 245 | set_bg_colour!(self.general_widget_style, config.widgets, bg_colour); |
| 246 | set_style!(self.widget_title_style, config.widgets, widget_title); |
| 247 | set_style!(self.text_style, config.widgets, text); |
| 248 | set_style!(self.selected_text_style, config.widgets, selected_text); |
| 249 | set_style!(self.disabled_text_style, config.widgets, disabled_text); |
| 250 | |
| 251 | #[cfg(target_os = "linux")] |
| 252 | { |
| 253 | set_style!(self.thread_text_style, config.widgets, thread_text); |
| 254 | } |
| 255 | |
| 256 | // Widget borders |
| 257 | set_colour!(self.border_style, config.widgets, border_colour); |
| 258 | set_colour!( |
| 259 | self.highlighted_border_style, |
| 260 | config.widgets, |
| 261 | selected_border_colour |
| 262 | ); |
| 263 | |
| 264 | if let Some(widgets) = &config.widgets |
| 265 | && let Some(widget_borders) = widgets.widget_border_type |
| 266 | { |
| 267 | self.border_type = widget_borders.into(); |
| 268 | } |
| 269 | |
| 270 | Ok(()) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | #[cfg(test)] |
| 275 | mod test { |
| 276 | |
| 277 | use ratatui::style::{Color, Style}; |
| 278 | |
| 279 | use super::Styles; |
| 280 | use crate::options::config::{Config, style::utils::str_to_colour}; |
| 281 | |
| 282 | #[test] |
| 283 | fn color_spelling_aliases_work() { |
| 284 | // Parse all_styling_color.toml (which uses the "color" alias spelling throughout) |
| 285 | // and assert every field was actually parsed. |
| 286 | let toml_str = include_str!("../../../tests/valid_configs/all_styling_color.toml"); |
| 287 | let config = toml_edit::de::from_str::<Config>(toml_str) |
| 288 | .expect("config file should parse") |
| 289 | .styles |
| 290 | .expect("styles section should be present"); |
| 291 | |
| 292 | let cpu = config.cpu.as_ref().unwrap(); |
| 293 | assert!(cpu.all_entry_colour.is_some()); |
| 294 | assert!(cpu.avg_entry_colour.is_some()); |
| 295 | assert!(cpu.cpu_core_colours.is_some()); |
| 296 | |
| 297 | let temp = config.temp_graph.as_ref().unwrap(); |
| 298 | assert!(temp.temp_graph_colour_styles.is_some()); |
| 299 | |
| 300 | let mem = config.memory.as_ref().unwrap(); |
| 301 | assert!(mem.ram_colour.is_some()); |
| 302 | assert!(mem.cache_colour.is_some()); |
| 303 | assert!(mem.swap_colour.is_some()); |
| 304 | assert!(mem.arc_colour.is_some()); |
| 305 | assert!(mem.gpu_colours.is_some()); |
| 306 | |
| 307 | let net = config.network.as_ref().unwrap(); |
| 308 | assert!(net.rx_colour.is_some()); |
| 309 | assert!(net.tx_colour.is_some()); |
| 310 | assert!(net.rx_total_colour.is_some()); |
| 311 | assert!(net.tx_total_colour.is_some()); |
| 312 | |
| 313 | let bat = config.battery.as_ref().unwrap(); |
| 314 | assert!(bat.high_battery_colour.is_some()); |
| 315 | assert!(bat.medium_battery_colour.is_some()); |
| 316 | assert!(bat.low_battery_colour.is_some()); |
| 317 | |
| 318 | let graphs = config.graphs.as_ref().unwrap(); |
| 319 | assert!(graphs.graph_colour.is_some()); |
| 320 | |
| 321 | let widgets = config.widgets.as_ref().unwrap(); |
| 322 | assert!(widgets.border_colour.is_some()); |
| 323 | assert!(widgets.selected_border_colour.is_some()); |
| 324 | assert!(widgets.bg_colour.is_some()); |
| 325 | } |
| 326 | |
| 327 | #[test] |
| 328 | fn default_selected_colour_works() { |
| 329 | let mut colours = Styles::default(); |
| 330 | let original_selected_text_colour = |
| 331 | Styles::default_palette().selected_text_style.fg.unwrap(); |
| 332 | let original_selected_bg_colour = Styles::default_palette().selected_text_style.bg.unwrap(); |
| 333 | |
| 334 | assert_eq!( |
| 335 | colours.selected_text_style, |
| 336 | Style::default() |
| 337 | .fg(original_selected_text_colour) |
| 338 | .bg(original_selected_bg_colour), |
| 339 | ); |
| 340 | |
| 341 | colours.selected_text_style = colours |
| 342 | .selected_text_style |
| 343 | .fg(str_to_colour("magenta").unwrap()) |
| 344 | .bg(str_to_colour("red").unwrap()); |
| 345 | |
| 346 | assert_eq!( |
| 347 | colours.selected_text_style, |
| 348 | Style::default().fg(Color::Magenta).bg(Color::Red), |
| 349 | ); |
| 350 | } |
| 351 | |
| 352 | #[test] |
| 353 | fn built_in_colour_schemes_work() { |
| 354 | Styles::from_theme("default").unwrap(); |
| 355 | Styles::from_theme("default-light").unwrap(); |
| 356 | Styles::from_theme("gruvbox").unwrap(); |
| 357 | Styles::from_theme("gruvbox-light").unwrap(); |
| 358 | Styles::from_theme("nord").unwrap(); |
| 359 | Styles::from_theme("nord-light").unwrap(); |
| 360 | } |
| 361 | } |