about summary refs log tree commit diff stats
path: root/widgets
diff options
context:
space:
mode:
authorRay Ganardi <ray@ganardi.xyz>2020-04-24 11:36:16 +0200
committerDrew DeVault <sir@cmpwn.com>2020-04-24 12:59:21 -0400
commit447e662057c663f47f5c8a490543b1a52b26bc86 (patch)
tree6c44b7234a43b0da25bb448b93bfca8014d864a6 /widgets
parentacf69b7490f4848066f2df4b3c2f675a8d57661a (diff)
downloadaerc-447e662057c663f47f5c8a490543b1a52b26bc86.tar.gz
Add :choose command
Usage:
	*choose* -o <key> <text> <command> [-o <key> <text> <command>]...

Prompts the user to choose from various options.
Diffstat (limited to 'widgets')
-rw-r--r--widgets/aerc.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 1d45696..8307bd0 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -39,6 +39,12 @@ type Aerc struct {
 	getpasswd   *GetPasswd
 }
 
+type Choice struct {
+	Key     string
+	Text    string
+	Command []string
+}
+
 func NewAerc(conf *config.AercConfig, logger *log.Logger,
 	cmd func(cmd []string) error, complete func(cmd string) []string,
 	cmdHistory lib.History) *Aerc {
@@ -444,6 +450,33 @@ func (aerc *Aerc) RegisterPrompt(prompt string, cmd []string) {
 	aerc.prompts.Push(p)
 }
 
+func (aerc *Aerc) RegisterChoices(choices []Choice) {
+	cmds := make(map[string][]string)
+	texts := []string{}
+	for _, c := range choices {
+		text := fmt.Sprintf("[%s] %s", c.Key, c.Text)
+		if strings.Contains(c.Text, c.Key) {
+			text = strings.Replace(c.Text, c.Key, "[" + c.Key + "]", 1)
+		}
+		texts = append(texts, text)
+		cmds[c.Key] = c.Command
+	}
+	prompt := strings.Join(texts, ", ") + "? "
+	p := NewPrompt(aerc.conf, prompt, func(text string) {
+		cmd, ok := cmds[text]
+		if !ok {
+			return
+		}
+		err := aerc.cmd(cmd)
+		if err != nil {
+			aerc.PushError(" " + err.Error())
+		}
+	}, func(cmd string) []string {
+		return nil // TODO: completions
+	})
+	aerc.prompts.Push(p)
+}
+
 func (aerc *Aerc) Mailto(addr *url.URL) error {
 	acct := aerc.SelectedAccount()
 	if acct == nil {