mirror of
https://github.com/nushell/nushell
synced 2024-12-28 05:53:09 +00:00
allow user to use hex colors in config (#486)
This commit is contained in:
parent
ee6ab17fde
commit
3701fd1d76
1 changed files with 91 additions and 66 deletions
|
@ -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(),
|
||||
|
@ -82,6 +91,22 @@ pub fn lookup_ansi_color_style(s: String) -> Style {
|
|||
"wst" | "white_strike" => Color::White.strikethrough(),
|
||||
_ => 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
|
||||
|
|
Loading…
Reference in a new issue