fix: color terminal background correctly

resolves #2215
This commit is contained in:
Jan De Dobbeleer 2022-05-06 09:08:00 +02:00 committed by Jan De Dobbeleer
parent 6cc4160ddf
commit 1b32fa0f9a
2 changed files with 11 additions and 3 deletions

View file

@ -62,6 +62,14 @@ func (c AnsiColor) IsTransparent() bool {
return c == transparentAnsiColor
}
func (c AnsiColor) ToForeground() AnsiColor {
colorString := string(c)
if strings.HasPrefix(colorString, "38;") {
return AnsiColor(strings.Replace(colorString, "38;", "48;", 1))
}
return c
}
const (
// Transparent implies a transparent color
Transparent = "transparent"
@ -111,8 +119,8 @@ func (a *AnsiWriter) writeColoredText(background, foreground AnsiColor, text str
foreground = a.getAnsiFromColorString("white", false)
}
if foreground.IsTransparent() && !background.IsEmpty() && len(a.TerminalBackground) != 0 {
fgAnsiColor := a.getAnsiFromColorString(a.TerminalBackground, false)
coloredText := fmt.Sprintf(a.Ansi.colorFull, background, fgAnsiColor, text)
bgAnsiColor := a.getAnsiFromColorString(a.TerminalBackground, false)
coloredText := fmt.Sprintf(a.Ansi.colorFull, background.ToForeground(), bgAnsiColor, text)
a.builder.WriteString(coloredText)
return
}

View file

@ -129,7 +129,7 @@ func TestWriteANSIColors(t *testing.T) {
{
Case: "Transparent foreground, terminal background set",
Input: "test",
Expected: "\x1b[38;2;255;87;51m\x1b[38;2;33;47;60mtest\x1b[0m",
Expected: "\x1b[48;2;255;87;51m\x1b[38;2;33;47;60mtest\x1b[0m",
Colors: &Color{Foreground: Transparent, Background: "#FF5733"},
TerminalBackground: "#212F3C",
},