From 63f934ee71cdf2c96379c393566556b7c289cceb Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Fri, 2 Aug 2019 13:10:42 -0400 Subject: Fix directory completion case sensitivity Before, lower_only was not being correctly set and was only considering whether the string ended with a lowercase sequence. Refactored this with some more explicit functions as the logic is a little confusing. --- commands/commands.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'commands') diff --git a/commands/commands.go b/commands/commands.go index 3f7fbcd4..1e86118b 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -116,23 +116,31 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string { func GetFolders(aerc *widgets.Aerc, args []string) []string { out := make([]string, 0) - lower_only := false if len(args) == 0 { return aerc.SelectedAccount().Directories().List() } - for _, rune := range args[0] { - lower_only = lower_only || unicode.IsLower(rune) - } - for _, dir := range aerc.SelectedAccount().Directories().List() { - test := dir - if lower_only { - test = strings.ToLower(dir) - } - - if strings.HasPrefix(test, args[0]) { + if hasCaseSmartPrefix(dir, args[0]) { out = append(out, dir) } } return out } + +// 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 { + if hasUpper(prefix) { + return strings.HasPrefix(s, prefix) + } + return strings.HasPrefix(strings.ToLower(s), strings.ToLower(prefix)) +} + +func hasUpper(s string) bool { + for _, r := range s { + if unicode.IsUpper(r) { + return true + } + } + return false +} -- cgit