mirror of
https://github.com/starship/starship.git
synced 2024-11-08 01:15:22 +01:00
fix(config): don't panic when hex color is too short (#1473)
* fix(config): don't panic when hex color is too short * check length instead of using get
This commit is contained in:
parent
08b74c1672
commit
133574ab09
@ -384,6 +384,10 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> {
|
||||
"Attempting to read hexadecimal color string: {}",
|
||||
color_string
|
||||
);
|
||||
if color_string.len() != 7 {
|
||||
log::debug!("Could not parse hexadecimal string: {}", color_string);
|
||||
return None;
|
||||
}
|
||||
let r: u8 = u8::from_str_radix(&color_string[1..3], 16).ok()?;
|
||||
let g: u8 = u8::from_str_radix(&color_string[3..5], 16).ok()?;
|
||||
let b: u8 = u8::from_str_radix(&color_string[5..7], 16).ok()?;
|
||||
@ -591,6 +595,24 @@ mod tests {
|
||||
assert_eq!(<Style>::from_config(&config).unwrap(), Color::Red.bold());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_hex_color_style() {
|
||||
let config = Value::from("#00000");
|
||||
assert_eq!(<Style>::from_config(&config), None);
|
||||
|
||||
let config = Value::from("#0000000");
|
||||
assert_eq!(<Style>::from_config(&config), None);
|
||||
|
||||
let config = Value::from("#NOTHEX");
|
||||
assert_eq!(<Style>::from_config(&config), None);
|
||||
|
||||
let config = Value::from("#a12BcD");
|
||||
assert_eq!(
|
||||
<Style>::from_config(&config).unwrap(),
|
||||
Color::RGB(0xA1, 0x2B, 0xCD).into()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_vec() {
|
||||
let config: Value = Value::Array(vec![Value::from("S")]);
|
||||
|
Loading…
Reference in New Issue
Block a user