Skip to content

Commit

Permalink
total rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
david-littlefarmer committed Aug 30, 2023
1 parent efed53b commit a86f3dd
Show file tree
Hide file tree
Showing 6 changed files with 774 additions and 379 deletions.
2 changes: 1 addition & 1 deletion attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (a attributes) Less(i, j int) bool {
func (a attributes) padding(c foregroundColor) int {
var padding int
for _, e := range a {
color := len(cs(e.Key, c))
color := len(cs([]byte(e.Key), c))
if color > padding {
padding = color
}
Expand Down
21 changes: 15 additions & 6 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import (
"testing"
)

func Test_AttributesLen(t *testing.T) {
func Test_Attributes(t *testing.T) {
test_AttributesLen(t)
test_AttributesSwap(t)
test_AttributesLess(t)
test_AttributesLessGroupTrue(t)
test_AttributesLessGroupFalse(t)
test_AttributesPadding(t)
}

func test_AttributesLen(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand All @@ -21,7 +30,7 @@ func Test_AttributesLen(t *testing.T) {
}
}

func Test_AttributesSwap(t *testing.T) {
func test_AttributesSwap(t *testing.T) {
attr1 := slog.Attr{Key: "key1", Value: slog.StringValue("value")}
attr2 := slog.Attr{Key: "key2", Value: slog.StringValue("value")}
attrs := attributes{
Expand All @@ -36,7 +45,7 @@ func Test_AttributesSwap(t *testing.T) {
}
}

func Test_AttributesLess(t *testing.T) {
func test_AttributesLess(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand All @@ -50,7 +59,7 @@ func Test_AttributesLess(t *testing.T) {
}
}

func Test_AttributesLessGroupTrue(t *testing.T) {
func test_AttributesLessGroupTrue(t *testing.T) {
attrs := attributes{
slog.String("key1", "someValue"),
slog.Group("key2", slog.String("someString", "someValue")),
Expand All @@ -63,7 +72,7 @@ func Test_AttributesLessGroupTrue(t *testing.T) {
}
}

func Test_AttributesLessGroupFalse(t *testing.T) {
func test_AttributesLessGroupFalse(t *testing.T) {
attrs := attributes{
slog.Group("key1", slog.String("someString", "someValue")),
slog.String("key2", "someValue"),
Expand All @@ -76,7 +85,7 @@ func Test_AttributesLessGroupFalse(t *testing.T) {
}
}

func Test_AttributesPadding(t *testing.T) {
func test_AttributesPadding(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand Down
74 changes: 41 additions & 33 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
package devslog

import "fmt"

type (
foregroundColor string
backgroundColor string
commonValuesColor string
foregroundColor []byte
backgroundColor []byte
commonValuesColor []byte
)

const (
var (
// Foreground colors
fgBlack foregroundColor = "\x1b[30m"
fgRed foregroundColor = "\x1b[31m"
fgGreen foregroundColor = "\x1b[32m"
fgYellow foregroundColor = "\x1b[33m"
fgBlue foregroundColor = "\x1b[34m"
fgMagenta foregroundColor = "\x1b[35m"
fgCyan foregroundColor = "\x1b[36m"
fgWhite foregroundColor = "\x1b[37m"
fgBlack foregroundColor = []byte("\x1b[30m")
fgRed foregroundColor = []byte("\x1b[31m")
fgGreen foregroundColor = []byte("\x1b[32m")
fgYellow foregroundColor = []byte("\x1b[33m")
fgBlue foregroundColor = []byte("\x1b[34m")
fgMagenta foregroundColor = []byte("\x1b[35m")
fgCyan foregroundColor = []byte("\x1b[36m")
fgWhite foregroundColor = []byte("\x1b[37m")

// Background colors
bgBlack backgroundColor = "\x1b[40m"
bgRed backgroundColor = "\x1b[41m"
bgGreen backgroundColor = "\x1b[42m"
bgYellow backgroundColor = "\x1b[43m"
bgBlue backgroundColor = "\x1b[44m"
bgMagenta backgroundColor = "\x1b[45m"
bgCyan backgroundColor = "\x1b[46m"
bgWhite backgroundColor = "\x1b[47m"
bgBlack backgroundColor = []byte("\x1b[40m")
bgRed backgroundColor = []byte("\x1b[41m")
bgGreen backgroundColor = []byte("\x1b[42m")
bgYellow backgroundColor = []byte("\x1b[43m")
bgBlue backgroundColor = []byte("\x1b[44m")
bgMagenta backgroundColor = []byte("\x1b[45m")
bgCyan backgroundColor = []byte("\x1b[46m")
bgWhite backgroundColor = []byte("\x1b[47m")

// Common consts
resetColor commonValuesColor = "\x1b[0m"
faintColor commonValuesColor = "\x1b[2m"
underlineColor commonValuesColor = "\x1b[4m"
resetColor commonValuesColor = []byte("\x1b[0m")
faintColor commonValuesColor = []byte("\x1b[2m")
underlineColor commonValuesColor = []byte("\x1b[4m")
)

// Color string foreground
func cs(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v", fgColor, text, resetColor)
func cs(b []byte, fgColor foregroundColor) []byte {
b = append(fgColor, b...)
b = append(b, resetColor...)
return b
}

// Color string fainted
func csf(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, faintColor, text, resetColor)
func csf(b []byte, fgColor foregroundColor) []byte {
b = append(fgColor, b...)
b = append(faintColor, b...)
b = append(b, resetColor...)
return b
}

// Color string background
func csb(text string, fgColor foregroundColor, bgColor backgroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, bgColor, text, resetColor)
func csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
b = append(fgColor, b...)
b = append(bgColor, b...)
b = append(b, resetColor...)
return b
}

// Underline text
func ul(text string) string {
return fmt.Sprintf("%v%v%v", underlineColor, text, resetColor)
func ul(b []byte) []byte {
b = append(underlineColor, b...)
b = append(b, resetColor...)
return b
}
49 changes: 29 additions & 20 deletions color_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package devslog

import (
"bytes"
"testing"
)

func Test_ColorCs(t *testing.T) {
expected := "\x1b[32mHello\x1b[0m"
result := cs("Hello", fgGreen)
func Test_Color(t *testing.T) {
b := []byte("Hello")
test_ColorCs(t, b)
test_ColorCsf(t, b)
test_ColorCsb(t, b)
test_ColorUl(t, b)
}

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

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
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) {
expected := "\x1b[34m\x1b[2mHello\x1b[0m"
result := csf("Hello", fgBlue)
func test_ColorCsf(t *testing.T, b []byte) {
result := csf(b, fgBlue)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
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) {
expected := "\x1b[35m\x1b[43mHello\x1b[0m"
result := csb("Hello", fgMagenta, bgYellow)
func test_ColorCsb(t *testing.T, b []byte) {
result := csb(b, fgYellow, bgRed)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
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) {
expected := "\x1b[4mHello\x1b[0m"
result := ul("Hello")
func test_ColorUl(t *testing.T, b []byte) {
result := ul(b)

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

0 comments on commit a86f3dd

Please sign in to comment.