about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-08-02 13:10:42 -0400
committerDrew DeVault <sir@cmpwn.com>2019-08-03 10:48:47 -0400
commit63f934ee71cdf2c96379c393566556b7c289cceb (patch)
treeb4ac6bf64f51bbc2db3cadc04afa31e32c77e43b
parent302bc1cda6a5f138a033b5d970ccee2d0abc0334 (diff)
downloadaerc-63f934ee71cdf2c96379c393566556b7c289cceb.tar.gz
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.
-rw-r--r--commands/commands.go30
1 files changed, 19 insertions, 11 deletions
diff --git a/commands/commands.go b/commands/commands.go
index 3f7fbcd..1e86118 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
+}