Added terminal detection

This commit is contained in:
Eduard Urbach 2024-03-12 00:25:20 +01:00
parent 393a9ebf17
commit 6a802c3ad8
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
12 changed files with 37 additions and 79 deletions

View File

@ -1,7 +1,6 @@
package color_test
import (
"io"
"testing"
"git.akyoto.dev/go/color"
@ -19,24 +18,6 @@ func BenchmarkLCH(b *testing.B) {
}
}
func BenchmarkFprint(b *testing.B) {
color.Terminal = true
c := color.RGB(1.0, 1.0, 1.0)
for i := 0; i < b.N; i++ {
c.Fprint(io.Discard, "")
}
}
func BenchmarkFprintRaw(b *testing.B) {
color.Terminal = false
c := color.RGB(1.0, 1.0, 1.0)
for i := 0; i < b.N; i++ {
c.Fprint(io.Discard, "")
}
}
func BenchmarkPrint(b *testing.B) {
color.Terminal = true
c := color.RGB(1.0, 1.0, 1.0)

View File

@ -2,7 +2,6 @@ package color
import (
"fmt"
"io"
)
// Value is a type definition for the data type of a single color component.
@ -20,16 +19,6 @@ func RGB(r Value, g Value, b Value) Color {
return Color{r, g, b}
}
// Fprint writes the text in the given color to the writer.
func (c Color) Fprint(writer io.Writer, args ...any) {
if !Terminal {
fmt.Fprint(writer, args...)
return
}
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(args ...any) {
if !Terminal {

View File

@ -1,27 +1,12 @@
package color_test
import (
"io"
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/color"
)
func TestFprint(t *testing.T) {
color.Terminal = true
color.RGB(1, 0, 0).Fprint(io.Discard, "red")
color.RGB(0, 1, 0).Fprint(io.Discard, "green")
color.RGB(0, 0, 1).Fprint(io.Discard, "blue")
color.Terminal = false
color.RGB(1, 0, 0).Fprint(io.Discard, "red")
color.RGB(0, 1, 0).Fprint(io.Discard, "green")
color.RGB(0, 0, 1).Fprint(io.Discard, "blue")
}
func TestPrint(t *testing.T) {
color.Terminal = true

View File

@ -4,10 +4,9 @@ Adds color to your terminal output.
## Features
- RGB color space
- LCH color space (oklch)
- Truecolor terminal output
- Zero dependencies (excluding tests)
- ANSI colors
- LCH colors
- RGB colors
## Installation
@ -28,7 +27,6 @@ orange.Println("orange text")
## Tests
```
PASS: TestFprint
PASS: TestPrint
PASS: TestPrintf
PASS: TestPrintln
@ -41,12 +39,10 @@ coverage: 100.0% of statements
## Benchmarks
```
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
BenchmarkRGB-12 1000000000 0.3141 ns/op 0 B/op 0 allocs/op
BenchmarkLCH-12 4780176 250.9 ns/op 0 B/op 0 allocs/op
BenchmarkPrint-12 375394 3343 ns/op 0 B/op 0 allocs/op
BenchmarkPrintRaw-12 3105022 387.3 ns/op 0 B/op 0 allocs/op
```
## License

View File

@ -2,7 +2,9 @@ package color
import (
"os"
"git.akyoto.dev/go/color/tty"
)
// Terminal is a boolean that indicates if we're in a terminal with truecolor support.
var Terminal = os.Getenv("COLORTERM") == "truecolor"
var Terminal = tty.IsTerminal(os.Stdout.Fd()) && os.Getenv("COLORTERM") == "truecolor"

View File

@ -2,7 +2,6 @@ package ansi
import (
"fmt"
"io"
"git.akyoto.dev/go/color"
)
@ -10,16 +9,6 @@ import (
// Code represents an ANSI escape code.
type Code int
// Fprint writes the text in the given color to the writer.
func (code Code) Fprint(writer io.Writer, args ...any) {
if !color.Terminal {
fmt.Fprint(writer, args...)
return
}
fmt.Fprintf(writer, "\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...))
}
// Print writes the text in the given color to standard output.
func (code Code) Print(args ...any) {
if !color.Terminal {

View File

@ -1,7 +1,6 @@
package ansi_test
import (
"os"
"testing"
"git.akyoto.dev/go/color"
@ -19,15 +18,6 @@ func TestPrintRaw(t *testing.T) {
}
func testColors() {
ansi.Black.Fprint(os.Stdout, "Black\n")
ansi.White.Fprint(os.Stdout, "White\n")
ansi.Red.Fprint(os.Stdout, "Red\n")
ansi.Green.Fprint(os.Stdout, "Green\n")
ansi.Blue.Fprint(os.Stdout, "Blue\n")
ansi.Yellow.Fprint(os.Stdout, "Yellow\n")
ansi.Magenta.Fprint(os.Stdout, "Magenta\n")
ansi.Cyan.Fprint(os.Stdout, "Cyan\n")
ansi.Black.Print("Black\n")
ansi.White.Print("White\n")
ansi.Red.Print("Red\n")

5
go.mod
View File

@ -2,4 +2,7 @@ module git.akyoto.dev/go/color
go 1.22.0
require git.akyoto.dev/go/assert v0.1.3
require (
git.akyoto.dev/go/assert v0.1.3
golang.org/x/sys v0.18.0
)

2
go.sum
View File

@ -1,2 +1,4 @@
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

6
tty/Terminal_darwin.go Normal file
View File

@ -0,0 +1,6 @@
package tty
// IsTerminal returns true if the file descriptor is a terminal.
func IsTerminal(fd uintptr) bool {
return false
}

9
tty/Terminal_linux.go Normal file
View File

@ -0,0 +1,9 @@
package tty
import "golang.org/x/sys/unix"
// IsTerminal returns true if the file descriptor is a terminal.
func IsTerminal(fd uintptr) bool {
_, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
return err == nil
}

6
tty/Terminal_windows.go Normal file
View File

@ -0,0 +1,6 @@
package tty
// IsTerminal returns true if the file descriptor is a terminal.
func IsTerminal(fd uintptr) bool {
return false
}