about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/msgstore.go17
-rw-r--r--lib/sort/sort.go56
2 files changed, 72 insertions, 1 deletions
diff --git a/lib/msgstore.go b/lib/msgstore.go
index 1f18fbf..b0392ba 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -25,6 +25,8 @@ type MessageStore struct {
 	resultIndex int
 	filter      bool
 
+	defaultSortCriteria []*types.SortCriterion
+
 	// Map of uids we've asked the worker to fetch
 	onUpdate       func(store *MessageStore) // TODO: multiple onUpdate handlers
 	onUpdateDirs   func()
@@ -38,6 +40,7 @@ type MessageStore struct {
 
 func NewMessageStore(worker *types.Worker,
 	dirInfo *models.DirectoryInfo,
+	defaultSortCriteria []*types.SortCriterion,
 	triggerNewEmail func(*models.MessageInfo),
 	triggerDirectoryChange func()) *MessageStore {
 
@@ -49,6 +52,8 @@ func NewMessageStore(worker *types.Worker,
 		bodyCallbacks:   make(map[uint32][]func(io.Reader)),
 		headerCallbacks: make(map[uint32][]func(*types.MessageInfo)),
 
+		defaultSortCriteria: defaultSortCriteria,
+
 		pendingBodies:  make(map[uint32]interface{}),
 		pendingHeaders: make(map[uint32]interface{}),
 		worker:         worker,
@@ -151,7 +156,9 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
 	switch msg := msg.(type) {
 	case *types.DirectoryInfo:
 		store.DirInfo = *msg.Info
-		store.worker.PostAction(&types.FetchDirectoryContents{}, nil)
+		store.worker.PostAction(&types.FetchDirectoryContents{
+			SortCriteria: store.defaultSortCriteria,
+		}, nil)
 		update = true
 	case *types.DirectoryContents:
 		newMap := make(map[uint32]*models.MessageInfo)
@@ -434,3 +441,11 @@ func (store *MessageStore) ModifyLabels(uids []uint32, add, remove []string,
 		Remove: remove,
 	}, cb)
 }
+
+func (store *MessageStore) Sort(criteria []*types.SortCriterion, cb func()) {
+	store.worker.PostAction(&types.FetchDirectoryContents{
+		SortCriteria: criteria,
+	}, func(msg types.WorkerMessage) {
+		cb()
+	})
+}
diff --git a/lib/sort/sort.go b/lib/sort/sort.go
new file mode 100644
index 0000000..89c36a9
--- /dev/null
+++ b/lib/sort/sort.go
@@ -0,0 +1,56 @@
+package sort
+
+import (
+	"errors"
+	"fmt"
+	"strings"
+
+	"git.sr.ht/~sircmpwn/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
+	default:
+		return types.SortArrival, fmt.Errorf("%v is not a valid sort criterion", arg)
+	}
+}