From e0b5f2e08cd766258bdfd5eb392690768d2f5964 Mon Sep 17 00:00:00 2001 From: inwit Date: Mon, 31 Jan 2022 17:32:19 +0100 Subject: completions: add folder flexible search as an option Provide an option to change the completion style when selecting a folder from completing with folders starting with the input string to completing with folders in which the input string is a substring present at any point in the folder name. References: https://lists.sr.ht/~sircmpwn/aerc/%3C20201129181020.186984-1-inwit%40sindominio.net%3E References: https://lists.sr.ht/~sircmpwn/aerc/%3C20210223202536.199355-1-clayton%40craftyguy.net%3E Signed-off-by: inwit --- commands/commands.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'commands/commands.go') diff --git a/commands/commands.go b/commands/commands.go index 37778356..cb5b63bf 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -121,7 +121,7 @@ func GetFolders(aerc *widgets.Aerc, args []string) []string { return aerc.SelectedAccount().Directories().List() } for _, dir := range aerc.SelectedAccount().Directories().List() { - if hasCaseSmartPrefix(dir, args[0]) { + if foundInString(dir, args[0], aerc.SelectedAccount().UiConfig().FuzzyFolderComplete) { out = append(out, dir) } } @@ -177,6 +177,14 @@ func GetLabels(aerc *widgets.Aerc, args []string) []string { 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 { @@ -186,6 +194,11 @@ 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) { -- cgit