klog/color.go

53 lines
1.4 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
}
style := ""
for i, v := range attrs {
if i >= 3 {
break
}
if i > 0 {
style += ";"
}
style = style + strconv.Itoa(int(v))
}
return fmt.Sprintf("\033[%sm%s\033[0m", style, s)
}
2021-11-01 14:41:11 +00:00
func setColorMap(funcMap template.FuncMap, color bool) {
if color {
funcMap["red"] = func() string { return "\033[91m" }
funcMap["redl"] = func() string { return "\033[31m" }
funcMap["green"] = func() string { return "\033[92m" }
funcMap["yellow"] = func() string { return "\033[93m" }
funcMap["blue"] = func() string { return "\033[94m" }
funcMap["magenta"] = func() string { return "\033[95m" }
funcMap["cyan"] = func() string { return "\033[96m" }
funcMap["white"] = func() string { return "\033[97m" }
funcMap["reset"] = func() string { return "\033[0m" }
return
}
funcMap["red"] = func() string { return "" }
funcMap["redl"] = func() string { return "" }
funcMap["green"] = func() string { return "" }
funcMap["yellow"] = func() string { return "" }
funcMap["blue"] = func() string { return "" }
funcMap["magenta"] = func() string { return "" }
funcMap["cyan"] = func() string { return "" }
funcMap["white"] = func() string { return "" }
funcMap["reset"] = func() string { return "" }
}
// func