aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates/functions.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-12-20 18:37:16 +0100
committerRobin Jarry <robin@jarry.cc>2023-01-04 22:57:31 +0100
commitd758441fe0c4da6bc3486d0643436ad7ac290a56 (patch)
tree8d8fe289a4612c2dd5b8043028cf5a813502b0f6 /lib/templates/functions.go
parentae675b491d2b55a06588e8ab4ce8205aaae796c8 (diff)
downloadaerc-d758441fe0c4da6bc3486d0643436ad7ac290a56.tar.gz
templates: add more fields and functions
Add functions and fields in preparation for more than only message templates. The idea is to reuse the same symbols for the message list format and other parts of the UI. Update the man page accordingly. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib/templates/functions.go')
-rw-r--r--lib/templates/functions.go100
1 files changed, 93 insertions, 7 deletions
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index 41c899c8..2e551594 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -2,10 +2,15 @@ package templates
import (
"bytes"
+ "fmt"
+ "os"
"os/exec"
"strings"
"text/template"
"time"
+
+ "git.sr.ht/~rjarry/aerc/lib/format"
+ "github.com/emersion/go-message/mail"
)
var version string
@@ -102,12 +107,93 @@ func toLocal(t time.Time) time.Time {
return time.Time.In(t, time.Local)
}
+func names(addresses []*mail.Address) []string {
+ n := make([]string, len(addresses))
+ for i, addr := range addresses {
+ name := addr.Name
+ if name == "" {
+ name = addr.Address
+ }
+ n[i] = name
+ }
+ return n
+}
+
+func emails(addresses []*mail.Address) []string {
+ e := make([]string, len(addresses))
+ for i, addr := range addresses {
+ e[i] = addr.Address
+ }
+ return e
+}
+
+func mboxes(addresses []*mail.Address) []string {
+ e := make([]string, len(addresses))
+ for i, addr := range addresses {
+ parts := strings.SplitN(addr.Address, "@", 1)
+ e[i] = parts[0]
+ }
+ return e
+}
+
+func persons(addresses []*mail.Address) []string {
+ e := make([]string, len(addresses))
+ for i, addr := range addresses {
+ e[i] = format.AddressForHumans(addr)
+ }
+ return e
+}
+
+var units = []string{"K", "M", "G", "T"}
+
+func humanReadable(value uint32) string {
+ if value < 1000 {
+ return fmt.Sprintf("%d", value)
+ }
+ val := float64(value)
+ unit := ""
+ for i := 0; val >= 1000 && i < len(units); i++ {
+ unit = units[i]
+ val /= 1000.0
+ }
+ if val < 100.0 {
+ return fmt.Sprintf("%.1f%s", val, unit)
+ }
+ return fmt.Sprintf("%.0f%s", val, unit)
+}
+
+func cwd() string {
+ path, err := os.Getwd()
+ if err != nil {
+ return err.Error()
+ }
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return err.Error()
+ }
+ if strings.HasPrefix(path, home) {
+ path = strings.Replace(path, home, "~", 1)
+ }
+ return path
+}
+
+func join(sep string, elems []string) string {
+ return strings.Join(elems, sep)
+}
+
var templateFuncs = template.FuncMap{
- "quote": quote,
- "wrapText": wrapText,
- "wrap": wrap,
- "dateFormat": time.Time.Format,
- "toLocal": toLocal,
- "exec": cmd,
- "version": func() string { return version },
+ "quote": quote,
+ "wrapText": wrapText,
+ "wrap": wrap,
+ "dateFormat": time.Time.Format,
+ "toLocal": toLocal,
+ "exec": cmd,
+ "version": func() string { return version },
+ "names": names,
+ "emails": emails,
+ "mboxes": mboxes,
+ "persons": persons,
+ "humanReadable": humanReadable,
+ "cwd": cwd,
+ "join": join,
}