formatter: accept foreground color config as {fg = "blue"}

This commit is contained in:
Martin von Zweigbergk 2023-01-05 21:58:52 -08:00 committed by Martin von Zweigbergk
parent 19e23c321b
commit 2080913d15

View file

@ -268,10 +268,24 @@ fn rules_from_config(config: &config::Config) -> HashMap<Vec<String>, Style> {
.split_whitespace()
.map(ToString::to_string)
.collect_vec();
let style = Style {
fg_color: color_for_name(&value.to_string()),
};
result.insert(labels, style);
match value.kind {
config::ValueKind::String(color_name) => {
let style = Style {
fg_color: color_for_name(&color_name),
};
result.insert(labels, style);
}
config::ValueKind::Table(style_table) => {
let mut style = Style::default();
if let Some(value) = style_table.get("fg") {
if let config::ValueKind::String(color_name) = &value.kind {
style.fg_color = color_for_name(color_name);
}
}
result.insert(labels, style);
}
_ => {}
}
}
}
result
@ -419,6 +433,22 @@ mod tests {
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" before  inside  after ");
}
#[test]
fn test_color_formatter_attributes() {
// Test that each attribute of the style can be set.
let config = config_from_string(
r#"
colors.red_fg = { fg = "red" }
"#,
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("red_fg").unwrap();
formatter.write_str(" fg only ").unwrap();
formatter.remove_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" fg only ");
}
#[test]
fn test_color_formatter_no_space() {
// Test that two different colors can touch.