2019-09-20 00:37:44 +02:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-06 03:58:07 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/sort"
|
2022-03-24 23:12:16 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/statusline"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2022-03-24 23:12:14 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-09-20 00:37:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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, " ") + " "
|
2022-03-06 03:58:07 +01:00
|
|
|
// if there is a completed criteria or option then suggest all again
|
2019-09-20 00:37:44 +02:00
|
|
|
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
|
2022-03-06 03:58:07 +01:00
|
|
|
completions = commands.FilterList(supportedCriteria, last, currentPrefix,
|
2022-03-15 02:55:51 +01:00
|
|
|
aerc.SelectedAccountUiConfig().FuzzyComplete)
|
2019-09-20 00:37:44 +02:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:48:27 +02:00
|
|
|
if c := store.Capabilities(); c != nil {
|
|
|
|
if !c.Sort {
|
|
|
|
return errors.New("Sorting is not available for this backend.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 23:12:14 +01:00
|
|
|
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
|
|
|
|
}
|
2019-09-20 00:37:44 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 23:12:16 +01:00
|
|
|
acct.SetStatus(statusline.Sorting(true))
|
2022-07-05 21:48:40 +02:00
|
|
|
store.Sort(sortCriteria, func(msg types.WorkerMessage) {
|
|
|
|
if _, ok := msg.(*types.Done); ok {
|
|
|
|
acct.SetStatus(statusline.Sorting(false))
|
|
|
|
}
|
2019-09-20 00:37:44 +02:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|