summary refs log tree commit diff stats
path: root/commands/account/sort.go
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-09-19 23:37:44 +0100
committerDrew DeVault <sir@cmpwn.com>2019-09-20 14:56:02 -0400
commit90d26da58a4af2d34328f5916adf3781222966c6 (patch)
tree58a60e0d42b183f94d12b35e19ed1b046d03d5cd /commands/account/sort.go
parent43435ba06cd0820a83f14630881981b338473cb8 (diff)
downloadaerc-90d26da58a4af2d34328f5916adf3781222966c6.tar.gz
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.
Diffstat (limited to 'commands/account/sort.go')
-rw-r--r--commands/account/sort.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/commands/account/sort.go b/commands/account/sort.go
new file mode 100644
index 0000000..6202578
--- /dev/null
+++ b/commands/account/sort.go
@@ -0,0 +1,85 @@
+package account
+
+import (
+	"errors"
+	"strings"
+
+	"git.sr.ht/~sircmpwn/aerc/lib/sort"
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+type Sort struct{}
+
+func init() {
+	register(Sort{})
+}
+
+func (Sort) Aliases() []string {
+	return []string{"sort"}
+}
+
+func (Sort) Complete(aerc *widgets.Aerc, args []string) []string {
+	supportedCriteria := []string{
+		"arrival",
+		"cc",
+		"date",
+		"from",
+		"read",
+		"size",
+		"subject",
+		"to",
+	}
+	if len(args) == 0 {
+		return supportedCriteria
+	}
+	last := args[len(args)-1]
+	var completions []string
+	currentPrefix := strings.Join(args, " ") + " "
+	// if there is a completed criteria then suggest all again or an option
+	for _, criteria := range append(supportedCriteria, "-r") {
+		if criteria == last {
+			for _, criteria := range supportedCriteria {
+				completions = append(completions, currentPrefix+criteria)
+			}
+			return completions
+		}
+	}
+
+	currentPrefix = strings.Join(args[:len(args)-1], " ")
+	if len(args) > 1 {
+		currentPrefix += " "
+	}
+	// last was beginning an option
+	if last == "-" {
+		return []string{currentPrefix + "-r"}
+	}
+	// the last item is not complete
+	for _, criteria := range supportedCriteria {
+		if strings.HasPrefix(criteria, last) {
+			completions = append(completions, currentPrefix+criteria)
+		}
+	}
+	return completions
+}
+
+func (Sort) Execute(aerc *widgets.Aerc, args []string) error {
+	acct := aerc.SelectedAccount()
+	if acct == nil {
+		return errors.New("No account selected.")
+	}
+	store := acct.Store()
+	if store == nil {
+		return errors.New("Messages still loading.")
+	}
+
+	sortCriteria, err := sort.GetSortCriteria(args[1:])
+	if err != nil {
+		return err
+	}
+
+	aerc.SetStatus("Sorting")
+	store.Sort(sortCriteria, func() {
+		aerc.SetStatus("Sorting complete")
+	})
+	return nil
+}