aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-12-20 17:05:43 +0100
committerRobin Jarry <robin@jarry.cc>2023-01-04 22:57:31 +0100
commitae675b491d2b55a06588e8ab4ce8205aaae796c8 (patch)
tree383920e34344ea3456162fb74cc180bfb33dec83
parent2a290cf1ed6113f0d01eea3f2cff446339bed0ce (diff)
downloadaerc-ae675b491d2b55a06588e8ab4ce8205aaae796c8.tar.gz
templates: change fields as lazy functions
No need to pre-render fields that are not necessarily accessed in templates. Change fields to functions that are evaluated only when required. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r--commands/account/compose.go3
-rw-r--r--commands/account/recover.go3
-rw-r--r--commands/msg/forward.go2
-rw-r--r--commands/msg/invite.go2
-rw-r--r--commands/msg/recall.go2
-rw-r--r--commands/msg/reply.go3
-rw-r--r--commands/msg/unsubscribe.go3
-rw-r--r--lib/templates/data.go102
-rw-r--r--widgets/aerc.go3
-rw-r--r--widgets/compose.go6
10 files changed, 79 insertions, 50 deletions
diff --git a/commands/account/compose.go b/commands/account/compose.go
index fb37b231..5695bb7d 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -13,7 +13,6 @@ import (
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/log"
- "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/widgets"
"git.sr.ht/~sircmpwn/getopt"
)
@@ -55,7 +54,7 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
composer, err := widgets.NewComposer(aerc, acct,
acct.AccountConfig(), acct.Worker(),
- template, &headers, models.OriginalMail{})
+ template, &headers, nil)
if err != nil {
return err
}
diff --git a/commands/account/recover.go b/commands/account/recover.go
index 8ce878ee..cef0720f 100644
--- a/commands/account/recover.go
+++ b/commands/account/recover.go
@@ -10,7 +10,6 @@ import (
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/log"
- "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/widgets"
"git.sr.ht/~sircmpwn/getopt"
)
@@ -103,7 +102,7 @@ func (Recover) Execute(aerc *widgets.Aerc, args []string) error {
composer, err := widgets.NewComposer(aerc, acct,
acct.AccountConfig(), acct.Worker(),
- "", nil, models.OriginalMail{})
+ "", nil, nil)
if err != nil {
return err
}
diff --git a/commands/msg/forward.go b/commands/msg/forward.go
index 9a4f3d41..90c32ec2 100644
--- a/commands/msg/forward.go
+++ b/commands/msg/forward.go
@@ -101,7 +101,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
addTab := func() (*widgets.Composer, error) {
composer, err := widgets.NewComposer(aerc, acct,
- acct.AccountConfig(), acct.Worker(), template, h, original)
+ acct.AccountConfig(), acct.Worker(), template, h, &original)
if err != nil {
aerc.PushError("Error: " + err.Error())
return nil, err
diff --git a/commands/msg/invite.go b/commands/msg/invite.go
index ef28b91c..1528abea 100644
--- a/commands/msg/invite.go
+++ b/commands/msg/invite.go
@@ -139,7 +139,7 @@ func (invite) Execute(aerc *widgets.Aerc, args []string) error {
addTab := func(cr *calendar.Reply) error {
composer, err := widgets.NewComposer(aerc, acct,
- acct.AccountConfig(), acct.Worker(), "", h, original)
+ acct.AccountConfig(), acct.Worker(), "", h, &original)
if err != nil {
aerc.PushError("Error: " + err.Error())
return err
diff --git a/commands/msg/recall.go b/commands/msg/recall.go
index f106e8bd..2ef468e0 100644
--- a/commands/msg/recall.go
+++ b/commands/msg/recall.go
@@ -74,7 +74,7 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error {
composer, err := widgets.NewComposer(aerc, acct,
acct.AccountConfig(), acct.Worker(), "", msgInfo.RFC822Headers,
- models.OriginalMail{})
+ nil)
if err != nil {
return errors.Wrap(err, "Cannot open a new composer")
}
diff --git a/commands/msg/reply.go b/commands/msg/reply.go
index 795158ca..f8cde644 100644
--- a/commands/msg/reply.go
+++ b/commands/msg/reply.go
@@ -175,7 +175,8 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
mv, _ := aerc.SelectedTabContent().(*widgets.MessageViewer)
addTab := func() error {
composer, err := widgets.NewComposer(aerc, acct,
- acct.AccountConfig(), acct.Worker(), template, h, original)
+ acct.AccountConfig(), acct.Worker(), template, h,
+ &original)
if err != nil {
aerc.PushError("Error: " + err.Error())
return err
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index a334e827..66797660 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -11,7 +11,6 @@ import (
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/log"
- "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/widgets"
"github.com/emersion/go-message/mail"
)
@@ -155,7 +154,7 @@ func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
acct.Worker(),
"",
h,
- models.OriginalMail{},
+ nil,
)
if err != nil {
return err
diff --git a/lib/templates/data.go b/lib/templates/data.go
index 5b351816..8637aa7f 100644
--- a/lib/templates/data.go
+++ b/lib/templates/data.go
@@ -8,51 +8,83 @@ import (
)
type TemplateData struct {
- To []*mail.Address
- Cc []*mail.Address
- Bcc []*mail.Address
- From []*mail.Address
- Date time.Time
- Subject string
+ msg *mail.Header
// Only available when replying with a quote
- OriginalText string
- OriginalFrom []*mail.Address
- OriginalDate time.Time
- OriginalMIMEType string
+ parent *models.OriginalMail
}
-func ParseTemplateData(h *mail.Header, original models.OriginalMail) TemplateData {
- // we ignore errors as this shouldn't fail the sending / replying even if
- // something is wrong with the message we reply to
- to, _ := h.AddressList("to")
- cc, _ := h.AddressList("cc")
- bcc, _ := h.AddressList("bcc")
- from, _ := h.AddressList("from")
- subject, err := h.Text("subject")
+func NewTemplateData(
+ msg *mail.Header, parent *models.OriginalMail,
+) *TemplateData {
+ return &TemplateData{
+ msg: msg,
+ parent: parent,
+ }
+}
+
+func (d *TemplateData) To() []*mail.Address {
+ to, _ := d.msg.AddressList("to")
+ return to
+}
+
+func (d *TemplateData) Cc() []*mail.Address {
+ to, _ := d.msg.AddressList("cc")
+ return to
+}
+
+func (d *TemplateData) Bcc() []*mail.Address {
+ to, _ := d.msg.AddressList("bcc")
+ return to
+}
+
+func (d *TemplateData) From() []*mail.Address {
+ to, _ := d.msg.AddressList("from")
+ return to
+}
+
+func (d *TemplateData) Date() time.Time {
+ return time.Now()
+}
+
+func (d *TemplateData) Subject() string {
+ subject, err := d.msg.Text("subject")
if err != nil {
- subject = h.Get("subject")
+ subject = d.msg.Get("subject")
+ }
+ return subject
+}
+
+func (d *TemplateData) OriginalText() string {
+ if d.parent == nil {
+ return ""
}
+ return d.parent.Text
+}
- td := TemplateData{
- To: to,
- Cc: cc,
- Bcc: bcc,
- From: from,
- Date: time.Now(),
- Subject: subject,
- OriginalText: original.Text,
- OriginalDate: original.Date,
- OriginalMIMEType: original.MIMEType,
+func (d *TemplateData) OriginalDate() time.Time {
+ if d.parent == nil {
+ return time.Time{}
}
- if original.RFC822Headers != nil {
- origFrom, _ := original.RFC822Headers.AddressList("from")
- td.OriginalFrom = origFrom
+ return d.parent.Date
+}
+
+func (d *TemplateData) OriginalFrom() []*mail.Address {
+ if d.parent == nil || d.parent.RFC822Headers == nil {
+ return nil
+ }
+ from, _ := d.parent.RFC822Headers.AddressList("from")
+ return from
+}
+
+func (d *TemplateData) OriginalMIMEType() string {
+ if d.parent == nil {
+ return ""
}
- return td
+ return d.parent.MIMEType
}
// DummyData provides dummy data to test template validity
-func DummyData() interface{} {
+func DummyData() *TemplateData {
from := &mail.Address{
Name: "John Doe",
Address: "john@example.com",
@@ -76,5 +108,5 @@ func DummyData() interface{} {
MIMEType: "text/plain",
RFC822Headers: oh,
}
- return ParseTemplateData(h, original)
+ return NewTemplateData(h, &original)
}
diff --git a/widgets/aerc.go b/widgets/aerc.go
index f6e5c1ff..eb904ea0 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -20,7 +20,6 @@ import (
"git.sr.ht/~rjarry/aerc/lib/crypto"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/log"
- "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
@@ -710,7 +709,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
}
composer, err := NewComposer(aerc, acct,
- acct.AccountConfig(), acct.Worker(), "", h, models.OriginalMail{})
+ acct.AccountConfig(), acct.Worker(), "", h, nil)
if err != nil {
return nil
}
diff --git a/widgets/compose.go b/widgets/compose.go
index 782e7538..c59fa8ef 100644
--- a/widgets/compose.go
+++ b/widgets/compose.go
@@ -33,7 +33,7 @@ type Composer struct {
sync.Mutex
editors map[string]*headerEditor // indexes in lower case (from / cc / bcc)
header *mail.Header
- parent models.OriginalMail // parent of current message, only set if reply
+ parent *models.OriginalMail // parent of current message, only set if reply
acctConfig *config.AccountConfig
acct *AccountView
@@ -68,7 +68,7 @@ type Composer struct {
func NewComposer(
aerc *Aerc, acct *AccountView, acctConfig *config.AccountConfig,
worker *types.Worker, template string,
- h *mail.Header, orig models.OriginalMail,
+ h *mail.Header, orig *models.OriginalMail,
) (*Composer, error) {
if h == nil {
h = new(mail.Header)
@@ -93,7 +93,7 @@ func NewComposer(
completer: nil,
}
- templateData := templates.ParseTemplateData(h, orig)
+ templateData := templates.NewTemplateData(h, orig)
if err := c.AddTemplate(template, templateData); err != nil {
return nil, err
}