aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2023-07-26 11:18:24 +0200
committerRobin Jarry <robin@jarry.cc>2023-08-03 22:31:34 +0200
commitd29c9d1a2ff82234ad1810abc6a57199340e7fd5 (patch)
tree5d87ff8313f12c288bc41466453b0ed139a20c8b
parent0e09c05937913a938bc4987db2b6d193ed0501bd (diff)
downloadaerc-d29c9d1a2ff82234ad1810abc6a57199340e7fd5.tar.gz
aerc-d29c9d1a2ff82234ad1810abc6a57199340e7fd5.zip
templates: attach directly from templates
Attach a file from templates. Add a split template function. {{- .Attach "LICENSE" -}} or {{range (exec "find ./doc -type f -name *.scd" "" | split "\n") -}} {{with . }} {{- $.Attach . -}} {{- end}} {{- end}} Fixes: https://todo.sr.ht/~rjarry/aerc/109 Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--commands/commands_test.go1
-rw-r--r--config/templates.go1
-rw-r--r--doc/aerc-templates.7.scd14
-rw-r--r--lib/state/templates.go19
-rw-r--r--lib/templates/functions.go9
-rw-r--r--models/templates.go1
-rw-r--r--widgets/compose.go2
7 files changed, 46 insertions, 1 deletions
diff --git a/commands/commands_test.go b/commands/commands_test.go
index c01019d0..a1518aaa 100644
--- a/commands/commands_test.go
+++ b/commands/commands_test.go
@@ -84,6 +84,7 @@ func (d *dummyData) Labels() []string { return nil }
func (d *dummyData) Flags() []string { return nil }
func (d *dummyData) IsReplied() bool { return true }
func (d *dummyData) HasAttachment() bool { return true }
+func (d *dummyData) Attach(string) string { return "" }
func (d *dummyData) IsRecent() bool { return false }
func (d *dummyData) IsUnread() bool { return false }
func (d *dummyData) IsFlagged() bool { return false }
diff --git a/config/templates.go b/config/templates.go
index ef304ae2..3f0249df 100644
--- a/config/templates.go
+++ b/config/templates.go
@@ -80,6 +80,7 @@ func (d *dummyData) ThreadCount() int { return 0 }
func (d *dummyData) ThreadFolded() bool { return false }
func (d *dummyData) Subject() string { return "Re: [PATCH] hey" }
func (d *dummyData) SubjectBase() string { return "[PATCH] hey" }
+func (d *dummyData) Attach(string) string { return "" }
func (d *dummyData) Number() int { return 0 }
func (d *dummyData) Labels() []string { return nil }
func (d *dummyData) Flags() []string { return nil }
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index 19a7bd36..dee44eee 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -273,6 +273,13 @@ aerc provides the following additional functions:
{{.To | names | join ", "}}
```
+*split*
+ Split a string into a string slice with a separator:
+
+ ```
+ {{.To | names | join ", " | split ", "}}
+ ```
+
*names*
Extracts the names part from a mail.Address list. If there is no name
available, the mbox (email address without @domain) is returned instead.
@@ -338,6 +345,13 @@ aerc provides the following additional functions:
{{index (.To | persons) 0}}
```
+*.Attach*
+ Attaches a file to the message being composed.
+
+ ```
+ {{.Attach '/usr/libexec/aerc/filters/html'}}
+ ```
+
*exec*
Execute external command, provide the second argument to its stdin.
diff --git a/lib/state/templates.go b/lib/state/templates.go
index 7b877c76..22d06589 100644
--- a/lib/state/templates.go
+++ b/lib/state/templates.go
@@ -12,11 +12,16 @@ import (
"github.com/emersion/go-message/mail"
)
+type Composer interface {
+ AddAttachment(string)
+}
+
type DataSetter interface {
Data() models.TemplateData
SetHeaders(*mail.Header, *models.OriginalMail)
SetInfo(*models.MessageInfo, int, bool)
SetThreading(string, bool, int, bool)
+ SetComposer(Composer)
SetAccount(*config.AccountConfig)
SetFolder(*models.Directory)
SetRUE([]string, func(string) (int, int, int))
@@ -53,6 +58,8 @@ type templateData struct {
state *AccountState
pendingKeys []config.KeyStroke
+
+ composer Composer
}
func NewDataSetter() DataSetter {
@@ -102,6 +109,10 @@ func (d *templateData) SetFolder(folder *models.Directory) {
d.folder = folder
}
+func (d *templateData) SetComposer(c Composer) {
+ d.composer = c
+}
+
func (d *templateData) SetRUE(folders []string,
cb func(string) (int, int, int),
) {
@@ -117,6 +128,14 @@ func (d *templateData) SetPendingKeys(keys []config.KeyStroke) {
d.pendingKeys = keys
}
+func (d *templateData) Attach(s string) string {
+ if d.composer != nil {
+ d.composer.AddAttachment(s)
+ return ""
+ }
+ return fmt.Sprintf("Failed to attach: %s", s)
+}
+
func (d *templateData) Account() string {
if d.account != nil {
return d.account.Name
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index f083c8fb..692974fd 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -240,6 +240,14 @@ func join(sep string, elems []string) string {
return strings.Join(elems, sep)
}
+func split(sep string, s string) []string {
+ sp := strings.Split(s, sep)
+ for i := range sp {
+ sp[i] = strings.TrimSpace(sp[i])
+ }
+ return sp
+}
+
// removes a signature from the piped in message
func trimSignature(message string) string {
var res strings.Builder
@@ -335,6 +343,7 @@ var templateFuncs = template.FuncMap{
"humanReadable": humanReadable,
"cwd": cwd,
"join": join,
+ "split": split,
"trimSignature": trimSignature,
"compactDir": compactDir,
"match": parse.MatchCache,
diff --git a/models/templates.go b/models/templates.go
index 686687cc..0c684e86 100644
--- a/models/templates.go
+++ b/models/templates.go
@@ -29,6 +29,7 @@ type TemplateData interface {
Flags() []string
IsReplied() bool
HasAttachment() bool
+ Attach(string) string
IsFlagged() bool
IsRecent() bool
IsUnread() bool
diff --git a/widgets/compose.go b/widgets/compose.go
index 281a35cc..0aac8705 100644
--- a/widgets/compose.go
+++ b/widgets/compose.go
@@ -104,6 +104,7 @@ func NewComposer(
data.SetAccount(acct.acct)
data.SetFolder(acct.Directories().SelectedDirectory())
data.SetHeaders(h, orig)
+ data.SetComposer(c)
if err := c.addTemplate(template, data.Data(), body); err != nil {
return nil, err
}
@@ -112,7 +113,6 @@ func NewComposer(
} else if err != nil {
return nil, err
}
-
if err := c.setupFor(acct); err != nil {
return nil, err
}