logfrog-go/formatters/colors.go

105 lines
1.9 KiB
Go

package formatters
import (
"strconv"
"codeark.it/Bithero-Agency-Go/logfrog-go"
)
type CliOption int
// Style options
const (
Reset CliOption = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
// Foreground colors
const (
FgBlack CliOption = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
const (
FgHiBlack CliOption = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Background colors
const (
BgBlack CliOption = iota + 30
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
const (
BgHiBlack CliOption = iota + 90
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)
var levelToColor = map[logfrog.LogLevel][]CliOption{
logfrog.TRACE: {Bold, FgCyan},
logfrog.DEBUG: {Bold, FgBlue},
logfrog.INFO: {Bold, FgGreen},
logfrog.WARN: {Bold, FgYellow},
logfrog.ERROR: {Bold, FgRed},
logfrog.CRITICAL: {Bold, BgMagenta},
logfrog.FATAL: {Bold, BgRed},
}
func ColorForLevel(l logfrog.LogLevel) []CliOption {
return levelToColor[l]
}
// --------------------------------------------------------------------------------
func ColorBytes(attrs ...CliOption) []byte {
bytes := make([]byte, 0, 20)
bytes = append(bytes, '\033', '[')
if len(attrs) > 0 {
bytes = append(bytes, strconv.Itoa(int(attrs[0]))...)
for _, a := range attrs[1:] {
bytes = append(bytes, ';')
bytes = append(bytes, strconv.Itoa(int(a))...)
}
} else {
bytes = append(bytes, strconv.Itoa(int(Bold))...)
}
bytes = append(bytes, 'm')
return bytes
}
func AppendColors(buf []byte, attrs ...CliOption) []byte {
return append(buf, ColorBytes(attrs...)...)
}