From 8ad0b2e1822de1e12342500da29d2d4c4155a44f Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 11 Mar 2024 22:08:28 +0100 Subject: [PATCH] Improved interface --- Benchmarks_test.go | 20 +++++++++++++++++++- Color.go | 28 +++++++++++++++++++--------- Color_test.go | 14 ++++++++++++++ README.md | 10 ++++++---- Terminal.go | 6 ------ 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/Benchmarks_test.go b/Benchmarks_test.go index 630f902..27403b3 100644 --- a/Benchmarks_test.go +++ b/Benchmarks_test.go @@ -19,7 +19,7 @@ func BenchmarkLCH(b *testing.B) { } } -func BenchmarkFprintColorized(b *testing.B) { +func BenchmarkFprint(b *testing.B) { color.Terminal = true c := color.RGB(1.0, 1.0, 1.0) @@ -36,3 +36,21 @@ func BenchmarkFprintRaw(b *testing.B) { c.Fprint(io.Discard, "") } } + +func BenchmarkPrint(b *testing.B) { + color.Terminal = true + c := color.RGB(1.0, 1.0, 1.0) + + for i := 0; i < b.N; i++ { + c.Print("") + } +} + +func BenchmarkPrintRaw(b *testing.B) { + color.Terminal = false + c := color.RGB(1.0, 1.0, 1.0) + + for i := 0; i < b.N; i++ { + c.Print("") + } +} diff --git a/Color.go b/Color.go index 3ffa1dc..cf80739 100644 --- a/Color.go +++ b/Color.go @@ -21,31 +21,41 @@ func RGB(r Value, g Value, b Value) Color { } // Fprint writes the text in the given color to the writer. -func (c Color) Fprint(writer io.Writer, text string) { +func (c Color) Fprint(writer io.Writer, args ...any) { if !Terminal { - fmt.Fprint(writer, text) + fmt.Fprint(writer, args...) return } - fmt.Fprintf(writer, format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) + fmt.Fprintf(writer, "\x1b[38;2;%d;%d;%dm%s\x1b[0m", byte(c.R*255), byte(c.G*255), byte(c.B*255), fmt.Sprint(args...)) } // Print writes the text in the given color to standard output. -func (c Color) Print(text string) { +func (c Color) Print(args ...any) { if !Terminal { - fmt.Print(text) + fmt.Print(args...) return } - fmt.Printf(format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) + fmt.Printf("\x1b[38;2;%d;%d;%dm%s\x1b[0m", byte(c.R*255), byte(c.G*255), byte(c.B*255), fmt.Sprint(args...)) +} + +// Print writes the text in the given color to standard output. +func (c Color) Printf(format string, args ...any) { + if !Terminal { + fmt.Printf(format, args...) + return + } + + fmt.Printf("\x1b[38;2;%d;%d;%dm%s\x1b[0m", byte(c.R*255), byte(c.G*255), byte(c.B*255), fmt.Sprintf(format, args...)) } // Println writes the text in the given color to standard output and appends a newline. -func (c Color) Println(text string) { +func (c Color) Println(args ...any) { if !Terminal { - fmt.Println(text) + fmt.Println(args...) return } - fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) + fmt.Printf("\x1b[38;2;%d;%d;%dm%s\n\x1b[0m", byte(c.R*255), byte(c.G*255), byte(c.B*255), fmt.Sprint(args...)) } diff --git a/Color_test.go b/Color_test.go index 87ad195..45646d7 100644 --- a/Color_test.go +++ b/Color_test.go @@ -36,6 +36,20 @@ func TestPrint(t *testing.T) { color.RGB(0, 0, 1).Print("blue\n") } +func TestPrintf(t *testing.T) { + color.Terminal = true + + color.RGB(1, 0, 0).Printf("%s\n", "red") + color.RGB(0, 1, 0).Printf("%s\n", "green") + color.RGB(0, 0, 1).Printf("%s\n", "blue") + + color.Terminal = false + + color.RGB(1, 0, 0).Printf("%s\n", "red") + color.RGB(0, 1, 0).Printf("%s\n", "green") + color.RGB(0, 0, 1).Printf("%s\n", "blue") +} + func TestPrintln(t *testing.T) { color.Terminal = true diff --git a/README.md b/README.md index e7b485b..fe0580d 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,12 @@ coverage: 100.0% of statements ## Benchmarks ``` -BenchmarkRGB-12 1000000000 0.3132 ns/op 0 B/op 0 allocs/op -BenchmarkLCH-12 4802006 249.8 ns/op 0 B/op 0 allocs/op -BenchmarkFprintColorized-12 6356535 188.4 ns/op 0 B/op 0 allocs/op -BenchmarkFprintRaw-12 27374659 43.76 ns/op 0 B/op 0 allocs/op +BenchmarkRGB-12 1000000000 0.3211 ns/op 0 B/op 0 allocs/op +BenchmarkLCH-12 4767306 251.8 ns/op 0 B/op 0 allocs/op +BenchmarkFprint-12 4869368 245.7 ns/op 0 B/op 0 allocs/op +BenchmarkFprintRaw-12 23155356 43.73 ns/op 0 B/op 0 allocs/op +BenchmarkPrint-12 358099 3560 ns/op 0 B/op 0 allocs/op +BenchmarkPrintRaw-12 3144412 378.9 ns/op 0 B/op 0 allocs/op ``` ## License diff --git a/Terminal.go b/Terminal.go index e2a73d8..d6ab79f 100644 --- a/Terminal.go +++ b/Terminal.go @@ -4,11 +4,5 @@ import ( "os" ) -// These constants represent the escape codes needed to display color in terminals. -const ( - format = "\x1b[38;2;%d;%d;%dm%s\x1b[0m" - formatLine = "\x1b[38;2;%d;%d;%dm%s\n\x1b[0m" -) - // Terminal is a boolean that indicates if we're in a terminal with truecolor support. var Terminal = os.Getenv("COLORTERM") == "truecolor"