Improved interface

This commit is contained in:
Eduard Urbach 2024-03-11 22:08:28 +01:00
parent 71dd96406c
commit 8ad0b2e182
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 58 additions and 20 deletions

View File

@ -19,7 +19,7 @@ func BenchmarkLCH(b *testing.B) {
} }
} }
func BenchmarkFprintColorized(b *testing.B) { func BenchmarkFprint(b *testing.B) {
color.Terminal = true color.Terminal = true
c := color.RGB(1.0, 1.0, 1.0) c := color.RGB(1.0, 1.0, 1.0)
@ -36,3 +36,21 @@ func BenchmarkFprintRaw(b *testing.B) {
c.Fprint(io.Discard, "") 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("")
}
}

View File

@ -21,31 +21,41 @@ func RGB(r Value, g Value, b Value) Color {
} }
// Fprint writes the text in the given color to the writer. // 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 { if !Terminal {
fmt.Fprint(writer, text) fmt.Fprint(writer, args...)
return 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. // 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 { if !Terminal {
fmt.Print(text) fmt.Print(args...)
return 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. // 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 { if !Terminal {
fmt.Println(text) fmt.Println(args...)
return 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...))
} }

View File

@ -36,6 +36,20 @@ func TestPrint(t *testing.T) {
color.RGB(0, 0, 1).Print("blue\n") 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) { func TestPrintln(t *testing.T) {
color.Terminal = true color.Terminal = true

View File

@ -40,10 +40,12 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkRGB-12 1000000000 0.3132 ns/op 0 B/op 0 allocs/op BenchmarkRGB-12 1000000000 0.3211 ns/op 0 B/op 0 allocs/op
BenchmarkLCH-12 4802006 249.8 ns/op 0 B/op 0 allocs/op BenchmarkLCH-12 4767306 251.8 ns/op 0 B/op 0 allocs/op
BenchmarkFprintColorized-12 6356535 188.4 ns/op 0 B/op 0 allocs/op BenchmarkFprint-12 4869368 245.7 ns/op 0 B/op 0 allocs/op
BenchmarkFprintRaw-12 27374659 43.76 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 ## License

View File

@ -4,11 +4,5 @@ import (
"os" "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. // Terminal is a boolean that indicates if we're in a terminal with truecolor support.
var Terminal = os.Getenv("COLORTERM") == "truecolor" var Terminal = os.Getenv("COLORTERM") == "truecolor"