refactor(ansi): remove background color on transparent

This commit is contained in:
Jan De Dobbeleer 2023-01-14 10:53:53 +01:00 committed by Jan De Dobbeleer
parent 8d8f2b8497
commit cc3a64fad2
3 changed files with 5 additions and 4 deletions

View file

@ -57,6 +57,7 @@ const (
colorise = "\x1b[%sm" colorise = "\x1b[%sm"
transparent = "\x1b[0m\x1b[%s;49m\x1b[7m" transparent = "\x1b[0m\x1b[%s;49m\x1b[7m"
transparentEnd = "\x1b[27m" transparentEnd = "\x1b[27m"
backgroundEnd = "\x1b[49m"
AnsiRegex = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))" AnsiRegex = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
@ -481,13 +482,13 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
if w.currentBackground != w.background { if w.currentBackground != w.background {
// end the colors in case we have a transparent background // end the colors in case we have a transparent background
if w.currentBackground.IsTransparent() { if w.currentBackground.IsTransparent() {
w.writeEscapedAnsiString(colorStyle.End) w.writeEscapedAnsiString(backgroundEnd)
} else { } else {
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.currentBackground)) w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.currentBackground))
} }
} }
if w.currentForeground != w.foreground || w.currentBackground.IsTransparent() { if w.currentForeground != w.foreground {
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.currentForeground)) w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.currentForeground))
} }

View file

@ -44,7 +44,7 @@ func TestGenerateHyperlinkWithUrl(t *testing.T) {
{ {
Text: "in <accent><b>pwsh </b></> ", Text: "in <accent><b>pwsh </b></> ",
ShellName: shell.PWSH, ShellName: shell.PWSH,
Expected: "\x1b[47m\x1b[30min \x1b[0m\x1b[30m\x1b[1mpwsh \x1b[22m\x1b[47m \x1b[0m", Expected: "\x1b[47m\x1b[30min \x1b[49m\x1b[1mpwsh \x1b[22m\x1b[47m \x1b[0m",
}, },
{Text: "«google»(http://www.google.be)", ShellName: shell.ZSH, Expected: "%{\x1b[47m%}%{\x1b[30m%}%{\x1b]8;;http://www.google.be\x1b\\%}google%{\x1b]8;;\x1b\\%}%{\x1b[0m%}"}, {Text: "«google»(http://www.google.be)", ShellName: shell.ZSH, Expected: "%{\x1b[47m%}%{\x1b[30m%}%{\x1b]8;;http://www.google.be\x1b\\%}google%{\x1b]8;;\x1b\\%}%{\x1b[0m%}"},
{Text: "«google»(http://www.google.be)", ShellName: shell.PWSH, Expected: "\x1b[47m\x1b[30m\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\\x1b[0m"}, {Text: "«google»(http://www.google.be)", ShellName: shell.PWSH, Expected: "\x1b[47m\x1b[30m\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\\x1b[0m"},

View file

@ -99,7 +99,7 @@ func TestWriteANSIColors(t *testing.T) {
{ {
Case: "Inherit no parent background", Case: "Inherit no parent background",
Input: "hello <,parentBackground>world</>", Input: "hello <,parentBackground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[30mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[49mworld\x1b[0m",
Colors: &Colors{Foreground: "black", Background: "white"}, Colors: &Colors{Foreground: "black", Background: "white"},
}, },
{ {