diff options
author | Cody Bloemhard <cblmhrd@live.nl> | 2020-12-02 07:40:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-02 00:40:15 -0600 |
commit | ef311408dbb283c79aa805796eac20b52b4a8cb3 (patch) | |
tree | 5aedf6ab004de450e21c8a95818cf4bee32987ef /src | |
parent | f873a9820e2f24ceadddde82b942711f2dd235db (diff) |
fix: bg:none overwrites foreground colour (#1903)
Changes the behaviour of a bg:none color string so that
it causes the background to have no color instead of
decoloring the entire string like it did before.
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 62 |
1 files changed, 54 insertions, 8 deletions
diff --git a/src/config.rs b/src/config.rs index cc3beaa5a..a210f52e8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -375,16 +375,31 @@ pub fn parse_style_string(style_string: &str) -> Option<ansi_term::Style> { "bold" => Some(style.bold()), "italic" => Some(style.italic()), "dimmed" => Some(style.dimmed()), - "none" => None, - - // Try to see if this token parses as a valid color string - color_string => parse_color_string(color_string).map(|ansi_color| { - if col_fg { - style.fg(ansi_color) + // When the string is supposed to be a color: + // Decide if we yield none, reset background or set color. + color_string => { + if color_string == "none" && col_fg { + None // fg:none yields no style. } else { - style.on(ansi_color) + // Either bg or valid color or both. + let parsed = parse_color_string(color_string); + // bg + invalid color = reset the background to default. + if !col_fg && parsed.is_none() { + let mut new_style = style; + new_style.background = Option::None; + Some(new_style) + } else { + // Valid color, apply color to either bg or fg + parsed.map(|ansi_color| { + if col_fg { + style.fg(ansi_color) + } else { + style.on(ansi_color) + } + }) + } } - }), + } } }) }) @@ -685,6 +700,37 @@ mod tests { } #[test] + fn table_get_styles_with_none() { + // Test that none on the end will result in None, overriding bg:none + let config = Value::from("fg:red bg:none none"); + assert!(<Style>::from_config(&config).is_none()); + + // Test that none in front will result in None, overriding bg:none + let config = Value::from("none fg:red bg:none"); + assert!(<Style>::from_config(&config).is_none()); + + // Test that none in the middle will result in None, overriding bg:none + let config = Value::from("fg:red none bg:none"); + assert!(<Style>::from_config(&config).is_none()); + + // Test that fg:none will result in None + let config = Value::from("fg:none bg:black"); + assert!(<Style>::from_config(&config).is_none()); + + // Test that bg:none will yield a style + let config = Value::from("fg:red bg:none"); + assert_eq!(<Style>::from_config(&config).unwrap(), Color::Red.normal()); + + // Test that bg:none will yield a style + let config = Value::from("fg:red bg:none bold"); + assert_eq!(<Style>::from_config(&config).unwrap(), Color::Red.bold()); + + // Test that bg:none will overwrite the previous background colour + let config = Value::from("fg:red bg:green bold bg:none"); + assert_eq!(<Style>::from_config(&config).unwrap(), Color::Red.bold()); + } + + #[test] fn table_get_styles_ordered() { // Test a background style with inverted order (also test hex + ANSI) let config = Value::from("bg:#050505 underline fg:120"); |