diff --git a/crates/nu-command/src/viewers/color_config.rs b/crates/nu-command/src/viewers/color_config.rs index 7feedbd4fd..62e93c855c 100644 --- a/crates/nu-command/src/viewers/color_config.rs +++ b/crates/nu-command/src/viewers/color_config.rs @@ -15,72 +15,97 @@ use std::collections::HashMap; // } pub fn lookup_ansi_color_style(s: String) -> Style { - match s.as_str() { - "g" | "green" => Color::Green.normal(), - "gb" | "green_bold" => Color::Green.bold(), - "gu" | "green_underline" => Color::Green.underline(), - "gi" | "green_italic" => Color::Green.italic(), - "gd" | "green_dimmed" => Color::Green.dimmed(), - "gr" | "green_reverse" => Color::Green.reverse(), - "gbl" | "green_blink" => Color::Green.blink(), - "gst" | "green_strike" => Color::Green.strikethrough(), - "r" | "red" => Color::Red.normal(), - "rb" | "red_bold" => Color::Red.bold(), - "ru" | "red_underline" => Color::Red.underline(), - "ri" | "red_italic" => Color::Red.italic(), - "rd" | "red_dimmed" => Color::Red.dimmed(), - "rr" | "red_reverse" => Color::Red.reverse(), - "rbl" | "red_blink" => Color::Red.blink(), - "rst" | "red_strike" => Color::Red.strikethrough(), - "u" | "blue" => Color::Blue.normal(), - "ub" | "blue_bold" => Color::Blue.bold(), - "uu" | "blue_underline" => Color::Blue.underline(), - "ui" | "blue_italic" => Color::Blue.italic(), - "ud" | "blue_dimmed" => Color::Blue.dimmed(), - "ur" | "blue_reverse" => Color::Blue.reverse(), - "ubl" | "blue_blink" => Color::Blue.blink(), - "ust" | "blue_strike" => Color::Blue.strikethrough(), - "b" | "black" => Color::Black.normal(), - "bb" | "black_bold" => Color::Black.bold(), - "bu" | "black_underline" => Color::Black.underline(), - "bi" | "black_italic" => Color::Black.italic(), - "bd" | "black_dimmed" => Color::Black.dimmed(), - "br" | "black_reverse" => Color::Black.reverse(), - "bbl" | "black_blink" => Color::Black.blink(), - "bst" | "black_strike" => Color::Black.strikethrough(), - "y" | "yellow" => Color::Yellow.normal(), - "yb" | "yellow_bold" => Color::Yellow.bold(), - "yu" | "yellow_underline" => Color::Yellow.underline(), - "yi" | "yellow_italic" => Color::Yellow.italic(), - "yd" | "yellow_dimmed" => Color::Yellow.dimmed(), - "yr" | "yellow_reverse" => Color::Yellow.reverse(), - "ybl" | "yellow_blink" => Color::Yellow.blink(), - "yst" | "yellow_strike" => Color::Yellow.strikethrough(), - "p" | "purple" => Color::Purple.normal(), - "pb" | "purple_bold" => Color::Purple.bold(), - "pu" | "purple_underline" => Color::Purple.underline(), - "pi" | "purple_italic" => Color::Purple.italic(), - "pd" | "purple_dimmed" => Color::Purple.dimmed(), - "pr" | "purple_reverse" => Color::Purple.reverse(), - "pbl" | "purple_blink" => Color::Purple.blink(), - "pst" | "purple_strike" => Color::Purple.strikethrough(), - "c" | "cyan" => Color::Cyan.normal(), - "cb" | "cyan_bold" => Color::Cyan.bold(), - "cu" | "cyan_underline" => Color::Cyan.underline(), - "ci" | "cyan_italic" => Color::Cyan.italic(), - "cd" | "cyan_dimmed" => Color::Cyan.dimmed(), - "cr" | "cyan_reverse" => Color::Cyan.reverse(), - "cbl" | "cyan_blink" => Color::Cyan.blink(), - "cst" | "cyan_strike" => Color::Cyan.strikethrough(), - "w" | "white" => Color::White.normal(), - "wb" | "white_bold" => Color::White.bold(), - "wu" | "white_underline" => Color::White.underline(), - "wi" | "white_italic" => Color::White.italic(), - "wd" | "white_dimmed" => Color::White.dimmed(), - "wr" | "white_reverse" => Color::White.reverse(), - "wbl" | "white_blink" => Color::White.blink(), - "wst" | "white_strike" => Color::White.strikethrough(), - _ => Color::White.normal(), + if s.starts_with('#') { + match color_from_hex(&s) { + Ok(c) => match c { + Some(c) => c.normal(), + None => Style::default(), + }, + Err(_) => Style::default(), + } + } else { + match s.as_str() { + "g" | "green" => Color::Green.normal(), + "gb" | "green_bold" => Color::Green.bold(), + "gu" | "green_underline" => Color::Green.underline(), + "gi" | "green_italic" => Color::Green.italic(), + "gd" | "green_dimmed" => Color::Green.dimmed(), + "gr" | "green_reverse" => Color::Green.reverse(), + "gbl" | "green_blink" => Color::Green.blink(), + "gst" | "green_strike" => Color::Green.strikethrough(), + "r" | "red" => Color::Red.normal(), + "rb" | "red_bold" => Color::Red.bold(), + "ru" | "red_underline" => Color::Red.underline(), + "ri" | "red_italic" => Color::Red.italic(), + "rd" | "red_dimmed" => Color::Red.dimmed(), + "rr" | "red_reverse" => Color::Red.reverse(), + "rbl" | "red_blink" => Color::Red.blink(), + "rst" | "red_strike" => Color::Red.strikethrough(), + "u" | "blue" => Color::Blue.normal(), + "ub" | "blue_bold" => Color::Blue.bold(), + "uu" | "blue_underline" => Color::Blue.underline(), + "ui" | "blue_italic" => Color::Blue.italic(), + "ud" | "blue_dimmed" => Color::Blue.dimmed(), + "ur" | "blue_reverse" => Color::Blue.reverse(), + "ubl" | "blue_blink" => Color::Blue.blink(), + "ust" | "blue_strike" => Color::Blue.strikethrough(), + "b" | "black" => Color::Black.normal(), + "bb" | "black_bold" => Color::Black.bold(), + "bu" | "black_underline" => Color::Black.underline(), + "bi" | "black_italic" => Color::Black.italic(), + "bd" | "black_dimmed" => Color::Black.dimmed(), + "br" | "black_reverse" => Color::Black.reverse(), + "bbl" | "black_blink" => Color::Black.blink(), + "bst" | "black_strike" => Color::Black.strikethrough(), + "y" | "yellow" => Color::Yellow.normal(), + "yb" | "yellow_bold" => Color::Yellow.bold(), + "yu" | "yellow_underline" => Color::Yellow.underline(), + "yi" | "yellow_italic" => Color::Yellow.italic(), + "yd" | "yellow_dimmed" => Color::Yellow.dimmed(), + "yr" | "yellow_reverse" => Color::Yellow.reverse(), + "ybl" | "yellow_blink" => Color::Yellow.blink(), + "yst" | "yellow_strike" => Color::Yellow.strikethrough(), + "p" | "purple" => Color::Purple.normal(), + "pb" | "purple_bold" => Color::Purple.bold(), + "pu" | "purple_underline" => Color::Purple.underline(), + "pi" | "purple_italic" => Color::Purple.italic(), + "pd" | "purple_dimmed" => Color::Purple.dimmed(), + "pr" | "purple_reverse" => Color::Purple.reverse(), + "pbl" | "purple_blink" => Color::Purple.blink(), + "pst" | "purple_strike" => Color::Purple.strikethrough(), + "c" | "cyan" => Color::Cyan.normal(), + "cb" | "cyan_bold" => Color::Cyan.bold(), + "cu" | "cyan_underline" => Color::Cyan.underline(), + "ci" | "cyan_italic" => Color::Cyan.italic(), + "cd" | "cyan_dimmed" => Color::Cyan.dimmed(), + "cr" | "cyan_reverse" => Color::Cyan.reverse(), + "cbl" | "cyan_blink" => Color::Cyan.blink(), + "cst" | "cyan_strike" => Color::Cyan.strikethrough(), + "w" | "white" => Color::White.normal(), + "wb" | "white_bold" => Color::White.bold(), + "wu" | "white_underline" => Color::White.underline(), + "wi" | "white_italic" => Color::White.italic(), + "wd" | "white_dimmed" => Color::White.dimmed(), + "wr" | "white_reverse" => Color::White.reverse(), + "wbl" | "white_blink" => Color::White.blink(), + "wst" | "white_strike" => Color::White.strikethrough(), + _ => Color::White.normal(), + } + } +} + +fn color_from_hex(hex_color: &str) -> std::result::Result, std::num::ParseIntError> { + // right now we only allow hex colors with hashtag and 6 characters + let trimmed = hex_color.trim_matches('#'); + if trimmed.len() != 6 { + Ok(None) + } else { + // make a nu_ansi_term::Color::Rgb color by converting hex to decimal + Ok(Some(Color::Rgb( + u8::from_str_radix(&trimmed[..2], 16)?, + u8::from_str_radix(&trimmed[2..4], 16)?, + u8::from_str_radix(&trimmed[4..6], 16)?, + ))) } }