Added ANSI colors
This commit is contained in:
51
ansi/Code.go
Normal file
51
ansi/Code.go
Normal 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...))
|
||||
}
|
Reference in New Issue
Block a user