aerc/commands/account/sort.go
Koni Marti e50ab59284 sort: keep sort criteria applied to folder
Keep the sort criteria applied to the selected folder until the default
sort order should be restored. Call the sort command without arguments
to restore the default sort order.

The current behavior is that the default sort order is restored as soon
as the folder reloads. This happens often and then the results of the
sort command are lost. This makes the sort command not very
user-friendly. Instead, we should keep the sort criteria applied until
the user explicitly wants to restore the default sort order again.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-03-25 13:07:20 +01:00

90 lines
1.9 KiB
Go

package account
import (
"errors"
"strings"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/sort"
"git.sr.ht/~rjarry/aerc/widgets"
"git.sr.ht/~rjarry/aerc/worker/types"
)
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 or option then suggest all again
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
completions = commands.FilterList(supportedCriteria, last, currentPrefix,
aerc.SelectedAccountUiConfig().FuzzyComplete)
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.")
}
var err error
var sortCriteria []*types.SortCriterion
if len(args[1:]) == 0 {
sortCriteria = acct.GetSortCriteria()
} else {
sortCriteria, err = sort.GetSortCriteria(args[1:])
if err != nil {
return err
}
}
aerc.SetStatus("Sorting")
store.Sort(sortCriteria, func() {
aerc.SetStatus("Sorting complete")
})
return nil
}