diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-05-06 11:30:48 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-05-16 17:12:00 +0200 |
commit | 30c1a30168dfff8ca5eecb8d0fa42ab4b638f79d (patch) | |
tree | 97d177a8e399ed7ad7f107b85df1cd4d246e50ff /widgets | |
parent | 6b6aaf3ae131971d05ab3f849ea3db14c6a6e055 (diff) | |
download | aerc-30c1a30168dfff8ca5eecb8d0fa42ab4b638f79d.tar.gz |
templates: use template interface consistently
Use the template interface consistently. Before, we have exported the
state.TemplateData struct and used it in most places instead of the
models.TemplateData interface. This lead to some inconsistencies, i.e.
Role() has been defined on the exported struct but not on the interface.
Unexport the state.TemplateData struct, add a DataSetter interface to
set the data needed for the template data and call the Data() method
which returns a models.TemplateData interface when the template data is
needed.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/account.go | 6 | ||||
-rw-r--r-- | widgets/compose.go | 11 | ||||
-rw-r--r-- | widgets/dirlist.go | 6 | ||||
-rw-r--r-- | widgets/dirtree.go | 4 | ||||
-rw-r--r-- | widgets/msglist.go | 10 | ||||
-rw-r--r-- | widgets/status.go | 5 |
6 files changed, 22 insertions, 20 deletions
diff --git a/widgets/account.go b/widgets/account.go index e28804de..d76333d2 100644 --- a/widgets/account.go +++ b/widgets/account.go @@ -605,17 +605,17 @@ func (acct *AccountView) Vsplit(n int) error { // setTitle executes the title template and sets the tab title func (acct *AccountView) setTitle() { - var data state.TemplateData - if acct.tab == nil { return } + + data := state.NewDataSetter() data.SetAccount(acct.acct) data.SetFolder(acct.Directories().SelectedDirectory()) data.SetRUE(acct.dirlist.List(), acct.dirlist.GetRUECount) var buf bytes.Buffer - err := templates.Render(acct.uiConf.TabTitleAccount, &buf, &data) + err := templates.Render(acct.uiConf.TabTitleAccount, &buf, data.Data()) if err != nil { acct.PushError(err) return diff --git a/widgets/compose.go b/widgets/compose.go index 4735782b..ea6a50a4 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -96,11 +96,11 @@ func NewComposer( completer: nil, } - var data state.TemplateData + data := state.NewDataSetter() data.SetAccount(acct.acct) data.SetFolder(acct.Directories().SelectedDirectory()) data.SetHeaders(h, orig) - if err := c.AddTemplate(template, &data); err != nil { + if err := c.AddTemplate(template, data.Data()); err != nil { return nil, err } c.AddSignature() @@ -1588,8 +1588,6 @@ func (c *Composer) setTitle() { return } - var data state.TemplateData - header := c.header.Copy() // Get subject direct from the textinput subject, ok := c.editors["subject"] @@ -1599,12 +1597,15 @@ func (c *Composer) setTitle() { if header.Get("subject") == "" { header.SetSubject("New Email") } + + data := state.NewDataSetter() data.SetAccount(c.acctConfig) data.SetFolder(c.acct.Directories().SelectedDirectory()) data.SetHeaders(&header, c.parent) var buf bytes.Buffer - err := templates.Render(c.acct.UiConfig().TabTitleComposer, &buf, &data) + err := templates.Render(c.acct.UiConfig().TabTitleComposer, &buf, + data.Data()) if err != nil { c.acct.PushError(err) return diff --git a/widgets/dirlist.go b/widgets/dirlist.go index 25d1e7ff..8112ebd2 100644 --- a/widgets/dirlist.go +++ b/widgets/dirlist.go @@ -238,8 +238,8 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) { } listCtx := ctx.Subcontext(0, 0, textWidth, ctx.Height()) - var data state.TemplateData + data := state.NewDataSetter() data.SetAccount(dirlist.acctConf) for i, name := range dirlist.dirs { @@ -254,7 +254,7 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) { data.SetFolder(dirlist.Directory(name)) data.SetRUE([]string{name}, dirlist.GetRUECount) left, right, style := dirlist.renderDir( - name, uiConfig, &data, + name, uiConfig, data.Data(), name == dirlist.selecting, listCtx.Width(), ) listCtx.Printf(0, row, style, "%s %s", left, right) @@ -267,7 +267,7 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) { } func (dirlist *DirectoryList) renderDir( - path string, conf *config.UIConfig, data *state.TemplateData, + path string, conf *config.UIConfig, data models.TemplateData, selected bool, width int, ) (string, string, tcell.Style) { var left, right string diff --git a/widgets/dirtree.go b/widgets/dirtree.go index 98b25850..9a33e6c3 100644 --- a/widgets/dirtree.go +++ b/widgets/dirtree.go @@ -98,8 +98,8 @@ func (dt *DirectoryTree) Draw(ctx *ui.Context) { } treeCtx := ctx.Subcontext(0, 0, textWidth, ctx.Height()) - var data state.TemplateData + data := state.NewDataSetter() data.SetAccount(dt.acctConf) n = 0 @@ -124,7 +124,7 @@ func (dt *DirectoryTree) Draw(ctx *ui.Context) { data.SetRUE([]string{path}, dt.GetRUECount) left, right, style := dt.renderDir( - path, uiConfig, &data, + path, uiConfig, data.Data(), i == dt.listIdx, treeCtx.Width(), ) diff --git a/widgets/msglist.go b/widgets/msglist.go index 1fc33279..df005431 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -91,8 +91,8 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } var needsHeaders []uint32 - var data state.TemplateData + data := state.NewDataSetter() data.SetAccount(acct.acct) data.SetFolder(acct.Directories().SelectedDirectory()) @@ -171,7 +171,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { lastSubject = baseSubject prevThread = thread - if addMessage(store, thread.Uid, &table, &data, uiConfig) { + if addMessage(store, thread.Uid, &table, data, uiConfig) { break threadLoop } } @@ -183,7 +183,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { continue } uid := iter.Value().(uint32) - if addMessage(store, uid, &table, &data, uiConfig) { + if addMessage(store, uid, &table, data, uiConfig) { break } } @@ -216,7 +216,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { func addMessage( store *lib.MessageStore, uid uint32, - table *ui.Table, data *state.TemplateData, + table *ui.Table, data state.DataSetter, uiConfig *config.UIConfig, ) bool { msg := store.Messages[uid] @@ -258,7 +258,7 @@ func addMessage( for c, col := range table.Columns { var buf bytes.Buffer - err := col.Def.Template.Execute(&buf, data) + err := col.Def.Template.Execute(&buf, data.Data()) if err != nil { log.Errorf("<%s> %s", msg.Envelope.MessageId, err) cells[c] = err.Error() diff --git a/widgets/status.go b/widgets/status.go index 9f2544c7..6157dd10 100644 --- a/widgets/status.go +++ b/widgets/status.go @@ -49,7 +49,7 @@ func (status *StatusLine) Draw(ctx *ui.Context) { style := status.uiConfig().GetStyle(config.STYLE_STATUSLINE_ERROR) ctx.Printf(0, 0, style, "%s", msg) case status.aerc != nil && status.acct != nil: - var data state.TemplateData + data := state.NewDataSetter() data.SetPendingKeys(status.aerc.pendingKeys) data.SetState(&status.acct.state) data.SetAccount(status.acct.acct) @@ -66,7 +66,8 @@ func (status *StatusLine) Draw(ctx *ui.Context) { var buf bytes.Buffer cells := make([]string, len(table.Columns)) for c, col := range table.Columns { - err := templates.Render(col.Def.Template, &buf, &data) + err := templates.Render(col.Def.Template, &buf, + data.Data()) if err != nil { log.Errorf("%s", err) cells[c] = err.Error() |