From cc172970a079bb78847f2276db8bfae375cda185 Mon Sep 17 00:00:00 2001 From: kt programs Date: Sun, 6 Mar 2022 10:58:07 +0800 Subject: commands: implement fuzzy completion for commands and options Change the option to enable fuzzy completion to be fuzzy-complete, since it's no longer only used for folders Signed-off-by: Kt Programs Acked-by: Koni Marti --- commands/account/recover.go | 35 +++++++++++++++++++++++++++-------- commands/account/sort.go | 15 +++++++++------ 2 files changed, 36 insertions(+), 14 deletions(-) (limited to 'commands/account') diff --git a/commands/account/recover.go b/commands/account/recover.go index a167d500..855d9846 100644 --- a/commands/account/recover.go +++ b/commands/account/recover.go @@ -6,8 +6,8 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~sircmpwn/getopt" @@ -24,24 +24,43 @@ func (Recover) Aliases() []string { } func (Recover) Complete(aerc *widgets.Aerc, args []string) []string { + acct := aerc.SelectedAccount() + if acct == nil { + return make([]string, 0) + } + // file name of temp file is hard-coded in the NewComposer() function files, err := filepath.Glob( filepath.Join(os.TempDir(), "aerc-compose-*.eml"), ) if err != nil { - return []string{} + return make([]string, 0) + } + // if nothing is entered yet, return all files + if len(args) == 0 { + return files } - arg := strings.Join(args, " ") - if arg != "" { - for i, file := range files { - files[i] = strings.Join([]string{arg, file}, " ") + if args[0] == "-" { + return []string{"-f"} + } else if args[0] == "-f" { + if len(args) == 1 { + for i, file := range files { + files[i] = args[0] + " " + file + } + return files + } else { + // only accepts one file to recover + return commands.FilterList(files, args[1], args[0]+" ", acct.UiConfig().FuzzyComplete) } + } else { + // only accepts one file to recover + return commands.FilterList(files, args[0], "", acct.UiConfig().FuzzyComplete) } - return files } func (Recover) Execute(aerc *widgets.Aerc, args []string) error { - if len(Recover{}.Complete(aerc, args)) == 0 { + // Complete() expects to be passed only the arguments, not including the command name + if len(Recover{}.Complete(aerc, args[1:])) == 0 { return errors.New("No messages to recover.") } diff --git a/commands/account/sort.go b/commands/account/sort.go index 89a5e386..15ecbc0c 100644 --- a/commands/account/sort.go +++ b/commands/account/sort.go @@ -4,6 +4,7 @@ import ( "errors" "strings" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/lib/sort" "git.sr.ht/~rjarry/aerc/widgets" ) @@ -19,6 +20,11 @@ func (Sort) Aliases() []string { } func (Sort) Complete(aerc *widgets.Aerc, args []string) []string { + acct := aerc.SelectedAccount() + if acct == nil { + return make([]string, 0) + } + supportedCriteria := []string{ "arrival", "cc", @@ -35,7 +41,7 @@ func (Sort) Complete(aerc *widgets.Aerc, args []string) []string { 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 + // 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 { @@ -54,11 +60,8 @@ func (Sort) Complete(aerc *widgets.Aerc, args []string) []string { return []string{currentPrefix + "-r"} } // the last item is not complete - for _, criteria := range supportedCriteria { - if strings.HasPrefix(criteria, last) { - completions = append(completions, currentPrefix+criteria) - } - } + completions = commands.FilterList(supportedCriteria, last, currentPrefix, + acct.UiConfig().FuzzyComplete) return completions } -- cgit