From 90d26da58a4af2d34328f5916adf3781222966c6 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Thu, 19 Sep 2019 23:37:44 +0100 Subject: Add sorting functionality There is a command and config option. The criteria are a list of the sort criterion and each can be individually reversed. This only includes support for sorting in the maildir backend currently. The other backends are not supported in this patch. --- worker/maildir/worker.go | 53 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) (limited to 'worker/maildir') diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 597e0d2..1df4e09 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -12,6 +12,7 @@ import ( "git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/worker/handlers" + "git.sr.ht/~sircmpwn/aerc/worker/lib" "git.sr.ht/~sircmpwn/aerc/worker/types" ) @@ -23,11 +24,12 @@ var errUnsupported = fmt.Errorf("unsupported command") // A Worker handles interfacing between aerc's UI and a group of maildirs. type Worker struct { - c *Container - selected *maildir.Dir - selectedName string - worker *types.Worker - watcher *fsnotify.Watcher + c *Container + selected *maildir.Dir + selectedName string + worker *types.Worker + watcher *fsnotify.Watcher + currentSortCriteria []*types.SortCriterion } // NewWorker creates a new maildir worker with the provided worker. @@ -86,8 +88,13 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) { w.worker.Logger.Printf("could not scan UIDs: %v", err) return } + sortedUids, err := w.sort(uids, w.currentSortCriteria) + if err != nil { + w.worker.Logger.Printf("error sorting directory: %v", err) + return + } w.worker.PostMessage(&types.DirectoryContents{ - Uids: uids, + Uids: sortedUids, }, nil) dirInfo := w.getDirectoryInfo() dirInfo.Recent = len(newUnseen) @@ -271,13 +278,45 @@ func (w *Worker) handleFetchDirectoryContents( w.worker.Logger.Printf("error scanning uids: %v", err) return err } + sortedUids, err := w.sort(uids, msg.SortCriteria) + if err != nil { + w.worker.Logger.Printf("error sorting directory: %v", err) + return err + } + w.currentSortCriteria = msg.SortCriteria w.worker.PostMessage(&types.DirectoryContents{ Message: types.RespondTo(msg), - Uids: uids, + Uids: sortedUids, }, nil) return nil } +func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, error) { + if len(criteria) == 0 { + return uids, nil + } + var msgInfos []*models.MessageInfo + for _, uid := range uids { + m, err := w.c.Message(*w.selected, uid) + if err != nil { + w.worker.Logger.Printf("could not get message: %v", err) + continue + } + info, err := m.MessageInfo() + if err != nil { + w.worker.Logger.Printf("could not get message info: %v", err) + continue + } + msgInfos = append(msgInfos, info) + } + sortedUids, err := lib.Sort(msgInfos, criteria) + if err != nil { + w.worker.Logger.Printf("could not sort the messages: %v", err) + return nil, err + } + return sortedUids, nil +} + func (w *Worker) handleCreateDirectory(msg *types.CreateDirectory) error { dir := w.c.Dir(msg.Directory) if err := dir.Create(); err != nil { -- cgit v1.2.3