fix(colors): support 256 palette

resolves #3175
This commit is contained in:
Jan De Dobbeleer 2022-12-06 20:20:27 +01:00 committed by Jan De Dobbeleer
parent a1801f717c
commit 20462aa50f
2 changed files with 11 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"oh-my-posh/platform"
"strconv"
"strings"
"github.com/gookit/color"
)
@ -78,6 +79,14 @@ func (d *DefaultColors) AnsiColorFromString(colorString string, isBackground boo
if err == nil {
return colorFromName
}
if !strings.HasPrefix(colorString, "#") {
val, err := strconv.ParseUint(colorString, 10, 64)
if err != nil {
return emptyAnsiColor
}
c256 := color.C256(uint8(val), isBackground)
return AnsiColor(c256.RGBColor().String())
}
style := color.HEX(colorString, isBackground)
if !style.IsEmpty() {
return AnsiColor(style.String())

View file

@ -13,6 +13,8 @@ func TestGetAnsiFromColorString(t *testing.T) {
Color string
Background bool
}{
{Case: "256 color", Expected: AnsiColor("38;2;135;95;255"), Color: "99", Background: false},
{Case: "256 color", Expected: AnsiColor("38;2;135;255;215"), Color: "122", Background: false},
{Case: "Invalid background", Expected: emptyAnsiColor, Color: "invalid", Background: true},
{Case: "Invalid background", Expected: emptyAnsiColor, Color: "invalid", Background: false},
{Case: "Hex foreground", Expected: AnsiColor("38;2;170;187;204"), Color: "#AABBCC", Background: false},