aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-03-08 00:33:41 +0100
committerRobin Jarry <robin@jarry.cc>2023-03-08 00:43:05 +0100
commitc477d83f2493871b215aa2f8b2668b9b84fd6295 (patch)
treeab397640d4ba6dbe848a2f721e946f276d6daa6a /lib/templates
parent56a6ed0cf71ecf55f37e94b574c41927452abc44 (diff)
downloadaerc-c477d83f2493871b215aa2f8b2668b9b84fd6295.tar.gz
templates: add switch/case functions
This allows much shorter templates. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib/templates')
-rw-r--r--lib/templates/functions.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index 8bd8f37b..b331dc85 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -12,6 +12,7 @@ import (
"git.sr.ht/~rjarry/aerc/lib/format"
"git.sr.ht/~rjarry/aerc/lib/parse"
+ "git.sr.ht/~rjarry/aerc/models"
"github.com/emersion/go-message/mail"
)
@@ -251,6 +252,33 @@ func compactDir(path string) string {
return format.CompactPath(path, os.PathSeparator)
}
+type (
+ Case struct{ expr, value string }
+ Default struct{ value string }
+)
+
+func (c *Case) Matches(s string) bool { return parse.MatchCache(s, c.expr) }
+func (c *Case) Value() string { return c.value }
+func (d *Default) Matches(s string) bool { return true }
+func (d *Default) Value() string { return d.value }
+
+func switch_(value string, cases ...models.Case) string {
+ for _, c := range cases {
+ if c.Matches(value) {
+ return c.Value()
+ }
+ }
+ return ""
+}
+
+func case_(expr, value string) models.Case {
+ return &Case{expr: expr, value: value}
+}
+
+func default_(value string) models.Case {
+ return &Default{value: value}
+}
+
var templateFuncs = template.FuncMap{
"quote": quote,
"wrapText": wrapText,
@@ -273,4 +301,7 @@ var templateFuncs = template.FuncMap{
"trimSignature": trimSignature,
"compactDir": compactDir,
"match": parse.MatchCache,
+ "switch": switch_,
+ "case": case_,
+ "default": default_,
}