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.
This commit is contained in:
Ben Burwell 2019-08-02 13:10:42 -04:00 committed by Drew DeVault
parent 302bc1cda6
commit 63f934ee71

View file

@ -116,23 +116,31 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
func GetFolders(aerc *widgets.Aerc, args []string) []string { func GetFolders(aerc *widgets.Aerc, args []string) []string {
out := make([]string, 0) out := make([]string, 0)
lower_only := false
if len(args) == 0 { if len(args) == 0 {
return aerc.SelectedAccount().Directories().List() return aerc.SelectedAccount().Directories().List()
} }
for _, rune := range args[0] {
lower_only = lower_only || unicode.IsLower(rune)
}
for _, dir := range aerc.SelectedAccount().Directories().List() { for _, dir := range aerc.SelectedAccount().Directories().List() {
test := dir if hasCaseSmartPrefix(dir, args[0]) {
if lower_only {
test = strings.ToLower(dir)
}
if strings.HasPrefix(test, args[0]) {
out = append(out, dir) out = append(out, dir)
} }
} }
return out 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
}