Skip to content

Commit

Permalink
no color
Browse files Browse the repository at this point in the history
  • Loading branch information
david-littlefarmer committed Sep 20, 2024
1 parent dd7530d commit 00f0138
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 106 deletions.
4 changes: 2 additions & 2 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func (a attributes) Less(i, j int) bool {
return a[i].Key < a[j].Key
}

func (a attributes) padding(c foregroundColor) int {
func (a attributes) padding(c foregroundColor, colorFunction func(b []byte, fgColor foregroundColor) []byte) int {
var padding int
for _, e := range a {
color := len(e.Key)
if c != nil {
color = len(cs([]byte(e.Key), c))
color = len(colorFunction([]byte(e.Key), c))
}

if color > padding {
Expand Down
3 changes: 2 additions & 1 deletion attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func test_AttributesPadding(t *testing.T) {
slog.Attr{Key: "key2", Value: someValue},
}

padding := attrs.padding(fgMagenta)
h := NewHandler(nil, nil)
padding := attrs.padding(fgMagenta, h.cs)

expectedPadding := 13
if padding != expectedPadding {
Expand Down
26 changes: 21 additions & 5 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var colors = []color{
{fgWhite, bgWhite},
}

func getColor(c Color) color {
func (h *developHandler) getColor(c Color) color {
if int(c) < len(colors) {
return colors[c]
}
Expand All @@ -73,30 +73,46 @@ func getColor(c Color) color {
}

// Color string foreground
func cs(b []byte, fgColor foregroundColor) []byte {
func (h *developHandler) cs(b []byte, fgColor foregroundColor) []byte {
if h.opts.NoColor {
return b
}

b = append(fgColor, b...)
b = append(b, resetColor...)
return b
}

// Color string fainted
func csf(b []byte, fgColor foregroundColor) []byte {
func (h *developHandler) csf(b []byte, fgColor foregroundColor) []byte {
if h.opts.NoColor {
return b
}

b = append(fgColor, b...)
b = append(faintColor, b...)
b = append(b, resetColor...)
return b
}

// Color string background
func csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
func (h *developHandler) csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
if h.opts.NoColor {
return b
}

b = append(fgColor, b...)
b = append(bgColor, b...)
b = append(b, resetColor...)
return b
}

// Underline text
func ul(b []byte) []byte {
func (h *developHandler) ul(b []byte) []byte {
if h.opts.NoColor {
return b
}

b = append(underlineColor, b...)
b = append(b, resetColor...)
return b
Expand Down
33 changes: 17 additions & 16 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,61 @@ import (
)

func Test_Color(t *testing.T) {
test_GetColor(t)
h := NewHandler(nil, nil)
test_GetColor(t, h)

b := []byte("Hello")
test_ColorCs(t, b)
test_ColorCsf(t, b)
test_ColorCsb(t, b)
test_ColorUl(t, b)
test_ColorCs(t, b, h)
test_ColorCsf(t, b, h)
test_ColorCsb(t, b, h)
test_ColorUl(t, b, h)
}

func test_GetColor(t *testing.T) {
result := getColor(Black)
func test_GetColor(t *testing.T, h *developHandler) {
result := h.getColor(Black)
expected := colors[1].fg

if !bytes.Equal(expected, result.fg) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}

result = getColor(Color(20))
result = h.getColor(Color(20))
expected = colors[8].fg

if !bytes.Equal(expected, result.fg) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func test_ColorCs(t *testing.T, b []byte) {
result := cs(b, fgGreen)
func test_ColorCs(t *testing.T, b []byte, h *developHandler) {
result := h.cs(b, fgGreen)

expected := []byte("\x1b[32mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func test_ColorCsf(t *testing.T, b []byte) {
result := csf(b, fgBlue)
func test_ColorCsf(t *testing.T, b []byte, h *developHandler) {
result := h.csf(b, fgBlue)

expected := []byte("\x1b[2m\x1b[34mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func test_ColorCsb(t *testing.T, b []byte) {
result := csb(b, fgYellow, bgRed)
func test_ColorCsb(t *testing.T, b []byte, h *developHandler) {
result := h.csb(b, fgYellow, bgRed)

expected := []byte("\x1b[41m\x1b[33mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func test_ColorUl(t *testing.T, b []byte) {
result := ul(b)
func test_ColorUl(t *testing.T, b []byte, h *developHandler) {
result := h.ul(b)

expected := []byte("\x1b[4mHello\x1b[0m")
if !bytes.Equal(expected, result) {
Expand Down
Loading

0 comments on commit 00f0138

Please sign in to comment.