1package account 2 3import ( 4 "errors" 5 "strings" 6 7 "git.sr.ht/~sircmpwn/aerc/lib/sort" 8 "git.sr.ht/~sircmpwn/aerc/widgets" 9) 10 11type Sort struct{} 12 13func init() { 14 register(Sort{}) 15} 16 17func (Sort) Aliases() []string { 18 return []string{"sort"} 19} 20 21func (Sort) Complete(aerc *widgets.Aerc, args []string) []string { 22 supportedCriteria := []string{ 23 "arrival", 24 "cc", 25 "date", 26 "from", 27 "read", 28 "size", 29 "subject", 30 "to", 31 } 32 if len(args) == 0 { 33 return supportedCriteria 34 } 35 last := args[len(args)-1] 36 var completions []string 37 currentPrefix := strings.Join(args, " ") + " " 38 // if there is a completed criteria then suggest all again or an option 39 for _, criteria := range append(supportedCriteria, "-r") { 40 if criteria == last { 41 for _, criteria := range supportedCriteria { 42 completions = append(completions, currentPrefix+criteria) 43 } 44 return completions 45 } 46 } 47 48 currentPrefix = strings.Join(args[:len(args)-1], " ") 49 if len(args) > 1 { 50 currentPrefix += " " 51 } 52 // last was beginning an option 53 if last == "-" { 54 return []string{currentPrefix + "-r"} 55 } 56 // the last item is not complete 57 for _, criteria := range supportedCriteria { 58 if strings.HasPrefix(criteria, last) { 59 completions = append(completions, currentPrefix+criteria) 60 } 61 } 62 return completions 63} 64 65func (Sort) Execute(aerc *widgets.Aerc, args []string) error { 66 acct := aerc.SelectedAccount() 67 if acct == nil { 68 return errors.New("No account selected.") 69 } 70 store := acct.Store() 71 if store == nil { 72 return errors.New("Messages still loading.") 73 } 74 75 sortCriteria, err := sort.GetSortCriteria(args[1:]) 76 if err != nil { 77 return err 78 } 79 80 aerc.SetStatus("Sorting") 81 store.Sort(sortCriteria, func() { 82 aerc.SetStatus("Sorting complete") 83 }) 84 return nil 85} 86