klog/color.go

52 lines
1.7 KiB
Go
Raw Normal View History

2021-11-01 14:38:30 +00:00
package log
2021-11-02 07:55:52 +00:00
import (
"fmt"
"strconv"
"text/template"
2021-11-01 14:41:11 +00:00
2021-11-02 07:55:52 +00:00
"kumoly.io/core/log/color"
2021-11-01 14:38:30 +00:00
)
2021-11-02 07:58:08 +00:00
func (l *Logger) M(s interface{}, attrs ...color.Attribute) string {
2021-11-02 07:55:52 +00:00
if !l.color {
2021-11-02 07:58:08 +00:00
return fmt.Sprint(s)
2021-11-02 07:55:52 +00:00
}
2021-11-03 05:51:59 +00:00
styles := ""
2021-11-02 07:55:52 +00:00
for i, v := range attrs {
if i >= 3 {
break
}
if i > 0 {
2021-11-03 05:51:59 +00:00
styles += ";"
2021-11-02 07:55:52 +00:00
}
2021-11-03 05:51:59 +00:00
styles = styles + strconv.Itoa(int(v))
2021-11-02 07:55:52 +00:00
}
2021-11-03 05:51:59 +00:00
return fmt.Sprintf("\033[%sm%s\033[0m", styles, s)
2021-11-02 07:55:52 +00:00
}
2021-11-01 14:41:11 +00:00
2021-11-03 05:51:59 +00:00
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) }
2021-11-01 14:41:11 +00:00
return
}
2021-11-03 05:51:59 +00:00
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 }
2021-11-01 14:41:11 +00:00
}
// func