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 --- lib/templates/functions.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'lib') 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