Files
glamour-temp-MUTN/baseelement.go
T
Christian Muehlhaeuser 39a6da5fe7 Support hex color values in style config
While we don't support extended color ranges just yet, this approximates
the hex value to the closest color value in the ANSI 256-color chart.
2019-12-05 11:43:56 +01:00

94 lines
1.7 KiB
Go

package gold
import (
"io"
"strconv"
"github.com/logrusorgru/aurora"
bf "gopkg.in/russross/blackfriday.v2"
)
type BaseElement struct {
Token string
Prefix string
Suffix string
Style StyleType
}
func renderText(w io.Writer, rules *ElementStyle, s string) {
if len(s) == 0 {
return
}
out := aurora.Reset(s)
if rules != nil {
if rules.Color != "" {
var i int
var err error
if rules.Color[0] == '#' {
i, err = hexToANSIColor(rules.Color)
} else {
i, err = strconv.Atoi(rules.Color)
}
if err == nil && i >= 0 && i <= 255 {
out = out.Index(uint8(i))
}
}
if rules.BackgroundColor != "" {
var i int
var err error
if rules.BackgroundColor[0] == '#' {
i, err = hexToANSIColor(rules.BackgroundColor)
} else {
i, err = strconv.Atoi(rules.BackgroundColor)
}
if err == nil && i >= 0 && i <= 255 {
out = out.BgIndex(uint8(i))
}
}
if rules.Underline {
out = out.Underline()
}
if rules.Bold {
out = out.Bold()
}
if rules.Italic {
out = out.Italic()
}
if rules.CrossedOut {
out = out.CrossedOut()
}
if rules.Overlined {
out = out.Overlined()
}
if rules.Inverse {
out = out.Reverse()
}
if rules.Blink {
out = out.Blink()
}
}
_, _ = w.Write([]byte(out.String()))
}
func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
renderText(w, tr.blockStack.Current().Style, e.Prefix)
defer func() {
renderText(w, tr.blockStack.Current().Style, e.Suffix)
}()
rules := tr.blockStack.With(tr.style[e.Style])
if rules != nil {
renderText(w, rules, rules.Prefix)
defer func() {
renderText(w, rules, rules.Suffix)
}()
}
renderText(w, rules, e.Token)
return nil
}