mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-03 15:27:26 -08:00
refactor(ansi): write inverted colors correctly for transparency
This commit is contained in:
parent
0ebd59297b
commit
850787cd6f
|
@ -55,7 +55,8 @@ const (
|
||||||
|
|
||||||
anchorRegex = `^(?P<ANCHOR><(?P<FG>[^,>]+)?,?(?P<BG>[^>]+)?>)`
|
anchorRegex = `^(?P<ANCHOR><(?P<FG>[^,>]+)?,?(?P<BG>[^>]+)?>)`
|
||||||
colorise = "\x1b[%sm"
|
colorise = "\x1b[%sm"
|
||||||
transparent = "\x1b[%s;49m\x1b[7m"
|
transparent = "\x1b[0m\x1b[%s;49m\x1b[7m"
|
||||||
|
transparentEnd = "\x1b[27m"
|
||||||
|
|
||||||
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=><~]))"
|
||||||
|
|
||||||
|
@ -87,6 +88,7 @@ type Writer struct {
|
||||||
currentForeground Color
|
currentForeground Color
|
||||||
currentBackground Color
|
currentBackground Color
|
||||||
runes []rune
|
runes []rune
|
||||||
|
transparent bool
|
||||||
|
|
||||||
shell string
|
shell string
|
||||||
format string
|
format string
|
||||||
|
@ -363,11 +365,15 @@ func (w *Writer) writeSegmentColors() {
|
||||||
fg = w.currentForeground
|
fg = w.currentForeground
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// always reset inverted
|
||||||
|
// w.transparent = false
|
||||||
|
|
||||||
if fg.IsTransparent() && len(w.TerminalBackground) != 0 {
|
if fg.IsTransparent() && len(w.TerminalBackground) != 0 {
|
||||||
background := w.getAnsiFromColorString(w.TerminalBackground, false)
|
background := w.getAnsiFromColorString(w.TerminalBackground, false)
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(colorise, background))
|
w.writeEscapedAnsiString(fmt.Sprintf(colorise, background))
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(colorise, bg.ToForeground()))
|
w.writeEscapedAnsiString(fmt.Sprintf(colorise, bg.ToForeground()))
|
||||||
} else if fg.IsTransparent() && !bg.IsEmpty() {
|
} else if fg.IsTransparent() && !bg.IsEmpty() {
|
||||||
|
w.transparent = true
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(transparent, bg))
|
w.writeEscapedAnsiString(fmt.Sprintf(transparent, bg))
|
||||||
} else {
|
} else {
|
||||||
if !bg.IsEmpty() && !bg.IsTransparent() {
|
if !bg.IsEmpty() && !bg.IsTransparent() {
|
||||||
|
@ -393,12 +399,16 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
|
||||||
if position == len(w.runes)-1 {
|
if position == len(w.runes)-1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if w.currentBackground != w.background {
|
if w.currentBackground != w.background || w.transparent {
|
||||||
|
if w.transparent {
|
||||||
|
w.writeEscapedAnsiString(transparentEnd)
|
||||||
|
}
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.background))
|
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.background))
|
||||||
}
|
}
|
||||||
if w.currentForeground != w.foreground {
|
if w.currentForeground != w.foreground || w.transparent {
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.foreground))
|
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.foreground))
|
||||||
}
|
}
|
||||||
|
w.transparent = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,6 +446,7 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.currentForeground.IsTransparent() && !w.currentBackground.IsTransparent() {
|
if w.currentForeground.IsTransparent() && !w.currentBackground.IsTransparent() {
|
||||||
|
w.transparent = true
|
||||||
w.writeEscapedAnsiString(fmt.Sprintf(transparent, w.currentBackground))
|
w.writeEscapedAnsiString(fmt.Sprintf(transparent, w.currentBackground))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -457,6 +468,12 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Writer) asAnsiColors(background, foreground string) (Color, Color) {
|
func (w *Writer) asAnsiColors(background, foreground string) (Color, Color) {
|
||||||
|
if len(background) == 0 {
|
||||||
|
background = Background
|
||||||
|
}
|
||||||
|
if len(foreground) == 0 {
|
||||||
|
foreground = Foreground
|
||||||
|
}
|
||||||
background = w.expandKeyword(background)
|
background = w.expandKeyword(background)
|
||||||
foreground = w.expandKeyword(foreground)
|
foreground = w.expandKeyword(foreground)
|
||||||
inverted := foreground == Transparent && len(background) != 0
|
inverted := foreground == Transparent && len(background) != 0
|
||||||
|
|
|
@ -41,9 +41,9 @@ func TestGenerateHyperlinkWithUrl(t *testing.T) {
|
||||||
Expected: "\x1b[47m\x1b[30m\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\ \x1b]8;;http://maps.google.be\x1b\\maps (2/2)\x1b]8;;\x1b\\\x1b[0m",
|
Expected: "\x1b[47m\x1b[30m\x1b]8;;http://www.google.be\x1b\\google\x1b]8;;\x1b\\ \x1b]8;;http://maps.google.be\x1b\\maps (2/2)\x1b]8;;\x1b\\\x1b[0m",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Text: "in <accent>\x1b[1mpwsh \x1b[22m</> ",
|
Text: "in <accent><b>pwsh </b></> ",
|
||||||
ShellName: shell.PWSH,
|
ShellName: shell.PWSH,
|
||||||
Expected: "\x1b[47m\x1b[30min \x1b[1mpwsh \x1b[22m \x1b[0m",
|
Expected: "\x1b[47m\x1b[30min \x1b[0m\x1b[30m\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"},
|
||||||
|
|
|
@ -93,7 +93,7 @@ func TestWriteANSIColors(t *testing.T) {
|
||||||
{
|
{
|
||||||
Case: "Inherit no parent foreground",
|
Case: "Inherit no parent foreground",
|
||||||
Input: "hello <parentForeground>world</>",
|
Input: "hello <parentForeground>world</>",
|
||||||
Expected: "\x1b[47m\x1b[30mhello \x1b[47;49m\x1b[7mworld\x1b[0m",
|
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[37;49m\x1b[7mworld\x1b[0m",
|
||||||
Colors: &cachedColor{Foreground: "black", Background: "white"},
|
Colors: &cachedColor{Foreground: "black", Background: "white"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -143,7 +143,7 @@ func TestWriteANSIColors(t *testing.T) {
|
||||||
{
|
{
|
||||||
Case: "Transparent foreground",
|
Case: "Transparent foreground",
|
||||||
Input: "test",
|
Input: "test",
|
||||||
Expected: "\x1b[38;2;255;87;51;49m\x1b[7mtest\x1b[0m",
|
Expected: "\x1b[0m\x1b[38;2;255;87;51;49m\x1b[7mtest\x1b[0m",
|
||||||
Colors: &cachedColor{Foreground: Transparent, Background: "#FF5733"},
|
Colors: &cachedColor{Foreground: Transparent, Background: "#FF5733"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue