mirror of
https://github.com/nushell/nushell.git
synced 2025-06-02 08:06:12 +02:00
allow user to use hex colors in config (#486)
This commit is contained in:
parent
ee6ab17fde
commit
3701fd1d76
@ -15,6 +15,15 @@ use std::collections::HashMap;
|
||||
// }
|
||||
|
||||
pub fn lookup_ansi_color_style(s: String) -> Style {
|
||||
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(),
|
||||
@ -83,6 +92,22 @@ pub fn lookup_ansi_color_style(s: String) -> Style {
|
||||
_ => Color::White.normal(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn color_from_hex(hex_color: &str) -> std::result::Result<Option<Color>, 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)?,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: i'm not sure how this ever worked but leaving it in case it's used elsewhere but not implemented yet
|
||||
// pub fn string_to_lookup_value(str_prim: &str) -> String {
|
||||
|
Loading…
x
Reference in New Issue
Block a user