56 lines
878 B
Go
56 lines
878 B
Go
|
package color
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// Whether the terminal supports true color
|
||
|
var TrueColor = os.Getenv("COLORTERM") == "truecolor"
|
||
|
|
||
|
// Color code
|
||
|
type Code int
|
||
|
|
||
|
// Color codes
|
||
|
const (
|
||
|
Black Code = iota + 30
|
||
|
Red
|
||
|
Green
|
||
|
Yellow
|
||
|
Blue
|
||
|
Magenta
|
||
|
Cyan
|
||
|
White
|
||
|
)
|
||
|
|
||
|
func (code Code) String(args ...any) string {
|
||
|
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...))
|
||
|
}
|
||
|
|
||
|
func (code Code) Print(args ...any) {
|
||
|
if !TrueColor {
|
||
|
fmt.Print(args...)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...))
|
||
|
}
|
||
|
|
||
|
func (code Code) Printf(format string, args ...any) {
|
||
|
if !TrueColor {
|
||
|
fmt.Printf(format, args...)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (code Code) Println(args ...any) {
|
||
|
if !TrueColor {
|
||
|
fmt.Println(args...)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
fmt.Printf("\x1b[%dm%s\x1b[0m\n", code, fmt.Sprint(args...))
|
||
|
}
|