From 24f91b0277a1034f731aaf00c3e1f894c5ad27c1 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Sun, 12 Feb 2023 01:36:37 +0100 Subject: templates: add firstnames and shortmboxes functions These can be handy for tab-title-composer. Signed-off-by: Robin Jarry Reviewed-by: Moritz Poldrack --- doc/aerc-templates.7.scd | 21 ++++++++++++++++++++- lib/templates/functions.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd index 5f208067..c7bb0d1f 100644 --- a/doc/aerc-templates.7.scd +++ b/doc/aerc-templates.7.scd @@ -198,13 +198,23 @@ aerc provides the following additional functions: *names* Extracts the names part from a mail.Address list. If there is no name - available, the email address is returned instead. + available, the mbox (email address without @domain) is returned instead. ``` {{.To | names | join ", "}} {{index (.To | names) 0}} ``` +*firstnames* + Extracts the first names part from a mail.Address list. If there is no + name available, the short mbox (start of email address without @domain) + is returned instead. + + ``` + {{.To | firstnames | join ", "}} + {{index (.To | firstnames) 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 @@ -232,6 +242,15 @@ aerc provides the following additional functions: {{index (.To | mboxes) 0}} ``` +*shortmboxes* + Extracts the short mbox part from a mail.Address list (i.e. _smith_ from + _smith.and.wesson@example.com_). + + ``` + {{.To | shortmboxes | join ", "}} + {{index (.To | shortmboxes) 0}} + ``` + *persons* Formats a list of mail.Address into a list of strings containing the human readable form of RFC5322 (e.g. _Firstname Lastname diff --git a/lib/templates/functions.go b/lib/templates/functions.go index ec11c1c7..5d2f1928 100644 --- a/lib/templates/functions.go +++ b/lib/templates/functions.go @@ -113,7 +113,25 @@ func names(addresses []*mail.Address) []string { for i, addr := range addresses { name := addr.Name if name == "" { - name = addr.Address + parts := strings.SplitN(addr.Address, "@", 2) + name = parts[0] + } + n[i] = name + } + return n +} + +func firstnames(addresses []*mail.Address) []string { + n := make([]string, len(addresses)) + for i, addr := range addresses { + var name string + if addr.Name == "" { + parts := strings.SplitN(addr.Address, "@", 2) + parts = strings.SplitN(parts[0], ".", 2) + name = parts[0] + } else { + parts := strings.SplitN(addr.Name, " ", 2) + name = parts[0] } n[i] = name } @@ -151,6 +169,16 @@ func mboxes(addresses []*mail.Address) []string { return e } +func shortmboxes(addresses []*mail.Address) []string { + e := make([]string, len(addresses)) + for i, addr := range addresses { + parts := strings.SplitN(addr.Address, "@", 2) + parts = strings.SplitN(parts[0], ".", 2) + e[i] = parts[0] + } + return e +} + func persons(addresses []*mail.Address) []string { e := make([]string, len(addresses)) for i, addr := range addresses { @@ -227,9 +255,11 @@ var templateFuncs = template.FuncMap{ "exec": cmd, "version": func() string { return version }, "names": names, + "firstnames": firstnames, "initials": initials, "emails": emails, "mboxes": mboxes, + "shortmboxes": shortmboxes, "persons": persons, "humanReadable": humanReadable, "cwd": cwd, -- cgit