Fix legacy code

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/4/head
jolheiser 2021-02-16 23:39:09 -06:00
parent c628fd1987
commit 60c1edb5fe
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
5 changed files with 40 additions and 0 deletions

View File

@ -82,9 +82,15 @@ func (b *Basic) unformat() string {
return fmt.Sprintf("%s[%dm", escape, Reset) return fmt.Sprintf("%s[%dm", escape, Reset)
} }
// Format returns a string wrapped in the basic color
func (b *Basic) Format(text string) string { func (b *Basic) Format(text string) string {
if len(b.Attrs) > 0 { if len(b.Attrs) > 0 {
return b.wrap(text) return b.wrap(text)
} }
return text return text
} }
// Formatf returns a formatted string wrapped in the basic color
func (b *Basic) Formatf(format string, v ...interface{}) string {
return b.Format(fmt.Sprintf(format, v...))
}

View File

@ -47,3 +47,24 @@ const (
BgHiCyan BgHiCyan
BgHiWhite BgHiWhite
) )
var attrCache = make(map[BasicAttribute]*Basic)
func attr(a BasicAttribute) *Basic {
if c, ok := attrCache[a]; ok {
return c
}
c := New(a)
attrCache[a] = c
return c
}
// Format is a quick way to format a string using a single attribute
func (a BasicAttribute) 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 BasicAttribute) Formatf(text string, v ...interface{}) string {
return attr(a).Formatf(text, v...)
}

View File

@ -8,6 +8,7 @@ const escape = "\x1b"
type Color interface { type Color interface {
Format(text string) string Format(text string) string
Formatf(text string, v ...interface{}) string
} }
// ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info // ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info

View File

@ -43,6 +43,12 @@ func (e *Extended) unformat() string {
return fmt.Sprintf("%s[%dm", escape, Reset) return fmt.Sprintf("%s[%dm", escape, Reset)
} }
// Format returns a string wrapped in the extended color
func (e *Extended) Format(text string) string { func (e *Extended) Format(text string) string {
return e.wrap(text) return e.wrap(text)
} }
// Formatf returns a formatted string wrapped in the extended color
func (e *Extended) Formatf(text string, v ...interface{}) string {
return e.wrap(fmt.Sprintf(text, v...))
}

View File

@ -40,6 +40,12 @@ func (t *True) unformat() string {
return fmt.Sprintf("%s[%dm", escape, Reset) return fmt.Sprintf("%s[%dm", escape, Reset)
} }
// Format returns a string wrapped in the true color
func (t *True) Format(text string) string { func (t *True) Format(text string) string {
return t.wrap(text) return t.wrap(text)
} }
// Formatf returns a formatted string wrapped in the true color
func (t *True) Formatf(text string, v ...interface{}) string {
return t.wrap(fmt.Sprintf(text, v...))
}