Skip to content

Commit 00f0138

Browse files
no color
1 parent dd7530d commit 00f0138

File tree

6 files changed

+149
-106
lines changed

6 files changed

+149
-106
lines changed

attributes.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func (a attributes) Less(i, j int) bool {
1818
return a[i].Key < a[j].Key
1919
}
2020

21-
func (a attributes) padding(c foregroundColor) int {
21+
func (a attributes) padding(c foregroundColor, colorFunction func(b []byte, fgColor foregroundColor) []byte) int {
2222
var padding int
2323
for _, e := range a {
2424
color := len(e.Key)
2525
if c != nil {
26-
color = len(cs([]byte(e.Key), c))
26+
color = len(colorFunction([]byte(e.Key), c))
2727
}
2828

2929
if color > padding {

attributes_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ func test_AttributesPadding(t *testing.T) {
9292
slog.Attr{Key: "key2", Value: someValue},
9393
}
9494

95-
padding := attrs.padding(fgMagenta)
95+
h := NewHandler(nil, nil)
96+
padding := attrs.padding(fgMagenta, h.cs)
9697

9798
expectedPadding := 13
9899
if padding != expectedPadding {

color.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var colors = []color{
6464
{fgWhite, bgWhite},
6565
}
6666

67-
func getColor(c Color) color {
67+
func (h *developHandler) getColor(c Color) color {
6868
if int(c) < len(colors) {
6969
return colors[c]
7070
}
@@ -73,30 +73,46 @@ func getColor(c Color) color {
7373
}
7474

7575
// Color string foreground
76-
func cs(b []byte, fgColor foregroundColor) []byte {
76+
func (h *developHandler) cs(b []byte, fgColor foregroundColor) []byte {
77+
if h.opts.NoColor {
78+
return b
79+
}
80+
7781
b = append(fgColor, b...)
7882
b = append(b, resetColor...)
7983
return b
8084
}
8185

8286
// Color string fainted
83-
func csf(b []byte, fgColor foregroundColor) []byte {
87+
func (h *developHandler) csf(b []byte, fgColor foregroundColor) []byte {
88+
if h.opts.NoColor {
89+
return b
90+
}
91+
8492
b = append(fgColor, b...)
8593
b = append(faintColor, b...)
8694
b = append(b, resetColor...)
8795
return b
8896
}
8997

9098
// Color string background
91-
func csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
99+
func (h *developHandler) csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
100+
if h.opts.NoColor {
101+
return b
102+
}
103+
92104
b = append(fgColor, b...)
93105
b = append(bgColor, b...)
94106
b = append(b, resetColor...)
95107
return b
96108
}
97109

98110
// Underline text
99-
func ul(b []byte) []byte {
111+
func (h *developHandler) ul(b []byte) []byte {
112+
if h.opts.NoColor {
113+
return b
114+
}
115+
100116
b = append(underlineColor, b...)
101117
b = append(b, resetColor...)
102118
return b

color_test.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,61 @@ import (
66
)
77

88
func Test_Color(t *testing.T) {
9-
test_GetColor(t)
9+
h := NewHandler(nil, nil)
10+
test_GetColor(t, h)
1011

1112
b := []byte("Hello")
12-
test_ColorCs(t, b)
13-
test_ColorCsf(t, b)
14-
test_ColorCsb(t, b)
15-
test_ColorUl(t, b)
13+
test_ColorCs(t, b, h)
14+
test_ColorCsf(t, b, h)
15+
test_ColorCsb(t, b, h)
16+
test_ColorUl(t, b, h)
1617
}
1718

18-
func test_GetColor(t *testing.T) {
19-
result := getColor(Black)
19+
func test_GetColor(t *testing.T, h *developHandler) {
20+
result := h.getColor(Black)
2021
expected := colors[1].fg
2122

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

26-
result = getColor(Color(20))
27+
result = h.getColor(Color(20))
2728
expected = colors[8].fg
2829

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

34-
func test_ColorCs(t *testing.T, b []byte) {
35-
result := cs(b, fgGreen)
35+
func test_ColorCs(t *testing.T, b []byte, h *developHandler) {
36+
result := h.cs(b, fgGreen)
3637

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

43-
func test_ColorCsf(t *testing.T, b []byte) {
44-
result := csf(b, fgBlue)
44+
func test_ColorCsf(t *testing.T, b []byte, h *developHandler) {
45+
result := h.csf(b, fgBlue)
4546

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

52-
func test_ColorCsb(t *testing.T, b []byte) {
53-
result := csb(b, fgYellow, bgRed)
53+
func test_ColorCsb(t *testing.T, b []byte, h *developHandler) {
54+
result := h.csb(b, fgYellow, bgRed)
5455

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

61-
func test_ColorUl(t *testing.T, b []byte) {
62-
result := ul(b)
62+
func test_ColorUl(t *testing.T, b []byte, h *developHandler) {
63+
result := h.ul(b)
6364

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

0 commit comments

Comments
 (0)