From 2df3a079a060bb56aaa4d94df066936cb7b73f9d Mon Sep 17 00:00:00 2001 From: Bence Ferdinandy Date: Thu, 2 Feb 2023 19:39:58 +0100 Subject: 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 Acked-by: Robin Jarry --- lib/templates/functions.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') 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, -- cgit