41 lines
887 B
Go
Raw Normal View History

2024-03-11 22:28:37 +00:00
package ansi
import (
"fmt"
"git.akyoto.dev/go/color"
)
// Code represents an ANSI escape code.
type Code int
// 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...))
}