From 2dfeb7130a8fb97d927a55efa738f110f46cb688 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 30 Oct 2023 21:05:56 +0100 Subject: completion: refactor filter list api Remove CompletionFromList which is a trivial wrapper around FilterList. Remove the prefix, suffix and isFuzzy arguments from FilterList. Replace prefix, suffix by an optional callback to allow post processing of completion results before presenting them to the user. Signed-off-by: Robin Jarry Tested-by: Inwit --- commands/account/mkdir.go | 10 +++++++--- commands/account/recover.go | 2 +- commands/account/search.go | 6 +++--- commands/account/sort.go | 2 +- commands/commands.go | 18 ++++++------------ commands/compose/detach.go | 2 +- commands/compose/header.go | 2 +- commands/compose/multipart.go | 2 +- commands/compose/send.go | 2 +- commands/compose/switch.go | 2 +- commands/ct.go | 2 +- commands/help.go | 2 +- commands/msg/archive.go | 2 +- commands/msg/read.go | 2 +- commands/msgview/open-link.go | 2 +- commands/prompt.go | 2 +- commands/util.go | 21 ++++++++++++++++----- 17 files changed, 45 insertions(+), 36 deletions(-) (limited to 'commands') diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go index 3e546ace..af3d1045 100644 --- a/commands/account/mkdir.go +++ b/commands/account/mkdir.go @@ -7,6 +7,7 @@ import ( "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/worker/types" + "git.sr.ht/~rjarry/go-opt" ) type MakeDir struct { @@ -26,10 +27,13 @@ func (*MakeDir) CompleteFolder(arg string) []string { if acct == nil { return nil } + sep := app.SelectedAccount().Worker().PathSeparator() return commands.FilterList( - acct.Directories().List(), arg, "", - app.SelectedAccount().Worker().PathSeparator(), - app.SelectedAccountUiConfig().FuzzyComplete) + acct.Directories().List(), arg, + func(s string) string { + return opt.QuoteArg(s) + sep + }, + ) } func (m MakeDir) Execute(args []string) error { diff --git a/commands/account/recover.go b/commands/account/recover.go index cba6e0cb..a2170edd 100644 --- a/commands/account/recover.go +++ b/commands/account/recover.go @@ -39,7 +39,7 @@ func (*Recover) CompleteFile(arg string) []string { if err != nil { return nil } - return commands.CompletionFromList(files, arg) + return commands.FilterList(files, arg, nil) } func (r Recover) Execute(args []string) error { diff --git a/commands/account/search.go b/commands/account/search.go index ca1b9684..10481e8e 100644 --- a/commands/account/search.go +++ b/commands/account/search.go @@ -42,15 +42,15 @@ func (SearchFilter) Aliases() []string { } func (*SearchFilter) CompleteFlag(arg string) []string { - return commands.CompletionFromList(commands.GetFlagList(), arg) + return commands.FilterList(commands.GetFlagList(), arg, commands.QuoteSpace) } func (*SearchFilter) CompleteAddress(arg string) []string { - return commands.CompletionFromList(commands.GetAddress(arg), arg) + return commands.FilterList(commands.GetAddress(arg), arg, commands.QuoteSpace) } func (*SearchFilter) CompleteDate(arg string) []string { - return commands.CompletionFromList(commands.GetDateList(), arg) + return commands.FilterList(commands.GetDateList(), arg, commands.QuoteSpace) } func (s *SearchFilter) ParseRead(arg string) error { diff --git a/commands/account/sort.go b/commands/account/sort.go index ccccab25..3103a388 100644 --- a/commands/account/sort.go +++ b/commands/account/sort.go @@ -38,7 +38,7 @@ var supportedCriteria = []string{ } func (*Sort) CompleteCriteria(arg string) []string { - return commands.CompletionFromList(supportedCriteria, arg) + return commands.FilterList(supportedCriteria, arg, commands.QuoteSpace) } func (Sort) Execute(args []string) error { diff --git a/commands/commands.go b/commands/commands.go index 9c193018..2893aa82 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -163,9 +163,7 @@ func GetTemplateCompletion( options := FilterList( templates.Terms(), strings.TrimSpace(search), - "", - "", - app.SelectedAccountUiConfig().FuzzyComplete, + nil, ) return options, prefix + padding, true case countLeft == countRight: @@ -196,7 +194,7 @@ func GetFolders(arg string) []string { if acct == nil { return make([]string, 0) } - return CompletionFromList(acct.Directories().List(), arg) + return FilterList(acct.Directories().List(), arg, nil) } func GetTemplates(arg string) []string { @@ -213,13 +211,7 @@ func GetTemplates(arg string) []string { names = append(names, n) } sort.Strings(names) - return CompletionFromList(names, arg) -} - -// CompletionFromList provides a convenience wrapper for commands to use in a -// complete callback. It simply matches the items provided in valid -func CompletionFromList(valid []string, arg string) []string { - return FilterList(valid, arg, "", "", app.SelectedAccountUiConfig().FuzzyComplete) + return FilterList(names, arg, nil) } func GetLabels(arg string) []string { @@ -240,7 +232,9 @@ func GetLabels(arg string) []string { } arg = strings.TrimLeft(arg, "+-") } - return FilterList(acct.Labels(), arg, prefix, " ", acct.UiConfig().FuzzyComplete) + return FilterList(acct.Labels(), arg, func(s string) string { + return opt.QuoteArg(prefix+s) + " " + }) } // hasCaseSmartPrefix checks whether s starts with prefix, using a case diff --git a/commands/compose/detach.go b/commands/compose/detach.go index 91cf2a58..0ac3334e 100644 --- a/commands/compose/detach.go +++ b/commands/compose/detach.go @@ -21,7 +21,7 @@ func (Detach) Aliases() []string { func (*Detach) CompletePath(arg string) []string { composer, _ := app.SelectedTabContent().(*app.Composer) - return commands.CompletionFromList(composer.GetAttachments(), arg) + return commands.FilterList(composer.GetAttachments(), arg, nil) } func (d Detach) Execute(args []string) error { diff --git a/commands/compose/header.go b/commands/compose/header.go index afc27e92..3283d4e3 100644 --- a/commands/compose/header.go +++ b/commands/compose/header.go @@ -38,7 +38,7 @@ func (Header) Options() string { } func (*Header) CompleteHeaders(arg string) []string { - return commands.CompletionFromList(headers, arg) + return commands.FilterList(headers, arg, commands.QuoteSpace) } func (h Header) Execute(args []string) error { diff --git a/commands/compose/multipart.go b/commands/compose/multipart.go index 5b701342..a004ea2b 100644 --- a/commands/compose/multipart.go +++ b/commands/compose/multipart.go @@ -27,7 +27,7 @@ func (*Multipart) CompleteMime(arg string) []string { for mime := range config.Converters { completions = append(completions, mime) } - return commands.CompletionFromList(completions, arg) + return commands.FilterList(completions, arg, nil) } func (m Multipart) Execute(args []string) error { diff --git a/commands/compose/send.go b/commands/compose/send.go index 91fd42bd..315b6915 100644 --- a/commands/compose/send.go +++ b/commands/compose/send.go @@ -41,7 +41,7 @@ func (Send) Aliases() []string { } func (*Send) CompleteArchive(arg string) []string { - return commands.CompletionFromList(msg.ARCHIVE_TYPES, arg) + return commands.FilterList(msg.ARCHIVE_TYPES, arg, nil) } func (*Send) CompleteFolders(arg string) []string { diff --git a/commands/compose/switch.go b/commands/compose/switch.go index 637099b5..c71716e0 100644 --- a/commands/compose/switch.go +++ b/commands/compose/switch.go @@ -26,7 +26,7 @@ func (SwitchAccount) Aliases() []string { } func (*SwitchAccount) CompleteAccount(arg string) []string { - return commands.CompletionFromList(app.AccountNames(), arg) + return commands.FilterList(app.AccountNames(), arg, nil) } func (s SwitchAccount) Execute(args []string) error { diff --git a/commands/ct.go b/commands/ct.go index 2d057b4f..9948e691 100644 --- a/commands/ct.go +++ b/commands/ct.go @@ -21,7 +21,7 @@ func (ChangeTab) Aliases() []string { } func (*ChangeTab) CompleteTab(arg string) []string { - return CompletionFromList(app.TabNames(), arg) + return FilterList(app.TabNames(), arg, nil) } func (c ChangeTab) Execute(args []string) error { diff --git a/commands/help.go b/commands/help.go index 07332303..a3cfff40 100644 --- a/commands/help.go +++ b/commands/help.go @@ -36,7 +36,7 @@ func (Help) Aliases() []string { } func (*Help) CompleteTopic(arg string) []string { - return CompletionFromList(pages, arg) + return FilterList(pages, arg, nil) } func (h *Help) ParseTopic(arg string) error { diff --git a/commands/msg/archive.go b/commands/msg/archive.go index 34cba8b8..5c97e2da 100644 --- a/commands/msg/archive.go +++ b/commands/msg/archive.go @@ -43,7 +43,7 @@ func (Archive) Aliases() []string { } func (*Archive) CompleteType(arg string) []string { - return commands.CompletionFromList(ARCHIVE_TYPES, arg) + return commands.FilterList(ARCHIVE_TYPES, arg, nil) } func (a Archive) Execute(args []string) error { diff --git a/commands/msg/read.go b/commands/msg/read.go index 72159a53..a92a4d0a 100644 --- a/commands/msg/read.go +++ b/commands/msg/read.go @@ -46,7 +46,7 @@ func (f *FlagMsg) ParseFlag(arg string) error { var validFlags = []string{"seen", "answered", "flagged"} func (*FlagMsg) CompleteFlag(arg string) []string { - return commands.CompletionFromList(validFlags, arg) + return commands.FilterList(validFlags, arg, nil) } // If this was called as 'flag' or 'unflag', without the toggle (-t) diff --git a/commands/msgview/open-link.go b/commands/msgview/open-link.go index eceb4232..b13f5d4f 100644 --- a/commands/msgview/open-link.go +++ b/commands/msgview/open-link.go @@ -27,7 +27,7 @@ func (*OpenLink) CompleteUrl(arg string) []string { mv := app.SelectedTabContent().(*app.MessageViewer) if mv != nil { if p := mv.SelectedMessagePart(); p != nil { - return commands.CompletionFromList(p.Links, arg) + return commands.FilterList(p.Links, arg, nil) } } return nil diff --git a/commands/prompt.go b/commands/prompt.go index d791f7a9..4fcf8a80 100644 --- a/commands/prompt.go +++ b/commands/prompt.go @@ -20,7 +20,7 @@ func (Prompt) Aliases() []string { } func (*Prompt) CompleteCommand(arg string) []string { - return CompletionFromList(GlobalCommands.Names(), arg) + return FilterList(GlobalCommands.Names(), arg, nil) } func (p Prompt) Execute(args []string) error { diff --git a/commands/util.go b/commands/util.go index 726669af..c2c530da 100644 --- a/commands/util.go +++ b/commands/util.go @@ -228,18 +228,29 @@ func MsgInfoFromUids(store *lib.MessageStore, uids []uint32, statusInfo func(str return infos, nil } +func QuoteSpace(s string) string { + return opt.QuoteArg(s) + " " +} + // FilterList takes a list of valid completions and filters it, either -// by case smart prefix, or by fuzzy matching, prepending "prefix" to each completion -func FilterList(valid []string, search, prefix, suffix string, isFuzzy bool) []string { +// by case smart prefix, or by fuzzy matching +// An optional post processing function can be passed to prepend, append or +// quote each value. +func FilterList( + valid []string, search string, postProc func(string) string, +) []string { + if postProc == nil { + postProc = opt.QuoteArg + } out := make([]string, 0, len(valid)) - if isFuzzy { + if app.SelectedAccountUiConfig().FuzzyComplete { for _, v := range fuzzy.RankFindFold(search, valid) { - out = append(out, opt.QuoteArg(prefix+v.Target)+suffix) + out = append(out, postProc(v.Target)) } } else { for _, v := range valid { if hasCaseSmartPrefix(v, search) { - out = append(out, opt.QuoteArg(prefix+v)+suffix) + out = append(out, postProc(v)) } } } -- cgit