Add custom sorting for folders

A new config options for accounts.conf (folders-sort) was added to
allow a user to choose which folders should be shown on top.
My use case was to avoid stepping into heavy, but rarely viewed folders
when cycling through other often accessed ones.

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.
This commit is contained in:
Michele Finotto 2019-12-03 20:20:21 +01:00 committed by Drew DeVault
parent 2559ebfac5
commit dfe58842b9
3 changed files with 41 additions and 2 deletions

View file

@ -61,6 +61,7 @@ type AccountConfig struct {
OutgoingCredCmd string OutgoingCredCmd string
SignatureFile string SignatureFile string
SignatureCmd string SignatureCmd string
FoldersSort []string `ini:"folders-sort" delim:","`
} }
type BindingConfig struct { type BindingConfig struct {

View file

@ -301,6 +301,12 @@ Note that many of these configuration options are written for you, such as
Default: all folders Default: all folders
*folders-sort*
Specifies a comma separated list of folders to be shown at the top of the
list in the provided order. Remaining folders will be sorted alphabetically.
Default: none
*from* *from*
The default value to use for the From header in new emails. This should be The default value to use for the From header in new emails. This should be
an RFC 5322-compatible string, such as "Your Name <you@example.org>". an RFC 5322-compatible string, such as "Your Name <you@example.org>".

View file

@ -5,6 +5,7 @@ import (
"log" "log"
"regexp" "regexp"
"sort" "sort"
"strings"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
@ -61,9 +62,10 @@ func (dirlist *DirectoryList) UpdateList(done func(dirs []string)) {
case *types.Directory: case *types.Directory:
dirs = append(dirs, msg.Dir.Name) dirs = append(dirs, msg.Dir.Name)
case *types.Done: case *types.Done:
sort.Strings(dirs)
dirlist.store.Update(dirs) dirlist.store.Update(dirs)
dirlist.filterDirsByFoldersConfig() dirlist.filterDirsByFoldersConfig()
dirlist.sortDirsByFoldersSortConfig()
dirlist.store.Update(dirlist.dirs)
dirlist.spinner.Stop() dirlist.spinner.Stop()
dirlist.Invalidate() dirlist.Invalidate()
if done != nil { if done != nil {
@ -94,6 +96,7 @@ func (dirlist *DirectoryList) Select(name string) {
dirlist.dirs = append(dirlist.dirs, dirlist.selected) dirlist.dirs = append(dirlist.dirs, dirlist.selected)
} }
sort.Strings(dirlist.dirs) sort.Strings(dirlist.dirs)
dirlist.sortDirsByFoldersSortConfig()
} }
dirlist.Invalidate() dirlist.Invalidate()
}) })
@ -261,7 +264,7 @@ func (dirlist *DirectoryList) Clicked(x int, y int) (string, bool) {
} }
func (dirlist *DirectoryList) NextPrev(delta int) { func (dirlist *DirectoryList) NextPrev(delta int) {
curIdx := sort.SearchStrings(dirlist.dirs, dirlist.selected) curIdx := findString(dirlist.dirs, dirlist.selected)
if curIdx == len(dirlist.dirs) { if curIdx == len(dirlist.dirs) {
return return
} }
@ -297,6 +300,26 @@ func folderMatches(folder string, pattern string) bool {
return pattern == folder 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() {
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 // filterDirsByFoldersConfig sets dirlist.dirs to the filtered subset of the
// dirstore, based on the AccountConfig.Folders option // dirstore, based on the AccountConfig.Folders option
func (dirlist *DirectoryList) filterDirsByFoldersConfig() { func (dirlist *DirectoryList) filterDirsByFoldersConfig() {
@ -331,3 +354,12 @@ func (dirlist *DirectoryList) SetMsgStore(name string, msgStore *lib.MessageStor
dirlist.Invalidate() dirlist.Invalidate()
}) })
} }
func findString(slice []string, str string) int {
for i, s := range slice {
if str == s {
return i
}
}
return -1
}