feat(color): allow override from parent

BREAKING CHANGE: this removes the inherit keyword used in color
overrides. Migration from inherit to parentBackground or
parentForeground is needed to stay compatible.
This commit is contained in:
Jan De Dobbeleer 2021-11-06 13:13:45 +01:00 committed by Jan De Dobbeleer
parent fa7c1740a6
commit 9323f89704
4 changed files with 44 additions and 48 deletions

View file

@ -166,13 +166,9 @@ func (b *Block) renderPlainSegment(text string) {
} }
func (b *Block) renderDiamondSegment(text string) { func (b *Block) renderDiamondSegment(text string) {
background := b.activeBackground b.writer.write(Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
if background == Inherit {
background = b.previousActiveSegment.background()
}
b.writer.write(Transparent, background, b.activeSegment.LeadingDiamond)
b.renderText(text) b.renderText(text)
b.writer.write(Transparent, background, b.activeSegment.TrailingDiamond) b.writer.write(Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
} }
func (b *Block) renderText(text string) { func (b *Block) renderText(text string) {

View file

@ -73,8 +73,10 @@ type Color struct {
const ( const (
// Transparent implies a transparent color // Transparent implies a transparent color
Transparent = "transparent" Transparent = "transparent"
// Inherit takes the previous segment's color // ParentBackground takes the previous segment's background color
Inherit = "inherit" ParentBackground = "parentBackground"
// ParentForeground takes the previous segment's color
ParentForeground = "parentForeground"
// Background takes the current segment's background color // Background takes the current segment's background color
Background = "background" Background = "background"
// Foreground takes the current segment's foreground color // Foreground takes the current segment's foreground color
@ -156,30 +158,22 @@ func (a *AnsiWriter) write(background, foreground, text string) {
} }
getAnsiColors := func(background, foreground string) (string, string) { getAnsiColors := func(background, foreground string) (string, string) {
if background == Background { getColorString := func(color string) string {
background = a.Colors.Background if color == Background {
} color = a.Colors.Background
if background == Foreground { } else if color == Foreground {
background = a.Colors.Foreground color = a.Colors.Foreground
} } else if color == ParentBackground && a.ParentColors != nil {
if foreground == Foreground { color = a.ParentColors.Background
foreground = a.Colors.Foreground } else if color == ParentForeground && a.ParentColors != nil {
} color = a.ParentColors.Foreground
if foreground == Background { } else if (color == ParentForeground || color == ParentBackground) && a.ParentColors == nil {
foreground = a.Colors.Background color = Transparent
} }
if background == Inherit && a.ParentColors != nil { return color
background = a.ParentColors.Background
}
if background == Inherit && a.ParentColors == nil {
background = Transparent
}
if foreground == Inherit && a.ParentColors != nil {
foreground = a.ParentColors.Foreground
}
if foreground == Inherit && a.ParentColors == nil {
foreground = Transparent
} }
background = getColorString(background)
foreground = getColorString(foreground)
inverted := foreground == Transparent && len(background) != 0 inverted := foreground == Transparent && len(background) != 0
background = a.getAnsiFromColorString(background, !inverted) background = a.getAnsiFromColorString(background, !inverted)
foreground = a.getAnsiFromColorString(foreground, false) foreground = a.getAnsiFromColorString(foreground, false)
@ -187,7 +181,6 @@ func (a *AnsiWriter) write(background, foreground, text string) {
} }
bgAnsi, fgAnsi := getAnsiColors(background, foreground) bgAnsi, fgAnsi := getAnsiColors(background, foreground)
text = a.ansi.escapeText(text) text = a.ansi.escapeText(text)
text = a.ansi.formatText(text) text = a.ansi.formatText(text)
text = a.ansi.generateHyperlink(text) text = a.ansi.generateHyperlink(text)

View file

@ -48,62 +48,69 @@ func TestWriteANSIColors(t *testing.T) {
Case: "Inherit foreground", Case: "Inherit foreground",
Input: "test", Input: "test",
Expected: "\x1b[47m\x1b[33mtest\x1b[0m", Expected: "\x1b[47m\x1b[33mtest\x1b[0m",
Colors: &Color{Foreground: Inherit, Background: "white"}, Colors: &Color{Foreground: ParentForeground, Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "white"}, Parent: &Color{Foreground: "yellow", Background: "white"},
}, },
{ {
Case: "Inherit background", Case: "Inherit background",
Input: "test", Input: "test",
Expected: "\x1b[41m\x1b[30mtest\x1b[0m", Expected: "\x1b[41m\x1b[30mtest\x1b[0m",
Colors: &Color{Foreground: "black", Background: Inherit}, Colors: &Color{Foreground: "black", Background: ParentBackground},
Parent: &Color{Foreground: "yellow", Background: "red"}, Parent: &Color{Foreground: "yellow", Background: "red"},
}, },
{ {
Case: "No parent", Case: "No parent",
Input: "test", Input: "test",
Expected: "\x1b[30mtest\x1b[0m", Expected: "\x1b[30mtest\x1b[0m",
Colors: &Color{Foreground: "black", Background: Inherit}, Colors: &Color{Foreground: "black", Background: ParentBackground},
}, },
{ {
Case: "Inherit override foreground", Case: "Inherit override foreground",
Input: "hello <inherit>world</>", Input: "hello <parentForeground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[47m\x1b[33mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[47m\x1b[33mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"}, Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"}, Parent: &Color{Foreground: "yellow", Background: "red"},
}, },
{ {
Case: "Inherit override background", Case: "Inherit override background",
Input: "hello <black,inherit>world</>", Input: "hello <black,parentBackground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[30mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[30mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"}, Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"}, Parent: &Color{Foreground: "yellow", Background: "red"},
}, },
{ {
Case: "Inherit override background, no foreground specified", Case: "Inherit override background, no foreground specified",
Input: "hello <,inherit>world</>", Input: "hello <,parentBackground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[30mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[30mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"}, Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"}, Parent: &Color{Foreground: "yellow", Background: "red"},
}, },
{
Case: "Inherit override both",
Input: "hello <inherit,inherit>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[33mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"},
},
{ {
Case: "Inherit no parent foreground", Case: "Inherit no parent foreground",
Input: "hello <inherit>world</>", Input: "hello <parentForeground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[47;49m\x1b[7mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[47;49m\x1b[7mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"}, Colors: &Color{Foreground: "black", Background: "white"},
}, },
{ {
Case: "Inherit no parent background", Case: "Inherit no parent background",
Input: "hello <,inherit>world</>", Input: "hello <,parentBackground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[30mworld\x1b[0m", Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[30mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"}, Colors: &Color{Foreground: "black", Background: "white"},
}, },
{
Case: "Inherit override both",
Input: "hello <parentForeground,parentBackground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[41m\x1b[33mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"},
},
{
Case: "Inherit override both inverted",
Input: "hello <parentBackground,parentForeground>world</>",
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[43m\x1b[31mworld\x1b[0m",
Colors: &Color{Foreground: "black", Background: "white"},
Parent: &Color{Foreground: "yellow", Background: "red"},
},
{ {
Case: "Inline override", Case: "Inline override",
Input: "hello, <red>world</>, rabbit", Input: "hello, <red>world</>, rabbit",

View file

@ -7,7 +7,7 @@
"definitions": { "definitions": {
"color": { "color": {
"type": "string", "type": "string",
"pattern": "^(#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})|black|red|green|yellow|blue|magenta|cyan|white|default|darkGray|lightRed|lightGreen|lightYellow|lightBlue|lightMagenta|lightCyan|lightWhite|transparent|inherit)$", "pattern": "^(#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})|black|red|green|yellow|blue|magenta|cyan|white|default|darkGray|lightRed|lightGreen|lightYellow|lightBlue|lightMagenta|lightCyan|lightWhite|transparent|parentBackground|parentForeground|background|foreground)$",
"title": "Color string", "title": "Color string",
"description": "https://ohmyposh.dev/docs/configure#colors", "description": "https://ohmyposh.dev/docs/configure#colors",
"format": "color" "format": "color"