aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBence Ferdinandy <bence@ferdinandy.com>2023-02-02 19:39:58 +0100
committerRobin Jarry <robin@jarry.cc>2023-02-12 00:39:18 +0100
commit2df3a079a060bb56aaa4d94df066936cb7b73f9d (patch)
tree4c3bfd7abaf012a6d759976a1d1aff5245f200a2
parentfff5e2f1bbe4e1d2afecada9b69fca0fc7cec424 (diff)
downloadaerc-2df3a079a060bb56aaa4d94df066936cb7b73f9d.tar.gz
templates: add initials function
When listing all addresses with names, in for example the To field, one might end up with a very long string. Add the initials function, which extracts the names from addresses and then shortens each of them to the initials of the name. Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--doc/aerc-templates.7.scd10
-rw-r--r--lib/templates/functions.go15
2 files changed, 25 insertions, 0 deletions
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index bfba05c4..f5998b23 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -196,6 +196,16 @@ aerc provides the following additional functions:
{{index (.To | names) 0}}
```
+*initials*
+ Extracts the initials from the names part from a mail.Address list. If
+ there is no name available, the first letter of the email address is
+ returned instead.
+
+ ```
+ {{.To | initials | join ", "}}
+ {{index (.To | initials) 0}}
+ ```
+
*emails*
Extracts the addresses part from a mail.Address list.
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index f6cb0df4..630db264 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -120,6 +120,20 @@ func names(addresses []*mail.Address) []string {
return n
}
+func initials(addresses []*mail.Address) []string {
+ n := names(addresses)
+ ret := make([]string, len(addresses))
+ for i, name := range n {
+ split := strings.Split(name, " ")
+ initial := ""
+ for _, s := range split {
+ initial += string([]rune(s)[0:1])
+ }
+ ret[i] = initial
+ }
+ return ret
+}
+
func emails(addresses []*mail.Address) []string {
e := make([]string, len(addresses))
for i, addr := range addresses {
@@ -208,6 +222,7 @@ var templateFuncs = template.FuncMap{
"exec": cmd,
"version": func() string { return version },
"names": names,
+ "initials": initials,
"emails": emails,
"mboxes": mboxes,
"persons": persons,