Repository: JanDeDobbeleer/oh-my-posh
Pull request #3180 (2022-12-06 19:21:12)

colors
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

- chore(path): add home tests for mapped locations
- fix(colors): support 256 palette

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/color/colors.go b/src/color/colors.go
index a535d647..b410eda7 100644
--- a/src/color/colors.go
+++ b/src/color/colors.go
@@ -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())


Test patch:
diff --git a/src/color/colors_test.go b/src/color/colors_test.go
index 1efd6bb7..b951c631 100644
--- a/src/color/colors_test.go
+++ b/src/color/colors_test.go
@@ -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},
diff --git a/src/segments/path_test.go b/src/segments/path_test.go
index 687a2a76..4260720e 100644
--- a/src/segments/path_test.go
+++ b/src/segments/path_test.go
@@ -1355,6 +1355,8 @@ func TestReplaceMappedLocations(t *testing.T) {
 		{Pwd: "/f/g/h/e", Expected: "^/e"},
 		{Pwd: "/a/b/c/d", Expected: "#"},
 		{Pwd: "/a/b/c/d/e", Expected: "#/e"},
+		{Pwd: "/a/b/c/d/e", Expected: "#/e"},
+		{Pwd: "/a/b/k/j/e", Expected: "e"},
 	}
 
 	for _, tc := range cases {
@@ -1363,6 +1365,7 @@ func TestReplaceMappedLocations(t *testing.T) {
 		env.On("Pwd").Return(tc.Pwd)
 		env.On("Shell").Return(shell.FISH)
 		env.On("GOOS").Return(platform.DARWIN)
+		env.On("Home").Return("/a/b/k")
 		path := &Path{
 			env: env,
 			props: properties.Map{
@@ -1371,6 +1374,7 @@ func TestReplaceMappedLocations(t *testing.T) {
 					"/a/b/c/d": "#",
 					"/f/g/h/*": "^",
 					"/c/l/k/*": "",
+					"~/j/*":    "",
 				},
 			},
 		}