1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-02-23 07:53:59 +01:00
aerc/lib/sort/sort.go
Robin Jarry 73dc39c6ee treewide: replace uint32 uids with opaque strings
Add a new models.UID type (an alias to string). Replace all occurrences
of uint32 being used as message UID or thread UID with models.UID.

Update all workers to only expose models.UID values and deal with the
conversion internally. Only IMAP needs to convert these to uint32. All
other backends already use plain strings as message identifiers, in
which case no conversion is even needed.

The directory tree implementation needed to be heavily refactored in
order to accommodate thread UID not being usable as a list index.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Inwit <inwit@sindominio.net>
Tested-by: Tim Culverhouse <tim@timculverhouse.com>
2024-08-28 12:06:01 +02:00

87 lines
1.9 KiB
Go

package sort
import (
"errors"
"fmt"
"sort"
"strings"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
func GetSortCriteria(args []string) ([]*types.SortCriterion, error) {
var sortCriteria []*types.SortCriterion
reverse := false
for _, arg := range args {
if arg == "-r" {
reverse = true
continue
}
field, err := parseSortField(arg)
if err != nil {
return nil, err
}
sortCriteria = append(sortCriteria, &types.SortCriterion{
Field: field,
Reverse: reverse,
})
reverse = false
}
if reverse {
return nil, errors.New("Expected argument to reverse")
}
return sortCriteria, nil
}
func parseSortField(arg string) (types.SortField, error) {
switch strings.ToLower(arg) {
case "arrival":
return types.SortArrival, nil
case "cc":
return types.SortCc, nil
case "date":
return types.SortDate, nil
case "from":
return types.SortFrom, nil
case "read":
return types.SortRead, nil
case "size":
return types.SortSize, nil
case "subject":
return types.SortSubject, nil
case "to":
return types.SortTo, nil
case "flagged":
return types.SortFlagged, nil
default:
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 []models.UID, sortBy []models.UID) {
// build a map from sortBy
uidMap := make(map[models.UID]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]]
})
}
// 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]]
})
}