about summary refs log tree commit diff stats
path: root/commands/commands.go
diff options
context:
space:
mode:
Diffstat (limited to 'commands/commands.go')
-rw-r--r--commands/commands.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/commands/commands.go b/commands/commands.go
index 1e86118..39720bf 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -2,6 +2,7 @@ package commands
 
 import (
 	"errors"
+	"fmt"
 	"sort"
 	"strings"
 	"unicode"
@@ -127,6 +128,40 @@ func GetFolders(aerc *widgets.Aerc, args []string) []string {
 	return out
 }
 
+func GetLabels(aerc *widgets.Aerc, args []string) []string {
+	if len(args) == 0 {
+		return aerc.SelectedAccount().Labels()
+	}
+
+	// + and - are used to denote tag addition / removal and need to be striped
+	// only the last tag should be completed, so that multiple labels can be
+	// selected
+	last := args[len(args)-1]
+	others := strings.Join(args[:len(args)-1], " ")
+	var prefix string
+	switch last[0] {
+	case '+':
+		prefix = "+"
+	case '-':
+		prefix = "-"
+	default:
+		prefix = ""
+	}
+	trimmed := strings.TrimLeft(last, "+-")
+
+	out := make([]string, 0)
+	for _, label := range aerc.SelectedAccount().Labels() {
+		if hasCaseSmartPrefix(label, trimmed) {
+			var prev string
+			if len(others) > 0 {
+				prev = others + " "
+			}
+			out = append(out, fmt.Sprintf("%v%v%v", prev, prefix, label))
+		}
+	}
+	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 {