Skip to content

Commit

Permalink
threads: add .ThreadUnread to template data
Browse files Browse the repository at this point in the history
When a thread is folded, it can be useful to know how many unseen
messages lie below the root. For example, one might want to show that
count in the message list:

column-folded = {{if .ThreadFolded \
	}}{{if ne .ThreadUnread 0 \
		}}{{.ThreadUnread | printf "%s/"}}{{ \
	end}}{{ .ThreadCount | printf "%s"}}{{end}}

Add `.ThreadUnread` to the template functions.

Changelog-added: `.ThreadUnread` is now available in templates.
Signed-off-by: inwit <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
agenbite authored and rjarry committed Nov 12, 2023
1 parent 0631873 commit c13df79
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
19 changes: 17 additions & 2 deletions app/msglist.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ func countThreads(thread *types.Thread) (ctr int) {
return
}

func unreadInThread(thread *types.Thread, store *lib.MessageStore) (ctr int) {
if thread == nil {
return
}
_ = thread.Walk(func(t *types.Thread, _ int, _ error) error {
msg := store.Messages[t.Uid]
if msg != nil && !msg.Flags.Has(models.SeenFlag) {
ctr++
}
return nil
})
return
}

func threadPrefix(t *types.Thread, reverse bool, point bool) string {
var arrow string
if t.Parent != nil {
Expand Down Expand Up @@ -476,7 +490,7 @@ func newThreadView(store *lib.MessageStore) *threadView {
}

func (t *threadView) Update(data state.DataSetter, uid uint32) {
prefix, same, count, folded, context := "", false, 0, false, false
prefix, same, count, unread, folded, context := "", false, 0, 0, false, false
thread, err := t.store.Thread(uid)
if thread != nil && err == nil {
prefix = threadPrefix(thread, t.reverse, true)
Expand All @@ -485,8 +499,9 @@ func (t *threadView) Update(data state.DataSetter, uid uint32) {
t.prev = thread
t.prevSubj = subject
count = countThreads(thread)
unread = unreadInThread(thread, t.store)
folded = thread.FirstChild != nil && thread.FirstChild.Hidden != 0
context = thread.Context
}
data.SetThreading(prefix, same, count, folded, context)
data.SetThreading(prefix, same, count, unread, folded, context)
}
1 change: 1 addition & 0 deletions config/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (d *dummyData) DateAutoFormat(time.Time) string { return "" }
func (d *dummyData) Header(string) string { return "" }
func (d *dummyData) ThreadPrefix() string { return "└─>" }
func (d *dummyData) ThreadCount() int { return 0 }
func (d *dummyData) ThreadUnread() int { return 0 }
func (d *dummyData) ThreadFolded() bool { return false }
func (d *dummyData) ThreadContext() bool { return true }
func (d *dummyData) Subject() string { return "Re: [PATCH] hey" }
Expand Down
5 changes: 4 additions & 1 deletion doc/aerc-templates.7.scd
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ available always.
hidden by *:fold*.

_ThreadCount_
The number of thread children.
The number of messages in the thread.

_ThreadUnread_
The number of unread messages in the thread.

*Flags*
List of message flags, not available when composing, replying nor
Expand Down
10 changes: 8 additions & 2 deletions lib/state/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DataSetter interface {
Data() models.TemplateData
SetHeaders(*mail.Header, *models.OriginalMail)
SetInfo(*models.MessageInfo, int, bool)
SetThreading(string, bool, int, bool, bool)
SetThreading(string, bool, int, int, bool, bool)
SetComposer(Composer)
SetAccount(*config.AccountConfig)
SetFolder(*models.Directory)
Expand All @@ -34,6 +34,7 @@ type ThreadInfo struct {
SameSubject bool
Prefix string
Count int
Unread int
Folded bool
Context bool
}
Expand Down Expand Up @@ -88,11 +89,12 @@ func (d *templateData) SetInfo(info *models.MessageInfo, num int, marked bool,
}

func (d *templateData) SetThreading(prefix string, same bool, count int,
folded bool, context bool,
unread int, folded bool, context bool,
) {
d.threadInfo.Prefix = prefix
d.threadInfo.SameSubject = same
d.threadInfo.Count = count
d.threadInfo.Unread = unread
d.threadInfo.Folded = folded
d.threadInfo.Context = context
}
Expand Down Expand Up @@ -301,6 +303,10 @@ func (d *templateData) ThreadCount() int {
return d.threadInfo.Count
}

func (d *templateData) ThreadUnread() int {
return d.threadInfo.Unread
}

func (d *templateData) ThreadFolded() bool {
return d.threadInfo.Folded
}
Expand Down
1 change: 1 addition & 0 deletions models/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type TemplateData interface {
Header(name string) string
ThreadPrefix() string
ThreadCount() int
ThreadUnread() int
ThreadFolded() bool
ThreadContext() bool
Subject() string
Expand Down

0 comments on commit c13df79

Please sign in to comment.