88 lines
1.3 KiB
Go
88 lines
1.3 KiB
Go
|
package color
|
||
|
|
||
|
// Attribute defines a single SGR Code
|
||
|
type Attribute int
|
||
|
|
||
|
// 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
|
||
|
)
|
||
|
|
||
|
var attrCache = make(map[string]*Color)
|
||
|
|
||
|
func attr(a Attribute) *Color {
|
||
|
if c, ok := attrCache[string(a)]; ok {
|
||
|
return c
|
||
|
}
|
||
|
c := New(a)
|
||
|
attrCache[string(a)] = c
|
||
|
return c
|
||
|
}
|
||
|
|
||
|
// Format is a quick way to format a string using a single attribute
|
||
|
func (a Attribute) Format(text string) string {
|
||
|
return attr(a).Format(text)
|
||
|
}
|
||
|
|
||
|
// Format is a quick way to format a formatted string using a single attribute
|
||
|
func (a Attribute) Formatf(text string, v ...interface{}) string {
|
||
|
return attr(a).Formatf(text, v...)
|
||
|
}
|