Added terminal detection
This commit is contained in:
parent
393a9ebf17
commit
6a802c3ad8
@ -1,7 +1,6 @@
|
|||||||
package color_test
|
package color_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/color"
|
"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) {
|
func BenchmarkPrint(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)
|
||||||
|
11
Color.go
11
Color.go
@ -2,7 +2,6 @@ package color
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Value is a type definition for the data type of a single color component.
|
// 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}
|
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.
|
// Print writes the text in the given color to standard output.
|
||||||
func (c Color) Print(args ...any) {
|
func (c Color) Print(args ...any) {
|
||||||
if !Terminal {
|
if !Terminal {
|
||||||
|
@ -1,27 +1,12 @@
|
|||||||
package color_test
|
package color_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/assert"
|
"git.akyoto.dev/go/assert"
|
||||||
"git.akyoto.dev/go/color"
|
"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) {
|
func TestPrint(t *testing.T) {
|
||||||
color.Terminal = true
|
color.Terminal = true
|
||||||
|
|
||||||
|
18
README.md
18
README.md
@ -4,10 +4,9 @@ Adds color to your terminal output.
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- RGB color space
|
- ANSI colors
|
||||||
- LCH color space (oklch)
|
- LCH colors
|
||||||
- Truecolor terminal output
|
- RGB colors
|
||||||
- Zero dependencies (excluding tests)
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@ -28,7 +27,6 @@ orange.Println("orange text")
|
|||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
```
|
```
|
||||||
PASS: TestFprint
|
|
||||||
PASS: TestPrint
|
PASS: TestPrint
|
||||||
PASS: TestPrintf
|
PASS: TestPrintf
|
||||||
PASS: TestPrintln
|
PASS: TestPrintln
|
||||||
@ -41,12 +39,10 @@ coverage: 100.0% of statements
|
|||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
```
|
```
|
||||||
BenchmarkRGB-12 1000000000 0.3211 ns/op 0 B/op 0 allocs/op
|
BenchmarkRGB-12 1000000000 0.3141 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkLCH-12 4767306 251.8 ns/op 0 B/op 0 allocs/op
|
BenchmarkLCH-12 4780176 250.9 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkFprint-12 4869368 245.7 ns/op 0 B/op 0 allocs/op
|
BenchmarkPrint-12 375394 3343 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkFprintRaw-12 23155356 43.73 ns/op 0 B/op 0 allocs/op
|
BenchmarkPrintRaw-12 3105022 387.3 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
|
||||||
|
@ -2,7 +2,9 @@ package color
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.akyoto.dev/go/color/tty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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 = tty.IsTerminal(os.Stdout.Fd()) && os.Getenv("COLORTERM") == "truecolor"
|
||||||
|
11
ansi/Code.go
11
ansi/Code.go
@ -2,7 +2,6 @@ package ansi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.akyoto.dev/go/color"
|
"git.akyoto.dev/go/color"
|
||||||
)
|
)
|
||||||
@ -10,16 +9,6 @@ import (
|
|||||||
// Code represents an ANSI escape code.
|
// Code represents an ANSI escape code.
|
||||||
type Code int
|
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.
|
// Print writes the text in the given color to standard output.
|
||||||
func (code Code) Print(args ...any) {
|
func (code Code) Print(args ...any) {
|
||||||
if !color.Terminal {
|
if !color.Terminal {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ansi_test
|
package ansi_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/color"
|
"git.akyoto.dev/go/color"
|
||||||
@ -19,15 +18,6 @@ func TestPrintRaw(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testColors() {
|
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.Black.Print("Black\n")
|
||||||
ansi.White.Print("White\n")
|
ansi.White.Print("White\n")
|
||||||
ansi.Red.Print("Red\n")
|
ansi.Red.Print("Red\n")
|
||||||
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module git.akyoto.dev/go/color
|
|||||||
|
|
||||||
go 1.22.0
|
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
2
go.sum
@ -1,2 +1,4 @@
|
|||||||
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
|
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
|
||||||
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=
|
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
6
tty/Terminal_darwin.go
Normal 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
9
tty/Terminal_linux.go
Normal 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
6
tty/Terminal_windows.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package tty
|
||||||
|
|
||||||
|
// IsTerminal returns true if the file descriptor is a terminal.
|
||||||
|
func IsTerminal(fd uintptr) bool {
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user