From 393a9ebf17136baebf03670017fc3ab7e83d5d88 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 11 Mar 2024 23:28:37 +0100 Subject: [PATCH] Added ANSI colors --- Color.go | 2 +- ansi/Code.go | 51 ++++++++++++++++++++++++++++++++++++++++++ ansi/Code_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ ansi/Color.go | 12 ++++++++++ ansi/Format.go | 14 ++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 ansi/Code.go create mode 100644 ansi/Code_test.go create mode 100644 ansi/Color.go create mode 100644 ansi/Format.go diff --git a/Color.go b/Color.go index cf80739..cff542b 100644 --- a/Color.go +++ b/Color.go @@ -40,7 +40,7 @@ func (c Color) Print(args ...any) { 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. +// Printf formats according to a format specifier and writes the text in the given color to standard output. func (c Color) Printf(format string, args ...any) { if !Terminal { fmt.Printf(format, args...) diff --git a/ansi/Code.go b/ansi/Code.go new file mode 100644 index 0000000..037dd8d --- /dev/null +++ b/ansi/Code.go @@ -0,0 +1,51 @@ +package ansi + +import ( + "fmt" + "io" + + "git.akyoto.dev/go/color" +) + +// 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 { + fmt.Print(args...) + return + } + + fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...)) +} + +// Printf formats according to a format specifier and writes the text in the given color to standard output. +func (code Code) Printf(format string, args ...any) { + if !color.Terminal { + fmt.Printf(format, args...) + return + } + + fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprintf(format, args...)) +} + +// Println writes the text in the given color to standard output and appends a newline. +func (code Code) Println(args ...any) { + if !color.Terminal { + fmt.Println(args...) + return + } + + fmt.Printf("\x1b[%dm%s\n\x1b[0m", code, fmt.Sprint(args...)) +} diff --git a/ansi/Code_test.go b/ansi/Code_test.go new file mode 100644 index 0000000..6f2455e --- /dev/null +++ b/ansi/Code_test.go @@ -0,0 +1,57 @@ +package ansi_test + +import ( + "os" + "testing" + + "git.akyoto.dev/go/color" + "git.akyoto.dev/go/color/ansi" +) + +func Test(t *testing.T) { + color.Terminal = true + testColors() +} + +func TestPrintRaw(t *testing.T) { + color.Terminal = false + 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.White.Print("White\n") + ansi.Red.Print("Red\n") + ansi.Green.Print("Green\n") + ansi.Blue.Print("Blue\n") + ansi.Yellow.Print("Yellow\n") + ansi.Magenta.Print("Magenta\n") + ansi.Cyan.Print("Cyan\n") + + ansi.Black.Printf("%s\n", "Black") + ansi.White.Printf("%s\n", "White") + ansi.Red.Printf("%s\n", "Red") + ansi.Green.Printf("%s\n", "Green") + ansi.Blue.Printf("%s\n", "Blue") + ansi.Yellow.Printf("%s\n", "Yellow") + ansi.Magenta.Printf("%s\n", "Magenta") + ansi.Cyan.Printf("%s\n", "Cyan") + + ansi.Black.Println("Black") + ansi.White.Println("White") + ansi.Red.Println("Red") + ansi.Green.Println("Green") + ansi.Blue.Println("Blue") + ansi.Yellow.Println("Yellow") + ansi.Magenta.Println("Magenta") + ansi.Cyan.Println("Cyan") +} diff --git a/ansi/Color.go b/ansi/Color.go new file mode 100644 index 0000000..65bacda --- /dev/null +++ b/ansi/Color.go @@ -0,0 +1,12 @@ +package ansi + +const ( + Black Code = iota + 30 + Red + Green + Yellow + Blue + Magenta + Cyan + White +) diff --git a/ansi/Format.go b/ansi/Format.go new file mode 100644 index 0000000..29bfcf6 --- /dev/null +++ b/ansi/Format.go @@ -0,0 +1,14 @@ +package ansi + +const ( + Reset Code = iota + Bold + Dim + Italic + Underline + Blink + BlinkFast + Reverse + Hidden + Strikethrough +)