Skip to content

Commit d4f1aad

Browse files
committed
feat(tui): accept #RRGGBB theme colors
1 parent 0d7a432 commit d4f1aad

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

crates/trippy-tui/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ mod tests {
15721572
#[test_case("trip example.com", Ok(cfg().tui_theme(TuiTheme::default()).build()); "default tui theme")]
15731573
#[test_case("trip example.com --tui-theme-colors bg-color=red", Ok(cfg().tui_theme(TuiTheme { bg: TuiColor::Red, ..Default::default() }).build()); "custom tui theme named color")]
15741574
#[test_case("trip example.com --tui-theme-colors bg-color=010203", Ok(cfg().tui_theme(TuiTheme { bg: TuiColor::Rgb(1, 2, 3), ..Default::default() }).build()); "custom tui theme hex color")]
1575+
#[test_case("trip example.com --tui-theme-colors bg-color=#010203", Ok(cfg().tui_theme(TuiTheme { bg: TuiColor::Rgb(1, 2, 3), ..Default::default() }).build()); "custom tui theme hex color with hash prefix")]
15751576
#[test_case("trip example.com --tui-theme-colors bg-color=red,text-color=blue", Ok(cfg().tui_theme(TuiTheme { bg: TuiColor::Red, text: TuiColor::Blue, ..Default::default() }).build()); "custom tui theme multiple")]
15761577
#[test_case("trip example.com --tui-theme-colors bg-color=0", Err(anyhow!("error: invalid value 'bg-color=0' for '--tui-theme-colors <TUI_THEME_COLORS>': unknown color: 0 For more information, try '--help'.")); "invalid tui theme truncated hex value")]
15771578
#[test_case("trip example.com --tui-theme-colors bg-color=foo", Err(anyhow!("error: invalid value 'bg-color=foo' for '--tui-theme-colors <TUI_THEME_COLORS>': unknown color: foo For more information, try '--help'. ")); "invalid tui theme invalid named color")]

crates/trippy-tui/src/config/theme.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,9 @@ impl TryFrom<&str> for TuiColor {
545545

546546
#[expect(clippy::too_many_lines)]
547547
fn try_from(value: &str) -> Result<Self, Self::Error> {
548-
match value.to_ascii_lowercase().replace('-', "").as_ref() {
548+
let normalized = value.to_ascii_lowercase().replace('-', "");
549+
let rgb_hex = normalized.strip_prefix('#').unwrap_or(&normalized);
550+
match normalized.as_ref() {
549551
"black" => Ok(Self::Black),
550552
"red" => Ok(Self::Red),
551553
"green" => Ok(Self::Green),
@@ -689,7 +691,7 @@ impl TryFrom<&str> for TuiColor {
689691
"wheat" => Ok(Self::Wheat),
690692
"whitesmoke" => Ok(Self::WhiteSmoke),
691693
"yellowgreen" => Ok(Self::YellowGreen),
692-
rgb_hex if value.len() == 6 && value.chars().all(|c| c.is_ascii_hexdigit()) => {
694+
_ if rgb_hex.len() == 6 && rgb_hex.chars().all(|c| c.is_ascii_hexdigit()) => {
693695
let red = u8::from_str_radix(&rgb_hex[0..2], 16)?;
694696
let green = u8::from_str_radix(&rgb_hex[2..4], 16)?;
695697
let blue = u8::from_str_radix(&rgb_hex[4..6], 16)?;

0 commit comments

Comments
 (0)