about summary refs log tree commit diff stats
path: root/widgets/dirlist.go
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-09-11 17:37:21 +0100
committerDrew DeVault <sir@cmpwn.com>2019-09-11 12:37:43 -0400
commit618a500341d54ec5bec6d035a86b1307ff1dad0a (patch)
tree090fdff220eeb29e4bee7347a16d9e5ff7fbd0b9 /widgets/dirlist.go
parent572d9ff72883264fbffe35e998b9a6193f20137a (diff)
downloadaerc-618a500341d54ec5bec6d035a86b1307ff1dad0a.tar.gz
Add display of unread messages in dirlist
Add an onUpdateDirs handler. This is used to invalidate the dirlist and
redraw with the correct number of recent/unread/total messages is shown.

A config option and formatting options are provided.
Diffstat (limited to 'widgets/dirlist.go')
-rw-r--r--widgets/dirlist.go99
1 files changed, 98 insertions, 1 deletions
diff --git a/widgets/dirlist.go b/widgets/dirlist.go
index ec73082..ef2dd1e 100644
--- a/widgets/dirlist.go
+++ b/widgets/dirlist.go
@@ -1,15 +1,18 @@
 package widgets
 
 import (
+	"fmt"
 	"log"
 	"regexp"
 	"sort"
 
 	"github.com/gdamore/tcell"
+	"github.com/mattn/go-runewidth"
 
 	"git.sr.ht/~sircmpwn/aerc/config"
 	"git.sr.ht/~sircmpwn/aerc/lib"
 	"git.sr.ht/~sircmpwn/aerc/lib/ui"
+	"git.sr.ht/~sircmpwn/aerc/models"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
 
@@ -105,6 +108,92 @@ func (dirlist *DirectoryList) Invalidate() {
 	dirlist.DoInvalidate(dirlist)
 }
 
+func (dirlist *DirectoryList) getDirString(name string, width int, recentUnseen func() string) string {
+	percent := false
+	rightJustify := false
+	formatted := ""
+	doRightJustify := func(s string) {
+		formatted = runewidth.FillRight(formatted, width-len(s))
+		formatted = runewidth.Truncate(formatted, width-len(s), "…")
+	}
+	for _, char := range dirlist.uiConf.DirListFormat {
+		switch char {
+		case '%':
+			if percent {
+				formatted += string(char)
+				percent = false
+			} else {
+				percent = true
+			}
+		case '>':
+			if percent {
+				rightJustify = true
+			}
+		case 'n':
+			if percent {
+				if rightJustify {
+					doRightJustify(name)
+					rightJustify = false
+				}
+				formatted += name
+				percent = false
+			}
+		case 'r':
+			if percent {
+				rString := recentUnseen()
+				if rightJustify {
+					doRightJustify(rString)
+					rightJustify = false
+				}
+				formatted += rString
+				percent = false
+			}
+		default:
+			formatted += string(char)
+		}
+	}
+	return formatted
+}
+
+func (dirlist *DirectoryList) getRUEString(name string) string {
+	totalUnseen := 0
+	totalRecent := 0
+	totalExists := 0
+	if msgStore, ok := dirlist.MsgStore(name); ok {
+		for _, msg := range msgStore.Messages {
+			if msg == nil {
+				continue
+			}
+			seen := false
+			recent := false
+			for _, flag := range msg.Flags {
+				if flag == models.SeenFlag {
+					seen = true
+				} else if flag == models.RecentFlag {
+					recent = true
+				}
+			}
+			if !seen {
+				if recent {
+					totalRecent++
+				} else {
+					totalUnseen++
+				}
+			}
+		}
+		totalExists = msgStore.DirInfo.Exists
+	}
+	rueString := ""
+	if totalRecent > 0 {
+		rueString = fmt.Sprintf("%d/%d/%d", totalRecent, totalUnseen, totalExists)
+	} else if totalUnseen > 0 {
+		rueString = fmt.Sprintf("%d/%d", totalUnseen, totalExists)
+	} else if totalExists > 0 {
+		rueString = fmt.Sprintf("%d", totalExists)
+	}
+	return rueString
+}
+
 func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
 	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
 
@@ -132,7 +221,12 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
 			style = style.Foreground(tcell.ColorGray)
 		}
 		ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
-		ctx.Printf(0, row, style, "%s", name)
+
+		dirString := dirlist.getDirString(name, ctx.Width(), func() string {
+			return dirlist.getRUEString(name)
+		})
+
+		ctx.Printf(0, row, style, dirString)
 		row++
 	}
 }
@@ -233,4 +327,7 @@ func (dirlist *DirectoryList) MsgStore(name string) (*lib.MessageStore, bool) {
 
 func (dirlist *DirectoryList) SetMsgStore(name string, msgStore *lib.MessageStore) {
 	dirlist.store.SetMessageStore(name, msgStore)
+	msgStore.OnUpdateDirs(func() {
+		dirlist.Invalidate()
+	})
 }