klog/color.go

88 lines
1.1 KiB
Go

package klog
import (
"fmt"
"strconv"
)
func (l *Logger) M(s interface{}, attrs ...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)
}
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
)