Added ANSI colors

This commit is contained in:
Eduard Urbach 2024-03-11 23:28:37 +01:00
parent 6de4715030
commit 393a9ebf17
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 135 additions and 1 deletions

View File

@ -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...)) 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) { func (c Color) Printf(format string, args ...any) {
if !Terminal { if !Terminal {
fmt.Printf(format, args...) fmt.Printf(format, args...)

51
ansi/Code.go Normal file
View File

@ -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...))
}

57
ansi/Code_test.go Normal file
View File

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

12
ansi/Color.go Normal file
View File

@ -0,0 +1,12 @@
package ansi
const (
Black Code = iota + 30
Red
Green
Yellow
Blue
Magenta
Cyan
White
)

14
ansi/Format.go Normal file
View File

@ -0,0 +1,14 @@
package ansi
const (
Reset Code = iota
Bold
Dim
Italic
Underline
Blink
BlinkFast
Reverse
Hidden
Strikethrough
)