~sircmpwn/aerc

[WIP] Add custom sorting for folders v1 PROPOSED

Michele Finotto: 1
 [WIP] Add custom sorting for folders

 2 files changed, 36 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/9132/mbox | git am -3
Learn more about email & git

[PATCH] [WIP] Add custom sorting for folders Export this patch

This is a WIP. Everything seems to be working fine, except the dirlist
doesn't reflect the custom sort.
I'm probably (for sure) missing something, but I don't understand what.

Any help would be appreciated.

To test add this to your account.conf:

folders-sort  = INBOX|Sent|Archive

INBOX, Sent and Archive should then show at the top of your dirlist,
and all other folders should come next in alphabetical order.
---
 config/config.go   |  1 +
 widgets/dirlist.go | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/config/config.go b/config/config.go
index 32d07fc..004b862 100644
--- a/config/config.go
+++ b/config/config.go
@@ -61,6 +61,7 @@ type AccountConfig struct {
 	OutgoingCredCmd string
 	SignatureFile   string
 	SignatureCmd    string
+	FoldersSort     []string `ini:"folders-sort" delim:"|"`
 }
 
 type BindingConfig struct {
diff --git a/widgets/dirlist.go b/widgets/dirlist.go
index ef2dd1e..6ad4812 100644
--- a/widgets/dirlist.go
+++ b/widgets/dirlist.go
@@ -5,6 +5,7 @@ import (
 	"log"
 	"regexp"
 	"sort"
+	"strings"
 
 	"github.com/gdamore/tcell"
 	"github.com/mattn/go-runewidth"
@@ -64,6 +65,8 @@ func (dirlist *DirectoryList) UpdateList(done func(dirs []string)) {
 				sort.Strings(dirs)
 				dirlist.store.Update(dirs)
 				dirlist.filterDirsByFoldersConfig()
+				dirlist.sortDirsByFoldersSortConfig()
+				dirlist.store.Update(dirlist.dirs)
 				dirlist.spinner.Stop()
 				dirlist.Invalidate()
 				if done != nil {
@@ -297,6 +300,29 @@ func folderMatches(folder string, pattern string) bool {
 	return pattern == folder
 }
 
+// sortDirsByFoldersSortConfig sets dirlist.dirs to be sorted based on the
+// AccountConfig.FoldersSort option. Folders not included in the option
+// will be appended at the end in alphabetical order
+func (dirlist *DirectoryList) sortDirsByFoldersSortConfig() {
+	if len(dirlist.acctConf.FoldersSort) == 0 {
+		return
+	}
+	sort.Slice(dirlist.dirs, func(i, j int) bool {
+		iInFoldersSort := findString(dirlist.acctConf.FoldersSort, dirlist.dirs[i])
+		jInFoldersSort := findString(dirlist.acctConf.FoldersSort, dirlist.dirs[j])
+		if iInFoldersSort >= 0 && jInFoldersSort >= 0 {
+			return iInFoldersSort < jInFoldersSort
+		}
+		if iInFoldersSort >= 0 {
+			return true
+		}
+		if jInFoldersSort >= 0 {
+			return false
+		}
+		return strings.Compare(dirlist.dirs[i], dirlist.dirs[j]) == -1
+	})
+}
+
 // filterDirsByFoldersConfig sets dirlist.dirs to the filtered subset of the
 // dirstore, based on the AccountConfig.Folders option
 func (dirlist *DirectoryList) filterDirsByFoldersConfig() {
@@ -331,3 +357,12 @@ func (dirlist *DirectoryList) SetMsgStore(name string, msgStore *lib.MessageStor
 		dirlist.Invalidate()
 	})
 }
+
+func findString(slice []string, str string) int {
+	for i, s := range slice {
+		if str == s {
+			return i
+		}
+	}
+	return -1
+}
-- 
2.24.0
View this thread in the archives