summary refs log tree commit diff stats
path: root/widgets
diff options
context:
space:
mode:
authorMichele Finotto <m@finotto.org>2019-12-03 20:20:21 +0100
committerDrew DeVault <sir@cmpwn.com>2019-12-09 12:42:40 -0500
commitdfe58842b967e2951604b631b89dfd61117745ec (patch)
treea88fc85be9611de781ee4a1aa411c9254d2d167d /widgets
parent2559ebfac56a8060c8af001ba61709f3c4d78d04 (diff)
downloadaerc-dfe58842b967e2951604b631b89dfd61117745ec.tar.gz
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.
Diffstat (limited to 'widgets')
-rw-r--r--widgets/dirlist.go36
1 files changed, 34 insertions, 2 deletions
diff --git a/widgets/dirlist.go b/widgets/dirlist.go
index ef2dd1e..c0c8917 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"
@@ -61,9 +62,10 @@ func (dirlist *DirectoryList) UpdateList(done func(dirs []string)) {
 			case *types.Directory:
 				dirs = append(dirs, msg.Dir.Name)
 			case *types.Done:
-				sort.Strings(dirs)
 				dirlist.store.Update(dirs)
 				dirlist.filterDirsByFoldersConfig()
+				dirlist.sortDirsByFoldersSortConfig()
+				dirlist.store.Update(dirlist.dirs)
 				dirlist.spinner.Stop()
 				dirlist.Invalidate()
 				if done != nil {
@@ -94,6 +96,7 @@ func (dirlist *DirectoryList) Select(name string) {
 					dirlist.dirs = append(dirlist.dirs, dirlist.selected)
 				}
 				sort.Strings(dirlist.dirs)
+				dirlist.sortDirsByFoldersSortConfig()
 			}
 			dirlist.Invalidate()
 		})
@@ -261,7 +264,7 @@ func (dirlist *DirectoryList) Clicked(x int, y int) (string, bool) {
 }
 
 func (dirlist *DirectoryList) NextPrev(delta int) {
-	curIdx := sort.SearchStrings(dirlist.dirs, dirlist.selected)
+	curIdx := findString(dirlist.dirs, dirlist.selected)
 	if curIdx == len(dirlist.dirs) {
 		return
 	}
@@ -297,6 +300,26 @@ 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() {
+	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 +354,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
+}
the previous revision' href='/acidbong/suckless/dwm/blame/dwm.1?h=2.7&id=2eebebf26243a4a80d646c85db2b9c9ebd168743'>^
c39df91 ^

901b3ed ^
1076f2b

901b3ed ^
0c3544d ^
2b35fae ^

a3e3f0b ^

2b35fae ^
3e06ede ^


292ccc4 ^
d4b7a9a ^
3e06ede ^


2b35fae ^
72655f0 ^
df74b26 ^


72655f0 ^
df74b26 ^
72655f0 ^
3e06ede ^
72655f0 ^
df74b26 ^
3af6434 ^
3e06ede ^
3af6434 ^
df74b26 ^








72655f0 ^

3e06ede ^
72655f0 ^
1549faf ^

3af6434 ^

3e06ede ^
1549faf ^
df74b26 ^





45aea23 ^



df74b26 ^






1549faf ^
df74b26 ^


1549faf ^
ba59bc8 ^
3e06ede ^


4bb89e2 ^
df74b26 ^


2b35fae ^
0c3544d ^
0e5c819 ^
4bd0d33 ^
0e5c819 ^
df74b26 ^


0e5c819 ^
4bd0d33 ^




df74b26 ^


4bd0d33 ^
0e5c819 ^
4bd0d33 ^
0e5c819 ^
df74b26 ^


0c3544d ^

dc5c070 ^

4bb89e2 ^

3e06ede ^
4bb89e2 ^




3e06ede ^
5ef6ef1 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149