fix(color): fix gray color for 256 color terminal

This commit is contained in:
Vitali Tatarintev 2023-03-20 00:56:43 +00:00 committed by Jay McCure
parent 0d0b69da91
commit f87f7efaa6
2 changed files with 51 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package iostreams
import (
"fmt"
"io"
"os"
"strings"
@ -55,6 +56,12 @@ func NewColorable(out io.Writer) io.Writer {
}
func makeColorFunc(color string) func(string) string {
if color == "black+h" && isColorEnabled() && Is256ColorSupported() {
return func(t string) string {
return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t)
}
}
cf := ansi.ColorFunc(color)
return func(arg string) string {
if isColorEnabled() && isStdoutTerminal() {

View File

@ -1,10 +1,12 @@
package iostreams
import (
"fmt"
"os"
"testing"
"github.com/alecthomas/assert"
"github.com/stretchr/testify/require"
)
func Test_isColorEnabled(t *testing.T) {
@ -50,3 +52,45 @@ func Test_isColorEnabled(t *testing.T) {
assert.True(t, got)
})
}
func Test_makeColorFunc(t *testing.T) {
tests := []struct {
name string
color string
colorEnabled bool
is256color bool
want string
}{
{
"gray",
"black+h",
true,
false,
"text",
},
{
"gray_256",
"black+h",
true,
true,
fmt.Sprintf("\x1b[38;5;242m%s\x1b[m", "text"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.colorEnabled {
t.Setenv("COLOR_ENABLED", "true")
}
if tt.is256color {
t.Setenv("TERM", "256")
}
fn := makeColorFunc(tt.color)
got := fn("text")
require.Equal(t, tt.want, got)
})
}
}