Support regex filters for folders

It's nice to be able to filter the folders displayed in the side
bar. Basic string matching can get verbose with enough folders
whitelisted.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
This commit is contained in:
Daniel Xu 2019-08-16 00:11:37 -07:00 committed by Drew DeVault
parent b47143ccc7
commit 9fd6054ca1
2 changed files with 13 additions and 2 deletions

View File

@ -225,7 +225,8 @@ Note that many of these configuration options are written for you, such as
Default: INBOX Default: INBOX
*folders* *folders*
Specifies the list of folders to display in the sidebar. Specifies the comma separated list of folders to display in the sidebar.
Supports regex patterns.
Default: all folders Default: all folders

View File

@ -2,6 +2,7 @@ package widgets
import ( import (
"log" "log"
"regexp"
"sort" "sort"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -159,6 +160,15 @@ func (dirlist *DirectoryList) Prev() {
dirlist.NextPrev(-1) dirlist.NextPrev(-1)
} }
func folderMatches(folder string, pattern string) bool {
r, err := regexp.Compile(pattern)
if err != nil {
return false
}
return r.Match([]byte(folder))
}
// 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() {
@ -170,7 +180,7 @@ func (dirlist *DirectoryList) filterDirsByFoldersConfig() {
var filtered []string var filtered []string
for _, folder := range dirlist.dirs { for _, folder := range dirlist.dirs {
for _, cfgfolder := range dirlist.acctConf.Folders { for _, cfgfolder := range dirlist.acctConf.Folders {
if folder == cfgfolder { if folderMatches(folder, cfgfolder) {
filtered = append(filtered, folder) filtered = append(filtered, folder)
break break
} }