2023-01-04 11:44:29 -08:00
|
|
|
package ansi
|
2020-09-23 00:33:54 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-01-05 12:57:38 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/shell"
|
2022-12-28 08:30:48 -08:00
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-09-25 11:49:32 -07:00
|
|
|
func TestWriteANSIColors(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected string
|
|
|
|
Input string
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors *Colors
|
|
|
|
Parent *Colors
|
2021-09-25 11:49:32 -07:00
|
|
|
TerminalBackground string
|
|
|
|
}{
|
2023-01-03 03:21:27 -08:00
|
|
|
{
|
|
|
|
Case: "Bold",
|
|
|
|
Input: "<b>test</b>",
|
|
|
|
Expected: "\x1b[1m\x1b[30mtest\x1b[22m\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Bold with color override",
|
|
|
|
Input: "<b><#ffffff>test</></b>",
|
2023-01-06 01:53:57 -08:00
|
|
|
Expected: "\x1b[1m\x1b[30m\x1b[38;2;255;255;255mtest\x1b[0m\x1b[30m\x1b[22m\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Bold with color override, flavor 2",
|
|
|
|
Input: "<#ffffff><b>test</b></>",
|
|
|
|
Expected: "\x1b[38;2;255;255;255m\x1b[1mtest\x1b[22m\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Case: "Double override",
|
|
|
|
Input: "<#ffffff>jan</>@<#ffffff>Jans-MBP</>",
|
|
|
|
Expected: "\x1b[48;2;255;87;51m\x1b[38;2;255;255;255mjan\x1b[32m@\x1b[38;2;255;255;255mJans-MBP\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "green", Background: "#FF5733"},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
2021-09-25 11:49:32 -07:00
|
|
|
{
|
|
|
|
Case: "No color override",
|
|
|
|
Input: "test",
|
|
|
|
Expected: "\x1b[47m\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "black", Background: "white"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit foreground",
|
|
|
|
Input: "test",
|
|
|
|
Expected: "\x1b[47m\x1b[33mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: ParentForeground, Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "white"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit background",
|
|
|
|
Input: "test",
|
|
|
|
Expected: "\x1b[41m\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "No parent",
|
|
|
|
Input: "test",
|
|
|
|
Expected: "\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit override foreground",
|
2021-11-06 05:13:45 -07:00
|
|
|
Input: "hello <parentForeground>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[33mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit override background",
|
2021-11-06 05:13:45 -07:00
|
|
|
Input: "hello <black,parentBackground>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[41mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit override background, no foreground specified",
|
2021-11-06 05:13:45 -07:00
|
|
|
Input: "hello <,parentBackground>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[41mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
2021-10-24 09:42:01 -07:00
|
|
|
{
|
|
|
|
Case: "Inherit no parent foreground",
|
2021-11-06 05:13:45 -07:00
|
|
|
Input: "hello <parentForeground>world</>",
|
2023-01-05 03:46:15 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[0m\x1b[37;49m\x1b[7mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-24 09:42:01 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit no parent background",
|
2021-11-06 05:13:45 -07:00
|
|
|
Input: "hello <,parentBackground>world</>",
|
2023-01-14 01:53:53 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[49mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-24 09:42:01 -07:00
|
|
|
},
|
2021-11-06 05:13:45 -07:00
|
|
|
{
|
|
|
|
Case: "Inherit override both",
|
|
|
|
Input: "hello <parentForeground,parentBackground>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[41m\x1b[33mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-11-06 05:13:45 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Inherit override both inverted",
|
|
|
|
Input: "hello <parentBackground,parentForeground>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello \x1b[43m\x1b[31mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
|
|
|
Parent: &Colors{Foreground: "yellow", Background: "red"},
|
2021-11-06 05:13:45 -07:00
|
|
|
},
|
2021-09-25 11:49:32 -07:00
|
|
|
{
|
|
|
|
Case: "Inline override",
|
|
|
|
Input: "hello, <red>world</>, rabbit",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[47m\x1b[30mhello, \x1b[31mworld\x1b[30m, rabbit\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Transparent background",
|
|
|
|
Input: "hello world",
|
|
|
|
Expected: "\x1b[37mhello world\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "white", Background: Transparent},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Transparent foreground override",
|
|
|
|
Input: "hello <#ffffff>world</>",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[32mhello \x1b[38;2;255;255;255mworld\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "green", Background: Transparent},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "No foreground",
|
|
|
|
Input: "test",
|
|
|
|
Expected: "\x1b[48;2;255;87;51m\x1b[37mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "", Background: "#FF5733"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Transparent foreground",
|
|
|
|
Input: "test",
|
2023-01-05 03:46:15 -08:00
|
|
|
Expected: "\x1b[0m\x1b[38;2;255;87;51;49m\x1b[7mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: Transparent, Background: "#FF5733"},
|
2021-09-25 11:49:32 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Transparent foreground, terminal background set",
|
|
|
|
Input: "test",
|
2023-01-03 03:21:27 -08:00
|
|
|
Expected: "\x1b[38;2;33;47;60m\x1b[48;2;255;87;51mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: Transparent, Background: "#FF5733"},
|
2021-09-25 11:49:32 -07:00
|
|
|
TerminalBackground: "#212F3C",
|
|
|
|
},
|
2021-10-25 10:22:58 -07:00
|
|
|
{
|
|
|
|
Case: "Foreground for foreground override",
|
|
|
|
Input: "<foreground>test</>",
|
|
|
|
Expected: "\x1b[47m\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-25 10:22:58 -07:00
|
|
|
},
|
2023-01-03 03:21:27 -08:00
|
|
|
{
|
|
|
|
Case: "Background for background override",
|
|
|
|
Input: "<,background>test</>",
|
|
|
|
Expected: "\x1b[47m\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Google",
|
|
|
|
Input: "<blue,white>G</><red,white>o</><yellow,white>o</><blue,white>g</><green,white>l</><red,white>e</>",
|
|
|
|
Expected: "\x1b[47m\x1b[34mG\x1b[40m\x1b[30m\x1b[47m\x1b[31mo\x1b[40m\x1b[30m\x1b[47m\x1b[33mo\x1b[40m\x1b[30m\x1b[47m\x1b[34mg\x1b[40m\x1b[30m\x1b[47m\x1b[32ml\x1b[40m\x1b[30m\x1b[47m\x1b[31me\x1b[0m", //nolint: lll
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "black"},
|
2023-01-03 03:21:27 -08:00
|
|
|
},
|
2021-10-25 10:22:58 -07:00
|
|
|
{
|
|
|
|
Case: "Foreground for background override",
|
|
|
|
Input: "<background>test</>",
|
|
|
|
Expected: "\x1b[47m\x1b[37mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-25 10:22:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Foreground for background vice versa override",
|
|
|
|
Input: "<background,foreground>test</>",
|
|
|
|
Expected: "\x1b[40m\x1b[37mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-25 10:22:58 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Background for foreground override",
|
|
|
|
Input: "<,foreground>test</>",
|
|
|
|
Expected: "\x1b[40m\x1b[30mtest\x1b[0m",
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: "white"},
|
2021-10-25 10:22:58 -07:00
|
|
|
},
|
2020-09-23 00:33:54 -07:00
|
|
|
}
|
|
|
|
|
2021-09-25 11:49:32 -07:00
|
|
|
for _, tc := range cases {
|
2023-01-04 11:44:29 -08:00
|
|
|
renderer := &Writer{
|
2023-01-09 11:10:55 -08:00
|
|
|
ParentColors: []*Colors{tc.Parent},
|
2021-10-25 10:22:58 -07:00
|
|
|
Colors: tc.Colors,
|
2022-01-26 04:09:21 -08:00
|
|
|
TerminalBackground: tc.TerminalBackground,
|
|
|
|
AnsiColors: &DefaultColors{},
|
2021-09-25 11:49:32 -07:00
|
|
|
}
|
2023-01-03 03:21:27 -08:00
|
|
|
renderer.Init(shell.GENERIC)
|
2022-01-26 04:09:21 -08:00
|
|
|
renderer.Write(tc.Colors.Background, tc.Colors.Foreground, tc.Input)
|
2022-02-03 08:36:37 -08:00
|
|
|
got, _ := renderer.String()
|
2021-09-25 11:49:32 -07:00
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
2020-11-08 06:52:10 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 02:46:02 -08:00
|
|
|
|
|
|
|
func TestWriteLength(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected int
|
|
|
|
Input string
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors *Colors
|
2023-01-05 02:46:02 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Case: "Bold",
|
|
|
|
Input: "<b>test</b>",
|
|
|
|
Expected: 4,
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-05 02:46:02 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Bold with color override",
|
|
|
|
Input: "<b><#ffffff>test</></b>",
|
|
|
|
Expected: 4,
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-05 02:46:02 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Bold with color override and link",
|
2023-01-12 00:39:15 -08:00
|
|
|
Input: "<b><#ffffff>test</></b> «url»(https://example.com)",
|
2023-01-05 02:46:02 -08:00
|
|
|
Expected: 8,
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-05 02:46:02 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Bold with color override and link and leading/trailing spaces",
|
2023-01-12 00:39:15 -08:00
|
|
|
Input: " <b><#ffffff>test</></b> «url»(https://example.com) ",
|
2023-01-05 02:46:02 -08:00
|
|
|
Expected: 10,
|
2023-01-09 11:10:55 -08:00
|
|
|
Colors: &Colors{Foreground: "black", Background: ParentBackground},
|
2023-01-05 02:46:02 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
renderer := &Writer{
|
2023-01-09 11:10:55 -08:00
|
|
|
ParentColors: []*Colors{},
|
2023-01-05 02:46:02 -08:00
|
|
|
Colors: tc.Colors,
|
|
|
|
AnsiColors: &DefaultColors{},
|
|
|
|
}
|
|
|
|
renderer.Init(shell.GENERIC)
|
|
|
|
renderer.Write(tc.Colors.Background, tc.Colors.Foreground, tc.Input)
|
|
|
|
_, got := renderer.String()
|
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
|
|
|
}
|
|
|
|
}
|