Preserve sorting order in search results

This ensures that the search results follow the order of the current
sort so that cycling throught the results proceeds in displayed order.
This commit is contained in:
Jeffas 2019-10-02 17:16:15 +01:00 committed by Drew DeVault
parent 1339faf788
commit d3379dd7f0
2 changed files with 18 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"io" "io"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/lib/sort"
"git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/worker/types" "git.sr.ht/~sircmpwn/aerc/worker/types"
) )
@ -386,6 +387,7 @@ func (store *MessageStore) Search(args []string, cb func([]uint32)) {
}, func(msg types.WorkerMessage) { }, func(msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.SearchResults: case *types.SearchResults:
sort.SortBy(msg.Uids, store.uids)
cb(msg.Uids) cb(msg.Uids)
} }
}) })

View file

@ -3,6 +3,7 @@ package sort
import ( import (
"errors" "errors"
"fmt" "fmt"
"sort"
"strings" "strings"
"git.sr.ht/~sircmpwn/aerc/worker/types" "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) 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]]
})
}