builtin set_color: do print other colors if one is reset-all

Commit cebc05f6c1 (Reset is not a color, 2025-04-15) tried to simpilfy
this behavior but forgot about cases like "set_color --background=normal
red --bold" where we do want to continue after resetting background.

Closes #11417
This commit is contained in:
Johannes Altmanninger
2025-04-23 19:30:22 +02:00
parent 709f193b50
commit 07ed77e9a8
2 changed files with 24 additions and 22 deletions

View File

@@ -150,30 +150,32 @@ pub fn set_color(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -
// reset all colors/attributes. Same if foreground is "reset" (undocumented).
if is_reset || [fg, bg].iter().any(|c| c.is_some_and(|c| c.is_normal())) {
outp.reset_text_face();
} else {
// Historically we have not used set_text_face() for colors here.
// Doing so would add two magic behaviors:
// - if fg and bg are equal, it makes one of them white
// - if bg is not normal, it makes the foreground bold
// The first one seems fine but the second one not really.
outp.set_text_face(TextFace::new(Color::None, Color::None, Color::None, style));
if let Some(fg) = fg {
if !outp.write_color(Paintable::Foreground, fg) {
// We need to do *something* or the lack of any output messes up
// when the cartesian product here would make "foo" disappear:
// $ echo (set_color foo)bar
outp.reset_text_face();
}
}
// Historically we have not used set_text_face() for colors here.
// Doing so would add two magic behaviors:
// - if fg and bg are equal, it makes one of them white
// - if bg is not normal, it makes the foreground bold
// The first one seems fine but the second one not really.
outp.set_text_face(TextFace::new(Color::None, Color::None, Color::None, style));
if let Some(fg) = fg {
if !fg.is_normal() && !outp.write_color(Paintable::Foreground, fg) {
// We need to do *something* or the lack of any output messes up
// when the cartesian product here would make "foo" disappear:
// $ echo (set_color foo)bar
outp.reset_text_face();
}
if let Some(bg) = bg {
}
if let Some(bg) = bg {
if !bg.is_normal() {
outp.write_color(Paintable::Background, bg);
}
if let Some(underline_color) = underline_color {
if underline_color.is_normal() {
outp.write_command(DefaultUnderlineColor);
} else {
outp.write_color(Paintable::Underline, underline_color);
}
}
if let Some(underline_color) = underline_color {
if underline_color.is_normal() {
outp.write_command(DefaultUnderlineColor);
} else {
outp.write_color(Paintable::Underline, underline_color);
}
}

View File

@@ -14,7 +14,7 @@ string escape (set_color --background=reset)
# CHECKERR: set_color: Unknown color 'reset'
string escape (set_color --bold red --background=normal)
# CHECK: \e\[m
# CHECK: \e\[m\e\[1m\e\[31m
string escape (set_color --bold red --background=blue)
# CHECK: \e\[1m\e\[31m\e\[44m