diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/state/templates.go | 20 | ||||
-rw-r--r-- | lib/templates/functions.go | 30 |
2 files changed, 50 insertions, 0 deletions
diff --git a/lib/state/templates.go b/lib/state/templates.go index 8c7df3e8..36f7b725 100644 --- a/lib/state/templates.go +++ b/lib/state/templates.go @@ -560,3 +560,23 @@ func (d *templateData) StyleSwitch(content string, cases ...models.Case) string } return content } + +func (d *templateData) StyleMap(elems []string, cases ...models.Case) []string { + mapped := make([]string, 0, len(elems)) +top: + for _, e := range elems { + for _, c := range cases { + if c.Matches(e) { + if c.Skip() { + continue top + } + cfg := config.Ui.ForAccount(d.Account()) + style := cfg.GetUserStyle(c.Value()) + e = parse.ApplyStyle(style, e) + break + } + } + mapped = append(mapped, e) + } + return mapped +} diff --git a/lib/templates/functions.go b/lib/templates/functions.go index b331dc85..9380bc7b 100644 --- a/lib/templates/functions.go +++ b/lib/templates/functions.go @@ -255,12 +255,18 @@ func compactDir(path string) string { type ( Case struct{ expr, value string } Default struct{ value string } + Exclude struct{ expr string } ) func (c *Case) Matches(s string) bool { return parse.MatchCache(s, c.expr) } func (c *Case) Value() string { return c.value } +func (c *Case) Skip() bool { return false } func (d *Default) Matches(s string) bool { return true } func (d *Default) Value() string { return d.value } +func (d *Default) Skip() bool { return false } +func (e *Exclude) Matches(s string) bool { return parse.MatchCache(s, e.expr) } +func (e *Exclude) Value() string { return "" } +func (e *Exclude) Skip() bool { return true } func switch_(value string, cases ...models.Case) string { for _, c := range cases { @@ -279,6 +285,28 @@ func default_(value string) models.Case { return &Default{value: value} } +func exclude(expr string) models.Case { + return &Exclude{expr: expr} +} + +func map_(elements []string, cases ...models.Case) []string { + mapped := make([]string, 0, len(elements)) +top: + for _, e := range elements { + for _, c := range cases { + if c.Matches(e) { + if c.Skip() { + continue top + } + e = c.Value() + break + } + } + mapped = append(mapped, e) + } + return mapped +} + var templateFuncs = template.FuncMap{ "quote": quote, "wrapText": wrapText, @@ -304,4 +332,6 @@ var templateFuncs = template.FuncMap{ "switch": switch_, "case": case_, "default": default_, + "map": map_, + "exclude": exclude, } |