-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efed53b
commit a86f3dd
Showing
6 changed files
with
774 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.