| 1 | use concat_string::concat_string; |
| 2 | use ratatui::style::Color; |
| 3 | use unicode_segmentation::UnicodeSegmentation; |
| 4 | |
| 5 | /// Convert a hex string to a colour. |
| 6 | pub(super) fn try_hex_to_colour(hex: &str) -> Result<Color, String> { |
| 7 | fn hex_component_to_int(hex: &str, first: &str, second: &str) -> Result<u8, String> { |
| 8 | u8::from_str_radix(&concat_string!(first, second), 16) |
| 9 | .map_err(|_| format!("'{hex}' is an invalid hex colour, could not decode.")) |
| 10 | } |
| 11 | |
| 12 | fn invalid_hex_format(hex: &str) -> String { |
| 13 | format!( |
| 14 | "'{hex}' is an invalid hex colour. It must be either a 7 character hex string of the form '#12ab3c' or a 3 character hex string of the form '#1a2'.", |
| 15 | ) |
| 16 | } |
| 17 | |
| 18 | if !hex.starts_with('#') { |
| 19 | return Err(invalid_hex_format(hex)); |
| 20 | } |
| 21 | |
| 22 | let components: Vec<&str> = hex.graphemes(true).collect(); |
| 23 | if components.len() == 7 { |
| 24 | // A 6-long hex. |
| 25 | let r = hex_component_to_int(hex, components[1], components[2])?; |
| 26 | let g = hex_component_to_int(hex, components[3], components[4])?; |
| 27 | let b = hex_component_to_int(hex, components[5], components[6])?; |
| 28 | |
| 29 | Ok(Color::Rgb(r, g, b)) |
| 30 | } else if components.len() == 4 { |
| 31 | // A 3-long hex. |
| 32 | let r = hex_component_to_int(hex, components[1], components[1])?; |
| 33 | let g = hex_component_to_int(hex, components[2], components[2])?; |
| 34 | let b = hex_component_to_int(hex, components[3], components[3])?; |
| 35 | |
| 36 | Ok(Color::Rgb(r, g, b)) |
| 37 | } else { |
| 38 | Err(invalid_hex_format(hex)) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub fn str_to_colour(input_val: &str) -> Result<Color, String> { |
| 43 | if input_val.len() > 1 { |
| 44 | if input_val.starts_with('#') { |
| 45 | try_hex_to_colour(input_val) |
| 46 | } else if input_val.contains(',') { |
| 47 | convert_rgb_to_colour(input_val) |
| 48 | } else { |
| 49 | convert_name_to_colour(input_val) |
| 50 | } |
| 51 | } else { |
| 52 | Err(format!("Value '{input_val}' is not valid.",)) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn convert_rgb_to_colour(rgb_str: &str) -> Result<Color, String> { |
| 57 | let rgb_list = rgb_str.split(',').collect::<Vec<&str>>(); |
| 58 | if rgb_list.len() != 3 { |
| 59 | return Err(format!( |
| 60 | "Value '{rgb_str}' is an invalid RGB colour. It must be a comma separated value with 3 integers from 0 to 255 (ie: '255, 0, 155').", |
| 61 | )); |
| 62 | } |
| 63 | |
| 64 | let rgb = rgb_list |
| 65 | .iter() |
| 66 | .filter_map(|val| (*(*val)).to_string().trim().parse::<u8>().ok()) |
| 67 | .collect::<Vec<_>>(); |
| 68 | |
| 69 | if rgb.len() == 3 { |
| 70 | Ok(Color::Rgb(rgb[0], rgb[1], rgb[2])) |
| 71 | } else { |
| 72 | Err(format!( |
| 73 | "Value '{rgb_str}' contained invalid RGB values. It must be a comma separated value with 3 integers from 0 to 255 (ie: '255, 0, 155').", |
| 74 | )) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn convert_name_to_colour(colour_name: &str) -> Result<Color, String> { |
| 79 | match colour_name.to_lowercase().trim() { |
| 80 | "reset" => Ok(Color::Reset), |
| 81 | "black" => Ok(Color::Black), |
| 82 | "red" => Ok(Color::Red), |
| 83 | "green" => Ok(Color::Green), |
| 84 | "yellow" => Ok(Color::Yellow), |
| 85 | "blue" => Ok(Color::Blue), |
| 86 | "magenta" => Ok(Color::Magenta), |
| 87 | "cyan" => Ok(Color::Cyan), |
| 88 | "gray" | "grey" => Ok(Color::Gray), |
| 89 | "darkgray" | "darkgrey" | "dark gray" | "dark grey" => Ok(Color::DarkGray), |
| 90 | "lightred" | "light red" => Ok(Color::LightRed), |
| 91 | "lightgreen" | "light green" => Ok(Color::LightGreen), |
| 92 | "lightyellow" | "light yellow" => Ok(Color::LightYellow), |
| 93 | "lightblue" | "light blue" => Ok(Color::LightBlue), |
| 94 | "lightmagenta" | "light magenta" => Ok(Color::LightMagenta), |
| 95 | "lightcyan" | "light cyan" => Ok(Color::LightCyan), |
| 96 | "white" => Ok(Color::White), |
| 97 | _ => Err(format!( |
| 98 | "'{colour_name}' is an invalid named colour. |
| 99 | |
| 100 | The following are supported named colours: |
| 101 | +--------+-------------+---------------------+ |
| 102 | | Reset | Magenta | Light Yellow | |
| 103 | +--------+-------------+---------------------+ |
| 104 | | Black | Cyan | Light Blue | |
| 105 | +--------+-------------+---------------------+ |
| 106 | | Red | Gray/Grey | Light Magenta | |
| 107 | +--------+-------------+---------------------+ |
| 108 | | Green | Light Cyan | Dark Gray/Dark Grey | |
| 109 | +--------+-------------+---------------------+ |
| 110 | | Yellow | Light Red | White | |
| 111 | +--------+-------------+---------------------+ |
| 112 | | Blue | Light Green | | |
| 113 | +--------+-------------+---------------------+ |
| 114 | |
| 115 | Alternatively, hex colours or RGB colour codes are valid.\n" |
| 116 | )), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | macro_rules! opt { |
| 121 | ($($e: tt)+) => { |
| 122 | (|| { $($e)+ })() |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | macro_rules! set_style { |
| 127 | ($palette_field:expr, $config_location:expr, $field:tt) => { |
| 128 | if let Some(style) = &(opt!($config_location.as_ref()?.$field.as_ref())) { |
| 129 | match &style { |
| 130 | TextStyleConfig::Colour(colour) => { |
| 131 | $palette_field = $palette_field.fg( |
| 132 | crate::options::config::style::utils::str_to_colour(&colour.0).map_err( |
| 133 | |err| match stringify!($config_location).split_once(".") { |
| 134 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 135 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 136 | stringify!($field) |
| 137 | )), |
| 138 | None => crate::options::OptionError::config(format!( |
| 139 | "Please update 'styles.{}' in your config file. {err}", |
| 140 | stringify!($field) |
| 141 | )), |
| 142 | }, |
| 143 | )?, |
| 144 | ); |
| 145 | } |
| 146 | TextStyleConfig::TextStyle { |
| 147 | colour, |
| 148 | bg_colour, |
| 149 | bold, |
| 150 | italics, |
| 151 | } => { |
| 152 | if let Some(fg) = &colour { |
| 153 | $palette_field = $palette_field |
| 154 | .fg(crate::options::config::style::utils::str_to_colour( |
| 155 | &fg.0, |
| 156 | ) |
| 157 | .map_err(|err| { |
| 158 | match stringify!($config_location).split_once(".") { |
| 159 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 160 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 161 | stringify!($field) |
| 162 | )), |
| 163 | None => crate::options::OptionError::config(format!( |
| 164 | "Please update 'styles.{}' in your config file. {err}", |
| 165 | stringify!($field) |
| 166 | )), |
| 167 | } |
| 168 | })?); |
| 169 | } |
| 170 | |
| 171 | if let Some(bg) = &bg_colour { |
| 172 | $palette_field = $palette_field |
| 173 | .bg(crate::options::config::style::utils::str_to_colour( |
| 174 | &bg.0, |
| 175 | ) |
| 176 | .map_err(|err| { |
| 177 | match stringify!($config_location).split_once(".") { |
| 178 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 179 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 180 | stringify!($field) |
| 181 | )), |
| 182 | None => crate::options::OptionError::config(format!( |
| 183 | "Please update 'styles.{}' in your config file. {err}", |
| 184 | stringify!($field) |
| 185 | )), |
| 186 | } |
| 187 | })?); |
| 188 | } |
| 189 | |
| 190 | if let Some(bold) = &bold { |
| 191 | if *bold { |
| 192 | $palette_field = |
| 193 | $palette_field.add_modifier(ratatui::style::Modifier::BOLD); |
| 194 | } else { |
| 195 | $palette_field = |
| 196 | $palette_field.remove_modifier(ratatui::style::Modifier::BOLD); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if let Some(italics) = &italics { |
| 201 | if *italics { |
| 202 | $palette_field = |
| 203 | $palette_field.add_modifier(ratatui::style::Modifier::ITALIC); |
| 204 | } else { |
| 205 | $palette_field = |
| 206 | $palette_field.remove_modifier(ratatui::style::Modifier::ITALIC); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | }; |
| 213 | } |
| 214 | |
| 215 | macro_rules! set_colour { |
| 216 | ($palette_field:expr, $config_location:expr, $field:tt) => { |
| 217 | if let Some(colour) = &(opt!($config_location.as_ref()?.$field.as_ref())) { |
| 218 | $palette_field = $palette_field.fg( |
| 219 | crate::options::config::style::utils::str_to_colour(&colour.0).map_err(|err| { |
| 220 | match stringify!($config_location).split_once(".") { |
| 221 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 222 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 223 | stringify!($field) |
| 224 | )), |
| 225 | None => crate::options::OptionError::config(format!( |
| 226 | "Please update 'styles.{}' in your config file. {err}", |
| 227 | stringify!($field) |
| 228 | )), |
| 229 | } |
| 230 | })?, |
| 231 | ); |
| 232 | } |
| 233 | }; |
| 234 | } |
| 235 | |
| 236 | macro_rules! set_bg_colour { |
| 237 | ($palette_field:expr, $config_location:expr, $field:tt) => { |
| 238 | if let Some(colour) = &(opt!($config_location.as_ref()?.$field.as_ref())) { |
| 239 | $palette_field = $palette_field.bg( |
| 240 | crate::options::config::style::utils::str_to_colour(&colour.0).map_err(|err| { |
| 241 | match stringify!($config_location).split_once(".") { |
| 242 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 243 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 244 | stringify!($field) |
| 245 | )), |
| 246 | None => crate::options::OptionError::config(format!( |
| 247 | "Please update 'styles.{}' in your config file. {err}", |
| 248 | stringify!($field) |
| 249 | )), |
| 250 | } |
| 251 | })?, |
| 252 | ); |
| 253 | } |
| 254 | }; |
| 255 | } |
| 256 | |
| 257 | /// Set `palette_field` to the value in `config_location` for `field`. |
| 258 | macro_rules! set_colour_list { |
| 259 | ($palette_field:expr, $config_location:expr, $field:tt) => { |
| 260 | if let Some(colour_list) = &(opt!($config_location.as_ref()?.$field.as_ref())) { |
| 261 | $palette_field = colour_list |
| 262 | .iter() |
| 263 | .map(|s| { |
| 264 | Ok(Style::default() |
| 265 | .fg(crate::options::config::style::utils::str_to_colour(&s.0)?)) |
| 266 | }) |
| 267 | .collect::<Result<Vec<Style>, String>>() |
| 268 | .map_err(|err| match stringify!($config_location).split_once(".") { |
| 269 | Some((_, loc)) => crate::options::OptionError::config(format!( |
| 270 | "Please update 'styles.{loc}.{}' in your config file. {err}", |
| 271 | stringify!($field) |
| 272 | )), |
| 273 | None => crate::options::OptionError::config(format!( |
| 274 | "Please update 'styles.{}' in your config file. {err}", |
| 275 | stringify!($field) |
| 276 | )), |
| 277 | })?; |
| 278 | } |
| 279 | }; |
| 280 | } |
| 281 | |
| 282 | pub(super) use opt; |
| 283 | pub(super) use set_bg_colour; |
| 284 | pub(super) use set_colour; |
| 285 | pub(super) use set_colour_list; |
| 286 | pub(super) use set_style; |
| 287 | |
| 288 | #[cfg(test)] |
| 289 | mod test { |
| 290 | |
| 291 | use ratatui::style::{Modifier, Style}; |
| 292 | |
| 293 | use super::*; |
| 294 | use crate::options::config::style::{ColourStr, TextStyleConfig}; |
| 295 | |
| 296 | #[test] |
| 297 | fn general_str_to_colour() { |
| 298 | assert_eq!(str_to_colour("red").unwrap(), Color::Red); |
| 299 | assert!(str_to_colour("r ed").is_err()); |
| 300 | |
| 301 | assert_eq!(str_to_colour("#ffffff").unwrap(), Color::Rgb(255, 255, 255)); |
| 302 | assert!(str_to_colour("#fff fff").is_err()); |
| 303 | |
| 304 | assert_eq!( |
| 305 | str_to_colour("255, 255, 255").unwrap(), |
| 306 | Color::Rgb(255, 255, 255) |
| 307 | ); |
| 308 | assert!(str_to_colour("255, 256, 255").is_err()); |
| 309 | } |
| 310 | |
| 311 | #[test] |
| 312 | fn invalid_colour_names() { |
| 313 | // Test invalid spacing in single word. |
| 314 | assert!(convert_name_to_colour("bl ack").is_err()); |
| 315 | |
| 316 | // Test invalid spacing in dual word. |
| 317 | assert!(convert_name_to_colour("darkg ray").is_err()); |
| 318 | |
| 319 | // Test completely invalid colour. |
| 320 | assert!(convert_name_to_colour("darkreset").is_err()); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn valid_colour_names() { |
| 325 | // Standard colour should work |
| 326 | assert_eq!(convert_name_to_colour("red"), Ok(Color::Red)); |
| 327 | |
| 328 | // Capitalizing should be fine. |
| 329 | assert_eq!(convert_name_to_colour("RED"), Ok(Color::Red)); |
| 330 | |
| 331 | // Spacing shouldn't be an issue now. |
| 332 | assert_eq!(convert_name_to_colour(" red "), Ok(Color::Red)); |
| 333 | |
| 334 | // The following are all equivalent. |
| 335 | assert_eq!(convert_name_to_colour("darkgray"), Ok(Color::DarkGray)); |
| 336 | assert_eq!(convert_name_to_colour("darkgrey"), Ok(Color::DarkGray)); |
| 337 | assert_eq!(convert_name_to_colour("dark grey"), Ok(Color::DarkGray)); |
| 338 | assert_eq!(convert_name_to_colour("dark gray"), Ok(Color::DarkGray)); |
| 339 | |
| 340 | assert_eq!(convert_name_to_colour("grey"), Ok(Color::Gray)); |
| 341 | assert_eq!(convert_name_to_colour("gray"), Ok(Color::Gray)); |
| 342 | |
| 343 | // One more test with spacing. |
| 344 | assert_eq!( |
| 345 | convert_name_to_colour(" lightmagenta "), |
| 346 | Ok(Color::LightMagenta) |
| 347 | ); |
| 348 | assert_eq!( |
| 349 | convert_name_to_colour("light magenta"), |
| 350 | Ok(Color::LightMagenta) |
| 351 | ); |
| 352 | assert_eq!( |
| 353 | convert_name_to_colour(" light magenta "), |
| 354 | Ok(Color::LightMagenta) |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn valid_hex_colours() { |
| 360 | // Check hex with 6 characters. |
| 361 | assert_eq!( |
| 362 | try_hex_to_colour("#ffffff").unwrap(), |
| 363 | Color::Rgb(255, 255, 255) |
| 364 | ); |
| 365 | assert_eq!(try_hex_to_colour("#000000").unwrap(), Color::Rgb(0, 0, 0)); |
| 366 | try_hex_to_colour("#111111").unwrap(); |
| 367 | try_hex_to_colour("#11ff11").unwrap(); |
| 368 | try_hex_to_colour("#1f1f1f").unwrap(); |
| 369 | assert_eq!( |
| 370 | try_hex_to_colour("#123abc").unwrap(), |
| 371 | Color::Rgb(18, 58, 188) |
| 372 | ); |
| 373 | |
| 374 | // Check hex with 3 characters. |
| 375 | assert_eq!( |
| 376 | try_hex_to_colour("#fff").unwrap(), |
| 377 | Color::Rgb(255, 255, 255) |
| 378 | ); |
| 379 | assert_eq!(try_hex_to_colour("#000").unwrap(), Color::Rgb(0, 0, 0)); |
| 380 | try_hex_to_colour("#111").unwrap(); |
| 381 | try_hex_to_colour("#1f1").unwrap(); |
| 382 | try_hex_to_colour("#f1f").unwrap(); |
| 383 | try_hex_to_colour("#ff1").unwrap(); |
| 384 | try_hex_to_colour("#1ab").unwrap(); |
| 385 | assert_eq!(try_hex_to_colour("#1ab").unwrap(), Color::Rgb(17, 170, 187)); |
| 386 | } |
| 387 | |
| 388 | #[test] |
| 389 | fn invalid_hex_colours() { |
| 390 | assert!(try_hex_to_colour("ffffff").is_err()); |
| 391 | assert!(try_hex_to_colour("111111").is_err()); |
| 392 | |
| 393 | assert!(try_hex_to_colour("fff").is_err()); |
| 394 | assert!(try_hex_to_colour("111").is_err()); |
| 395 | assert!(try_hex_to_colour("fffffff").is_err()); |
| 396 | assert!(try_hex_to_colour("1234567").is_err()); |
| 397 | |
| 398 | assert!(try_hex_to_colour("#fffffff").is_err()); |
| 399 | assert!(try_hex_to_colour("#1234567").is_err()); |
| 400 | assert!(try_hex_to_colour("#ff").is_err()); |
| 401 | assert!(try_hex_to_colour("#12").is_err()); |
| 402 | assert!(try_hex_to_colour("").is_err()); |
| 403 | |
| 404 | assert!(try_hex_to_colour("#pppppp").is_err()); |
| 405 | assert!(try_hex_to_colour("#00000p").is_err()); |
| 406 | assert!(try_hex_to_colour("#ppp").is_err()); |
| 407 | |
| 408 | assert!(try_hex_to_colour("#一").is_err()); |
| 409 | assert!(try_hex_to_colour("#一二").is_err()); |
| 410 | assert!(try_hex_to_colour("#一二三").is_err()); |
| 411 | assert!(try_hex_to_colour("#一二三四").is_err()); |
| 412 | |
| 413 | assert!(try_hex_to_colour("#f一f").is_err()); |
| 414 | assert!(try_hex_to_colour("#ff一11").is_err()); |
| 415 | |
| 416 | assert!(try_hex_to_colour("#🇨🇦").is_err()); |
| 417 | assert!(try_hex_to_colour("#🇨🇦🇨🇦").is_err()); |
| 418 | assert!(try_hex_to_colour("#🇨🇦🇨🇦🇨🇦").is_err()); |
| 419 | assert!(try_hex_to_colour("#🇨🇦🇨🇦🇨🇦🇨🇦").is_err()); |
| 420 | |
| 421 | assert!(try_hex_to_colour("#हिन्दी").is_err()); |
| 422 | } |
| 423 | |
| 424 | #[test] |
| 425 | fn test_rgb_colours() { |
| 426 | assert_eq!( |
| 427 | convert_rgb_to_colour("0, 0, 0").unwrap(), |
| 428 | Color::Rgb(0, 0, 0) |
| 429 | ); |
| 430 | assert_eq!( |
| 431 | convert_rgb_to_colour("255, 255, 255").unwrap(), |
| 432 | Color::Rgb(255, 255, 255) |
| 433 | ); |
| 434 | assert!(convert_rgb_to_colour("255, 256, 255").is_err()); |
| 435 | assert!(convert_rgb_to_colour("256, 0, 256").is_err()); |
| 436 | assert!(convert_rgb_to_colour("1, -1, 1").is_err()); |
| 437 | assert!(convert_rgb_to_colour("1, -100000, 1").is_err()); |
| 438 | assert!(convert_rgb_to_colour("1, -100000, 100000").is_err()); |
| 439 | } |
| 440 | |
| 441 | struct DummyConfig { |
| 442 | inner: Option<InnerDummyConfig>, |
| 443 | } |
| 444 | |
| 445 | struct InnerDummyConfig { |
| 446 | color_a: Option<ColourStr>, |
| 447 | color_b: Option<ColourStr>, |
| 448 | color_c: Option<ColourStr>, |
| 449 | color_d: Option<ColourStr>, |
| 450 | many_colors: Option<Vec<ColourStr>>, |
| 451 | text_a: Option<TextStyleConfig>, |
| 452 | text_b: Option<TextStyleConfig>, |
| 453 | text_c: Option<TextStyleConfig>, |
| 454 | text_d: Option<TextStyleConfig>, |
| 455 | text_e: Option<TextStyleConfig>, |
| 456 | bad_color: Option<ColourStr>, |
| 457 | bad_list: Option<Vec<ColourStr>>, |
| 458 | bad_text_a: Option<TextStyleConfig>, |
| 459 | bad_text_b: Option<TextStyleConfig>, |
| 460 | } |
| 461 | |
| 462 | impl Default for InnerDummyConfig { |
| 463 | fn default() -> Self { |
| 464 | Self { |
| 465 | color_a: None, |
| 466 | color_b: Some(ColourStr("red".into())), |
| 467 | color_c: Some(ColourStr("255, 255, 255".into())), |
| 468 | color_d: Some(ColourStr("#000000".into())), |
| 469 | many_colors: Some(vec![ColourStr("red".into()), ColourStr("blue".into())]), |
| 470 | text_a: Some(TextStyleConfig::Colour(ColourStr("green".into()))), |
| 471 | text_b: Some(TextStyleConfig::TextStyle { |
| 472 | colour: None, |
| 473 | bg_colour: None, |
| 474 | bold: None, |
| 475 | italics: None, |
| 476 | }), |
| 477 | text_c: Some(TextStyleConfig::TextStyle { |
| 478 | colour: Some(ColourStr("magenta".into())), |
| 479 | bg_colour: Some(ColourStr("255, 255, 255".into())), |
| 480 | bold: Some(true), |
| 481 | italics: Some(false), |
| 482 | }), |
| 483 | text_d: Some(TextStyleConfig::TextStyle { |
| 484 | colour: Some(ColourStr("#fff".into())), |
| 485 | bg_colour: Some(ColourStr("1, 1, 1".into())), |
| 486 | bold: Some(false), |
| 487 | italics: Some(true), |
| 488 | }), |
| 489 | text_e: None, |
| 490 | bad_color: Some(ColourStr("asdf".into())), |
| 491 | bad_list: Some(vec![ |
| 492 | ColourStr("red".into()), |
| 493 | ColourStr("asdf".into()), |
| 494 | ColourStr("ghi".into()), |
| 495 | ]), |
| 496 | bad_text_a: Some(TextStyleConfig::TextStyle { |
| 497 | colour: Some(ColourStr("asdf".into())), |
| 498 | bg_colour: None, |
| 499 | bold: None, |
| 500 | italics: None, |
| 501 | }), |
| 502 | bad_text_b: Some(TextStyleConfig::TextStyle { |
| 503 | colour: None, |
| 504 | bg_colour: Some(ColourStr("asdf".into())), |
| 505 | bold: None, |
| 506 | italics: None, |
| 507 | }), |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | #[test] |
| 513 | fn test_set_colour() -> anyhow::Result<()> { |
| 514 | let mut s = Style::default().fg(Color::Black); |
| 515 | let dummy = DummyConfig { |
| 516 | inner: Some(InnerDummyConfig::default()), |
| 517 | }; |
| 518 | |
| 519 | set_colour!(s, &dummy.inner, color_a); |
| 520 | assert_eq!(s.fg.unwrap(), Color::Black); |
| 521 | assert_eq!(s.bg, None); |
| 522 | |
| 523 | set_colour!(s, &dummy.inner, color_b); |
| 524 | assert_eq!(s.fg.unwrap(), Color::Red); |
| 525 | assert_eq!(s.bg, None); |
| 526 | |
| 527 | set_colour!(s, &dummy.inner, color_c); |
| 528 | assert_eq!(s.fg.unwrap(), Color::Rgb(255, 255, 255)); |
| 529 | assert_eq!(s.bg, None); |
| 530 | |
| 531 | set_colour!(s, &dummy.inner, color_d); |
| 532 | assert_eq!(s.fg.unwrap(), Color::Rgb(0, 0, 0)); |
| 533 | assert_eq!(s.bg, None); |
| 534 | |
| 535 | Ok(()) |
| 536 | } |
| 537 | |
| 538 | #[test] |
| 539 | fn test_set_bg_colour() -> anyhow::Result<()> { |
| 540 | let mut s = Style::default().bg(Color::Black); |
| 541 | let dummy = DummyConfig { |
| 542 | inner: Some(InnerDummyConfig::default()), |
| 543 | }; |
| 544 | |
| 545 | set_bg_colour!(s, &dummy.inner, color_a); |
| 546 | assert_eq!(s.fg, None); |
| 547 | assert_eq!(s.bg.unwrap(), Color::Black); |
| 548 | |
| 549 | set_bg_colour!(s, &dummy.inner, color_b); |
| 550 | assert_eq!(s.fg, None); |
| 551 | assert_eq!(s.bg.unwrap(), Color::Red); |
| 552 | |
| 553 | set_bg_colour!(s, &dummy.inner, color_c); |
| 554 | assert_eq!(s.fg, None); |
| 555 | assert_eq!(s.bg.unwrap(), Color::Rgb(255, 255, 255)); |
| 556 | |
| 557 | set_bg_colour!(s, &dummy.inner, color_d); |
| 558 | assert_eq!(s.fg, None); |
| 559 | assert_eq!(s.bg.unwrap(), Color::Rgb(0, 0, 0)); |
| 560 | |
| 561 | Ok(()) |
| 562 | } |
| 563 | |
| 564 | #[test] |
| 565 | fn test_bad_set_colour() { |
| 566 | let mut _s = Style::default().fg(Color::Black); |
| 567 | let dummy = DummyConfig { |
| 568 | inner: Some(InnerDummyConfig::default()), |
| 569 | }; |
| 570 | |
| 571 | (move || -> anyhow::Result<()> { |
| 572 | set_colour!(_s, &dummy.inner, bad_color); |
| 573 | |
| 574 | Ok(()) |
| 575 | })() |
| 576 | .unwrap_err(); |
| 577 | } |
| 578 | |
| 579 | #[test] |
| 580 | fn test_set_multi_colours() -> anyhow::Result<()> { |
| 581 | let mut s: Vec<Style> = vec![]; |
| 582 | let dummy = DummyConfig { |
| 583 | inner: Some(InnerDummyConfig::default()), |
| 584 | }; |
| 585 | |
| 586 | set_colour_list!(s, &dummy.inner, many_colors); |
| 587 | assert_eq!(s.len(), 2); |
| 588 | assert_eq!(s[0].fg, Some(Color::Red)); |
| 589 | assert_eq!(s[1].fg, Some(Color::Blue)); |
| 590 | |
| 591 | Ok(()) |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn test_bad_set_list() { |
| 596 | let dummy = DummyConfig { |
| 597 | inner: Some(InnerDummyConfig::default()), |
| 598 | }; |
| 599 | |
| 600 | (move || -> anyhow::Result<()> { |
| 601 | let mut _s: Vec<Style>; |
| 602 | set_colour_list!(_s, &dummy.inner, bad_list); |
| 603 | |
| 604 | Ok(()) |
| 605 | })() |
| 606 | .unwrap_err(); |
| 607 | } |
| 608 | |
| 609 | #[test] |
| 610 | fn test_set_style() -> anyhow::Result<()> { |
| 611 | let mut s = Style::default().fg(Color::Black); |
| 612 | let dummy = DummyConfig { |
| 613 | inner: Some(InnerDummyConfig::default()), |
| 614 | }; |
| 615 | |
| 616 | set_style!(s, &dummy.inner, text_e); |
| 617 | assert_eq!(s.fg.unwrap(), Color::Black); |
| 618 | assert_eq!(s.bg, None); |
| 619 | assert!(s.add_modifier.is_empty()); |
| 620 | |
| 621 | set_style!(s, &dummy.inner, text_a); |
| 622 | assert_eq!(s.fg.unwrap(), Color::Green); |
| 623 | assert_eq!(s.bg, None); |
| 624 | |
| 625 | set_style!(s, &dummy.inner, text_b); |
| 626 | assert_eq!(s.fg.unwrap(), Color::Green); |
| 627 | assert_eq!(s.bg, None); |
| 628 | |
| 629 | set_style!(s, &dummy.inner, text_c); |
| 630 | assert_eq!(s.fg.unwrap(), Color::Magenta); |
| 631 | assert_eq!(s.bg.unwrap(), Color::Rgb(255, 255, 255)); |
| 632 | assert!(s.add_modifier.contains(Modifier::BOLD)); |
| 633 | assert!(!s.add_modifier.contains(Modifier::ITALIC)); |
| 634 | |
| 635 | set_style!(s, &dummy.inner, text_d); |
| 636 | assert_eq!(s.fg.unwrap(), Color::Rgb(255, 255, 255)); |
| 637 | assert_eq!(s.bg.unwrap(), Color::Rgb(1, 1, 1)); |
| 638 | assert!(!s.add_modifier.contains(Modifier::BOLD)); |
| 639 | assert!(s.add_modifier.contains(Modifier::ITALIC)); |
| 640 | |
| 641 | Ok(()) |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn test_bad_text_1() { |
| 646 | let mut _s = Style::default().fg(Color::Black); |
| 647 | let dummy = DummyConfig { |
| 648 | inner: Some(InnerDummyConfig::default()), |
| 649 | }; |
| 650 | |
| 651 | (move || -> anyhow::Result<()> { |
| 652 | set_style!(_s, &dummy.inner, bad_text_a); |
| 653 | |
| 654 | Ok(()) |
| 655 | })() |
| 656 | .unwrap_err(); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn test_bad_text_2() { |
| 661 | let mut _s = Style::default().fg(Color::Black); |
| 662 | let dummy = DummyConfig { |
| 663 | inner: Some(InnerDummyConfig::default()), |
| 664 | }; |
| 665 | (move || -> anyhow::Result<()> { |
| 666 | set_style!(_s, &dummy.inner, bad_text_b); |
| 667 | |
| 668 | Ok(()) |
| 669 | })() |
| 670 | .unwrap_err(); |
| 671 | } |
| 672 | } |