mirror of
https://github.com/martinvonz/jj.git
synced 2025-05-20 06:33:14 +00:00
formatter: error out on invalid color names
This commit is contained in:
parent
4b5b497283
commit
f5a3d02638
@ -215,6 +215,7 @@ impl Style {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct ColorFormatter<W> {
|
pub struct ColorFormatter<W> {
|
||||||
output: W,
|
output: W,
|
||||||
rules: Arc<Rules>,
|
rules: Arc<Rules>,
|
||||||
@ -332,7 +333,7 @@ fn rules_from_config(config: &config::Config) -> Result<Rules, config::ConfigErr
|
|||||||
match value.kind {
|
match value.kind {
|
||||||
config::ValueKind::String(color_name) => {
|
config::ValueKind::String(color_name) => {
|
||||||
let style = Style {
|
let style = Style {
|
||||||
fg_color: color_for_name(&color_name),
|
fg_color: Some(color_for_name(&color_name)?),
|
||||||
bg_color: None,
|
bg_color: None,
|
||||||
bold: None,
|
bold: None,
|
||||||
underlined: None,
|
underlined: None,
|
||||||
@ -343,12 +344,12 @@ fn rules_from_config(config: &config::Config) -> Result<Rules, config::ConfigErr
|
|||||||
let mut style = Style::default();
|
let mut style = Style::default();
|
||||||
if let Some(value) = style_table.get("fg") {
|
if let Some(value) = style_table.get("fg") {
|
||||||
if let config::ValueKind::String(color_name) = &value.kind {
|
if let config::ValueKind::String(color_name) = &value.kind {
|
||||||
style.fg_color = color_for_name(color_name);
|
style.fg_color = Some(color_for_name(color_name)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(value) = style_table.get("bg") {
|
if let Some(value) = style_table.get("bg") {
|
||||||
if let config::ValueKind::String(color_name) = &value.kind {
|
if let config::ValueKind::String(color_name) = &value.kind {
|
||||||
style.bg_color = color_for_name(color_name);
|
style.bg_color = Some(color_for_name(color_name)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(value) = style_table.get("bold") {
|
if let Some(value) = style_table.get("bold") {
|
||||||
@ -369,25 +370,27 @@ fn rules_from_config(config: &config::Config) -> Result<Rules, config::ConfigErr
|
|||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_for_name(color_name: &str) -> Option<Color> {
|
fn color_for_name(color_name: &str) -> Result<Color, config::ConfigError> {
|
||||||
match color_name {
|
match color_name {
|
||||||
"black" => Some(Color::Black),
|
"black" => Ok(Color::Black),
|
||||||
"red" => Some(Color::DarkRed),
|
"red" => Ok(Color::DarkRed),
|
||||||
"green" => Some(Color::DarkGreen),
|
"green" => Ok(Color::DarkGreen),
|
||||||
"yellow" => Some(Color::DarkYellow),
|
"yellow" => Ok(Color::DarkYellow),
|
||||||
"blue" => Some(Color::DarkBlue),
|
"blue" => Ok(Color::DarkBlue),
|
||||||
"magenta" => Some(Color::DarkMagenta),
|
"magenta" => Ok(Color::DarkMagenta),
|
||||||
"cyan" => Some(Color::DarkCyan),
|
"cyan" => Ok(Color::DarkCyan),
|
||||||
"white" => Some(Color::Grey),
|
"white" => Ok(Color::Grey),
|
||||||
"bright black" => Some(Color::DarkGrey),
|
"bright black" => Ok(Color::DarkGrey),
|
||||||
"bright red" => Some(Color::Red),
|
"bright red" => Ok(Color::Red),
|
||||||
"bright green" => Some(Color::Green),
|
"bright green" => Ok(Color::Green),
|
||||||
"bright yellow" => Some(Color::Yellow),
|
"bright yellow" => Ok(Color::Yellow),
|
||||||
"bright blue" => Some(Color::Blue),
|
"bright blue" => Ok(Color::Blue),
|
||||||
"bright magenta" => Some(Color::Magenta),
|
"bright magenta" => Ok(Color::Magenta),
|
||||||
"bright cyan" => Some(Color::Cyan),
|
"bright cyan" => Ok(Color::Cyan),
|
||||||
"bright white" => Some(Color::White),
|
"bright white" => Ok(Color::White),
|
||||||
_ => None,
|
_ => Err(config::ConfigError::Message(format!(
|
||||||
|
"invalid color: {color_name}"
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -843,7 +846,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_color_formatter_unrecognized_color() {
|
fn test_color_formatter_unrecognized_color() {
|
||||||
// An unrecognized color is ignored; it doesn't reset the color.
|
// An unrecognized color causes an error.
|
||||||
let config = config_from_string(
|
let config = config_from_string(
|
||||||
r#"
|
r#"
|
||||||
colors."outer" = "red"
|
colors."outer" = "red"
|
||||||
@ -851,16 +854,11 @@ mod tests {
|
|||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
let mut output: Vec<u8> = vec![];
|
let mut output: Vec<u8> = vec![];
|
||||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
let err = ColorFormatter::for_config(&mut output, &config)
|
||||||
formatter.push_label("outer").unwrap();
|
.unwrap_err()
|
||||||
formatter.write_str(" red before ").unwrap();
|
.to_string();
|
||||||
formatter.push_label("inner").unwrap();
|
insta::assert_snapshot!(err,
|
||||||
formatter.write_str(" still red inside ").unwrap();
|
@"invalid color: bloo");
|
||||||
formatter.pop_label().unwrap();
|
|
||||||
formatter.write_str(" also red afterwards ").unwrap();
|
|
||||||
formatter.pop_label().unwrap();
|
|
||||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
|
|
||||||
@"[38;5;1m red before still red inside also red afterwards [39m");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user