diff options
-rw-r--r-- | lib/dirstore.go | 11 | ||||
-rw-r--r-- | lib/dirstore_test.go | 23 | ||||
-rw-r--r-- | lib/sort/sort.go | 12 |
3 files changed, 45 insertions, 1 deletions
diff --git a/lib/dirstore.go b/lib/dirstore.go index bbf4e20b..8749fb09 100644 --- a/lib/dirstore.go +++ b/lib/dirstore.go @@ -1,10 +1,14 @@ package lib -import "git.sr.ht/~rjarry/aerc/models" +import ( + "git.sr.ht/~rjarry/aerc/lib/sort" + "git.sr.ht/~rjarry/aerc/models" +) type DirStore struct { dirs map[string]*models.Directory msgStores map[string]*MessageStore + order []string } func NewDirStore() *DirStore { @@ -19,6 +23,7 @@ func (store *DirStore) List() []string { for dir := range store.msgStores { dirs = append(dirs, dir) } + sort.SortStringBy(dirs, store.order) return dirs } @@ -28,6 +33,10 @@ func (store *DirStore) MessageStore(dirname string) (*MessageStore, bool) { } func (store *DirStore) SetMessageStore(dir *models.Directory, msgStore *MessageStore) { + s := dir.Name + if _, ok := store.dirs[s]; !ok { + store.order = append(store.order, s) + } store.dirs[dir.Name] = dir store.msgStores[dir.Name] = msgStore } diff --git a/lib/dirstore_test.go b/lib/dirstore_test.go new file mode 100644 index 00000000..b1ba4eb9 --- /dev/null +++ b/lib/dirstore_test.go @@ -0,0 +1,23 @@ +package lib_test + +import ( + "reflect" + "testing" + + "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/models" +) + +func TestDirStore_List(t *testing.T) { + dirs := []string{"a/c", "x", "a/b", "d"} + dirstore := lib.NewDirStore() + for _, d := range dirs { + dirstore.SetMessageStore(&models.Directory{Name: d}, nil) + } + for i := 0; i < 10; i++ { + if !reflect.DeepEqual(dirstore.List(), dirs) { + t.Errorf("order does not match") + return + } + } +} diff --git a/lib/sort/sort.go b/lib/sort/sort.go index c7e2b8ce..287f986c 100644 --- a/lib/sort/sort.go +++ b/lib/sort/sort.go @@ -70,3 +70,15 @@ func SortBy(toSort []uint32, sortBy []uint32) { return uidMap[toSort[i]] < uidMap[toSort[j]] }) } + +// SortStringBy sorts the string slice s according to the order given in the +// order string slice. +func SortStringBy(s []string, order []string) { + m := make(map[string]int) + for i, d := range order { + m[d] = i + } + sort.Slice(s, func(i, j int) bool { + return m[s[i]] < m[s[j]] + }) +} |