169 lines
2.7 KiB
Go
169 lines
2.7 KiB
Go
package color
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Color defines a custom color object which is defined by SGR parameters.
|
|
type Color struct {
|
|
Attrs []Attribute
|
|
}
|
|
|
|
// Attribute defines a single SGR Code
|
|
type Attribute int
|
|
|
|
const escape = "\x1b"
|
|
|
|
// Logger 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
|
|
)
|
|
|
|
// New returns a new Color with attrs Attributes
|
|
func New(attrs ...Attribute) *Color {
|
|
return &Color{Attrs: attrs}
|
|
}
|
|
|
|
// Add adds new Attributes to a Color
|
|
func (c *Color) Add(attrs ...Attribute) {
|
|
for _, attr := range attrs {
|
|
has := false
|
|
for _, att := range c.Attrs {
|
|
if attr == att {
|
|
has = true
|
|
break
|
|
}
|
|
}
|
|
if !has {
|
|
c.Attrs = append(c.Attrs, attr)
|
|
}
|
|
}
|
|
}
|
|
|
|
// String returns a string representation of the sum of a Color's Attributes
|
|
func (c *Color) String() string {
|
|
attrs := 0
|
|
for _, attr := range c.Attrs {
|
|
attrs += int(attr)
|
|
}
|
|
return fmt.Sprintf("%d", attrs)
|
|
}
|
|
|
|
func (c *Color) sequence() string {
|
|
format := make([]string, len(c.Attrs))
|
|
for i, v := range c.Attrs {
|
|
format[i] = strconv.Itoa(int(v))
|
|
}
|
|
|
|
return strings.Join(format, ";")
|
|
}
|
|
|
|
func (c *Color) wrap(s string) string {
|
|
return c.format() + s + c.unformat()
|
|
}
|
|
|
|
func (c *Color) format() string {
|
|
return fmt.Sprintf("%s[%sm", escape, c.sequence())
|
|
}
|
|
|
|
func (c *Color) unformat() string {
|
|
return fmt.Sprintf("%s[%dm", escape, Reset)
|
|
}
|
|
|
|
func (c *Color) Format(text string) string {
|
|
if len(c.Attrs) > 0 {
|
|
return c.wrap(text)
|
|
}
|
|
return text
|
|
}
|
|
|
|
// ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info
|
|
func ParseLevel(level string) *Color {
|
|
switch strings.ToUpper(level) {
|
|
case "T", "TRACE":
|
|
return Trace
|
|
case "D", "DEBUG":
|
|
return Debug
|
|
case "I", "INFO":
|
|
return Info
|
|
case "W", "WARN":
|
|
return Warn
|
|
case "E", "ERROR":
|
|
return Error
|
|
case "F", "FATAL":
|
|
return Fatal
|
|
}
|
|
return Info
|
|
}
|
|
|
|
var (
|
|
Trace = New(Bold, FgCyan)
|
|
Debug = New(Bold, FgBlue)
|
|
Info = New(Bold, FgGreen)
|
|
Warn = New(Bold, FgYellow)
|
|
Error = New(Bold, FgRed)
|
|
Fatal = New(Bold, BgRed)
|
|
Default = New()
|
|
Time = Default
|
|
Stack = Default
|
|
)
|