Added terminal detection

This commit is contained in:
2024-03-12 00:25:20 +01:00
parent 393a9ebf17
commit 6a802c3ad8
12 changed files with 37 additions and 79 deletions

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")