mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 00:47:15 -04:00
Support format strings in style elements
This commit is contained in:
+26
-1
@@ -1,11 +1,13 @@
|
|||||||
package gold
|
package gold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/lucasb-eyer/go-colorful"
|
"github.com/lucasb-eyer/go-colorful"
|
||||||
@@ -63,6 +65,21 @@ func colorSeq(fg *string, bg *string) (string, error) {
|
|||||||
return "", errors.New("Invalid color")
|
return "", errors.New("Invalid color")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatToken(format string, token string) (string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
v := make(map[string]interface{})
|
||||||
|
v["text"] = token
|
||||||
|
|
||||||
|
tmpl, err := template.New(format).Parse(format)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tmpl.Execute(&b, v)
|
||||||
|
return b.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
func renderText(w io.Writer, rules ElementStyle, s string) {
|
func renderText(w io.Writer, rules ElementStyle, s string) {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return
|
return
|
||||||
@@ -132,6 +149,14 @@ func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
|||||||
renderText(w, rules, rules.Suffix)
|
renderText(w, rules, rules.Suffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
renderText(w, rules, e.Token)
|
s := e.Token
|
||||||
|
if len(rules.Format) > 0 {
|
||||||
|
var err error
|
||||||
|
s, err = formatToken(rules.Format, s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
renderText(w, rules, s)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ type ElementStyle struct {
|
|||||||
Theme string `json:"theme"`
|
Theme string `json:"theme"`
|
||||||
Prefix string `json:"prefix"`
|
Prefix string `json:"prefix"`
|
||||||
Suffix string `json:"suffix"`
|
Suffix string `json:"suffix"`
|
||||||
|
Format string `json:"format"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadStyle(f string) ([]byte, error) {
|
func loadStyle(f string) ([]byte, error) {
|
||||||
@@ -119,6 +120,7 @@ func cascadeStyle(parent ElementStyle, child ElementStyle, onlyColors bool) Elem
|
|||||||
s.Blink = parent.Blink
|
s.Blink = parent.Blink
|
||||||
s.Prefix = parent.Prefix
|
s.Prefix = parent.Prefix
|
||||||
s.Suffix = parent.Suffix
|
s.Suffix = parent.Suffix
|
||||||
|
s.Format = parent.Format
|
||||||
}
|
}
|
||||||
|
|
||||||
if child.Color != nil {
|
if child.Color != nil {
|
||||||
@@ -166,6 +168,9 @@ func cascadeStyle(parent ElementStyle, child ElementStyle, onlyColors bool) Elem
|
|||||||
if child.Suffix != "" {
|
if child.Suffix != "" {
|
||||||
s.Suffix = child.Suffix
|
s.Suffix = child.Suffix
|
||||||
}
|
}
|
||||||
|
if child.Format != "" {
|
||||||
|
s.Format = child.Format
|
||||||
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user