go-shout/color.go

41 lines
599 B
Go

package main
import "os"
var colorEnabled = detectColor()
func detectColor() bool {
if os.Getenv("NO_COLOR") != "" {
return false
}
if os.Getenv("TERM") == "dumb" {
return false
}
fi, err := os.Stdout.Stat()
if err != nil {
return false
}
return fi.Mode()&os.ModeCharDevice != 0
}
func red(s string) string {
if !colorEnabled {
return s
}
return "\033[31m" + s + "\033[0m"
}
func green(s string) string {
if !colorEnabled {
return s
}
return "\033[32m" + s + "\033[0m"
}
func dim(s string) string {
if !colorEnabled {
return s
}
return "\033[2m" + s + "\033[0m"
}