modify-labels: add completion

This commit is contained in:
Reto Brunner 2019-12-21 16:21:27 +01:00 committed by Drew DeVault
parent 40ceee969b
commit 00263bf866
2 changed files with 37 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package commands
import ( import (
"errors" "errors"
"fmt"
"sort" "sort"
"strings" "strings"
"unicode" "unicode"
@ -127,6 +128,40 @@ func GetFolders(aerc *widgets.Aerc, args []string) []string {
return out 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 // hasCaseSmartPrefix checks whether s starts with prefix, using a case
// sensitive match if and only if prefix contains upper case letters. // sensitive match if and only if prefix contains upper case letters.
func hasCaseSmartPrefix(s, prefix string) bool { func hasCaseSmartPrefix(s, prefix string) bool {

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/commands"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/aerc/worker/types" "git.sr.ht/~sircmpwn/aerc/worker/types"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -20,7 +21,7 @@ func (ModifyLabels) Aliases() []string {
} }
func (ModifyLabels) Complete(aerc *widgets.Aerc, args []string) []string { func (ModifyLabels) Complete(aerc *widgets.Aerc, args []string) []string {
return nil return commands.GetLabels(aerc, args)
} }
func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error { func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error {