klog/color.go

88 lines
1.1 KiB
Go
Raw Normal View History

2021-11-03 19:02:27 +00:00
package klog
2021-11-01 14:38:30 +00:00
2021-11-02 07:55:52 +00:00
import (
"fmt"
"strconv"
2021-11-01 14:38:30 +00:00
)
func (l *Logger) M(s interface{}, attrs ...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
}
type Attribute int
// Base attributes
const (
Reset Attribute = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
// Foreground text colors
const (
FgBlack Attribute = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
// Foreground Hi-Intensity text colors
const (
FgHiBlack Attribute = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Background text colors
const (
BgBlack Attribute = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
// Background Hi-Intensity text colors
const (
BgHiBlack Attribute = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)