klog/color.go

52 lines
1.7 KiB
Go

package log
import (
"fmt"
"strconv"
"text/template"
"kumoly.io/lib/klog/color"
)
func (l *Logger) M(s interface{}, attrs ...color.Attribute) string {
if !l.color {
return fmt.Sprint(s)
}
styles := ""
for i, v := range attrs {
if i >= 3 {
break
}
if i > 0 {
styles += ";"
}
styles = styles + strconv.Itoa(int(v))
}
return fmt.Sprintf("\033[%sm%s\033[0m", styles, s)
}
func (l *Logger) setColorMap(funcMap template.FuncMap) {
funcMap["M"] = l.M
if l.color {
funcMap["red"] = func(s interface{}) string { return fmt.Sprintf("\033[91m%s\033[0m", s) }
funcMap["redl"] = func(s interface{}) string { return fmt.Sprintf("\033[31m%s\033[0m", s) }
funcMap["green"] = func(s interface{}) string { return fmt.Sprintf("\033[92m%s\033[0m", s) }
funcMap["yellow"] = func(s interface{}) string { return fmt.Sprintf("\033[93m%s\033[0m", s) }
funcMap["blue"] = func(s interface{}) string { return fmt.Sprintf("\033[94m%s\033[0m", s) }
funcMap["magenta"] = func(s interface{}) string { return fmt.Sprintf("\033[95m%s\033[0m", s) }
funcMap["cyan"] = func(s interface{}) string { return fmt.Sprintf("\033[96m%s\033[0m", s) }
funcMap["white"] = func(s interface{}) string { return fmt.Sprintf("\033[97m%s\033[0m", s) }
return
}
funcMap["red"] = func(s string) string { return s }
funcMap["redl"] = func(s string) string { return s }
funcMap["green"] = func(s string) string { return s }
funcMap["yellow"] = func(s string) string { return s }
funcMap["blue"] = func(s string) string { return s }
funcMap["magenta"] = func(s string) string { return s }
funcMap["cyan"] = func(s string) string { return s }
funcMap["white"] = func(s string) string { return s }
}
// func