aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-03-04 01:15:16 +0100
committerRobin Jarry <robin@jarry.cc>2023-03-08 00:43:08 +0100
commit41ae3639051869921b62c1bda51b88b6b71faec5 (patch)
treee983444625a5ebdacb9af32182b0ddd62160883a
parentc477d83f2493871b215aa2f8b2668b9b84fd6295 (diff)
downloadaerc-41ae3639051869921b62c1bda51b88b6b71faec5.tar.gz
templates: add conditional style method
Add .StyleSwitch that takes a string and an arbitrary number of cases (regexp, style). Reuse some of the constructs introduced by previous commit. The style of the first regular expression that matches will be applied. If the string does not match any of the expressions, it will be left as-is. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r--config/templates.go5
-rw-r--r--doc/aerc-templates.7.scd9
-rw-r--r--lib/state/templates.go11
-rw-r--r--models/templates.go1
4 files changed, 25 insertions, 1 deletions
diff --git a/config/templates.go b/config/templates.go
index 98be7781..ff88689f 100644
--- a/config/templates.go
+++ b/config/templates.go
@@ -6,6 +6,7 @@ import (
"git.sr.ht/~rjarry/aerc/lib/templates"
"git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/models"
"github.com/emersion/go-message/mail"
"github.com/go-ini/ini"
)
@@ -97,4 +98,6 @@ func (d *dummyData) ContentInfo() string { return "" }
func (d *dummyData) StatusInfo() string { return "" }
func (d *dummyData) TrayInfo() string { return "" }
func (d *dummyData) PendingKeys() string { return "" }
-func (d *dummyData) Style(string, string) string { return "" }
+
+func (d *dummyData) Style(string, string) string { return "" }
+func (d *dummyData) StyleSwitch(string, ...models.Case) string { return "" }
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index 7f3d6410..05084216 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -377,6 +377,15 @@ aerc provides the following additional functions:
{{.Style .Account "red"}}
```
+*.StyleSwitch*
+ Apply a user-defined style (see *aerc-stylesets*(7)) to a string if it
+ matches one of the associated regular expressions. If the string does
+ not match any of the expressions, leave it unstyled.
+
+ ```
+ {{.StyleSwitch (.From | names | join ", ") (case `Tim` "cyan") (case `Robin` "pink-blink")}}
+ ```
+
*version*
Returns the version of aerc, which can be useful for things like X-Mailer.
diff --git a/lib/state/templates.go b/lib/state/templates.go
index 1e8bad00..f971b62c 100644
--- a/lib/state/templates.go
+++ b/lib/state/templates.go
@@ -472,3 +472,14 @@ func (d *TemplateData) Style(content, name string) string {
style := cfg.GetUserStyle(name)
return parse.ApplyStyle(style, content)
}
+
+func (d *TemplateData) StyleSwitch(content string, cases ...models.Case) string {
+ for _, c := range cases {
+ if c.Matches(content) {
+ cfg := config.Ui.ForAccount(d.Account())
+ style := cfg.GetUserStyle(c.Value())
+ return parse.ApplyStyle(style, content)
+ }
+ }
+ return content
+}
diff --git a/models/templates.go b/models/templates.go
index dc984fc4..cebad5ea 100644
--- a/models/templates.go
+++ b/models/templates.go
@@ -43,6 +43,7 @@ type TemplateData interface {
TrayInfo() string
PendingKeys() string
Style(string, string) string
+ StyleSwitch(string, ...Case) string
}
type Case interface {