aboutsummaryrefslogtreecommitdiffstats
path: root/commands/commands.go
diff options
context:
space:
mode:
authorkt programs <ktprograms@gmail.com>2022-03-06 10:58:07 +0800
committerRobin Jarry <robin@jarry.cc>2022-03-07 10:18:50 +0100
commitcc172970a079bb78847f2276db8bfae375cda185 (patch)
tree143f9f049d7faf01e6ab1b07aac5017c516efbb2 /commands/commands.go
parent55ae3d2cab8489609a1b11c169c28306730a71ea (diff)
downloadaerc-cc172970a079bb78847f2276db8bfae375cda185.tar.gz
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 <ktprograms@gmail.com> Acked-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'commands/commands.go')
-rw-r--r--commands/commands.go57
1 files changed, 12 insertions, 45 deletions
diff --git a/commands/commands.go b/commands/commands.go
index 70a77b92..c23df7e3 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -2,7 +2,6 @@ package commands
import (
"errors"
- "fmt"
"sort"
"strings"
"unicode"
@@ -74,12 +73,14 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
return nil
}
+ // nothing entered, list all commands
if len(args) == 0 {
names := cmds.Names()
sort.Strings(names)
return names
}
+ // complete options
if len(args) > 1 || cmd[len(cmd)-1] == ' ' {
if cmd, ok := cmds.dict()[args[0]]; ok {
var completions []string
@@ -101,13 +102,9 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
return nil
}
+ // complete available commands
names := cmds.Names()
- options := make([]string, 0)
- for _, name := range names {
- if strings.HasPrefix(name, args[0]) {
- options = append(options, name)
- }
- }
+ options := FilterList(names, args[0], "", aerc.SelectedAccount().UiConfig().FuzzyComplete)
if len(options) > 0 {
return options
@@ -116,35 +113,23 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
}
func GetFolders(aerc *widgets.Aerc, args []string) []string {
- out := make([]string, 0)
acct := aerc.SelectedAccount()
if acct == nil {
- return out
+ return make([]string, 0)
}
if len(args) == 0 {
return acct.Directories().List()
}
- for _, dir := range acct.Directories().List() {
- if foundInString(dir, args[0], acct.UiConfig().FuzzyFolderComplete) {
- out = append(out, dir)
- }
- }
- return out
+ return FilterList(acct.Directories().List(), args[0], "", acct.UiConfig().FuzzyComplete)
}
// CompletionFromList provides a convenience wrapper for commands to use in the
// Complete function. It simply matches the items provided in valid
-func CompletionFromList(valid []string, args []string) []string {
- out := make([]string, 0)
+func CompletionFromList(aerc *widgets.Aerc, valid []string, args []string) []string {
if len(args) == 0 {
return valid
}
- for _, v := range valid {
- if hasCaseSmartPrefix(v, args[0]) {
- out = append(out, v)
- }
- }
- return out
+ return FilterList(valid, args[0], "", aerc.SelectedAccount().UiConfig().FuzzyComplete)
}
func GetLabels(aerc *widgets.Aerc, args []string) []string {
@@ -172,27 +157,14 @@ func GetLabels(aerc *widgets.Aerc, args []string) []string {
}
trimmed := strings.TrimLeft(last, "+-")
- out := make([]string, 0)
- for _, label := range acct.Labels() {
- if hasCaseSmartPrefix(label, trimmed) {
- var prev string
- if len(others) > 0 {
- prev = others + " "
- }
- out = append(out, fmt.Sprintf("%v%v%v", prev, prefix, label))
- }
+ var prev string
+ if len(others) > 0 {
+ prev = others + " "
}
+ out := FilterList(acct.Labels(), trimmed, prev+prefix, acct.UiConfig().FuzzyComplete)
return out
}
-func foundInString(s, substring string, fuzzy bool) bool {
- if fuzzy {
- return caseInsensitiveContains(s, substring)
- } else {
- return hasCaseSmartPrefix(s, substring)
- }
-}
-
// hasCaseSmartPrefix checks whether s starts with prefix, using a case
// sensitive match if and only if prefix contains upper case letters.
func hasCaseSmartPrefix(s, prefix string) bool {
@@ -202,11 +174,6 @@ func hasCaseSmartPrefix(s, prefix string) bool {
return strings.HasPrefix(strings.ToLower(s), strings.ToLower(prefix))
}
-func caseInsensitiveContains(s, substr string) bool {
- s, substr = strings.ToUpper(s), strings.ToUpper(substr)
- return strings.Contains(s, substr)
-}
-
func hasUpper(s string) bool {
for _, r := range s {
if unicode.IsUpper(r) {