~sircmpwn/aerc

Preserve sorting order in search results v1 PROPOSED

Thanks!

To git.sr.ht:~sircmpwn/aerc
   00b9ca5..d3379dd  master -> master
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/%3C20191002161615.138920-1-dev%40jeffas.io%3E/mbox | git am -3
Learn more about email & git

[PATCH] Preserve sorting order in search results Export this patch

This ensures that the search results follow the order of the current
sort so that cycling throught the results proceeds in displayed order.
---
 lib/msgstore.go  |  2 ++
 lib/sort/sort.go | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/lib/msgstore.go b/lib/msgstore.go
index b0392ba..071a504 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -4,6 +4,7 @@ import (
 	"io"
 	"time"

+	"git.sr.ht/~sircmpwn/aerc/lib/sort"
 	"git.sr.ht/~sircmpwn/aerc/models"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
@@ -383,6 +384,7 @@ func (store *MessageStore) Search(args []string, cb func([]uint32)) {
 	}, func(msg types.WorkerMessage) {
 		switch msg := msg.(type) {
 		case *types.SearchResults:
+			sort.SortBy(msg.Uids, store.uids)
 			cb(msg.Uids)
 		}
 	})
diff --git a/lib/sort/sort.go b/lib/sort/sort.go
index 89c36a9..840f77e 100644
--- a/lib/sort/sort.go
+++ b/lib/sort/sort.go
@@ -3,6 +3,7 @@ package sort
 import (
 	"errors"
 	"fmt"
+	"sort"
 	"strings"

 	"git.sr.ht/~sircmpwn/aerc/worker/types"
@@ -54,3 +55,18 @@ func parseSortField(arg string) (types.SortField, error) {
 		return types.SortArrival, fmt.Errorf("%v is not a valid sort criterion", arg)
 	}
 }
+
+// Sorts toSort by sortBy so that toSort becomes a permutation following the
+// order of sortBy.
+// toSort should be a subset of sortBy
+func SortBy(toSort []uint32, sortBy []uint32) {
+	// build a map from sortBy
+	uidMap := make(map[uint32]int)
+	for i, uid := range sortBy {
+		uidMap[uid] = i
+	}
+	// sortslice of toSort with less function of indexing the map sortBy
+	sort.Slice(toSort, func(i, j int) bool {
+		return uidMap[toSort[i]] < uidMap[toSort[j]]
+	})
+}
--
2.23.0
Thanks!

To git.sr.ht:~sircmpwn/aerc
   00b9ca5..d3379dd  master -> master
View this thread in the archives