summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--commands/choose.go48
-rw-r--r--doc/aerc.1.scd3
-rw-r--r--widgets/aerc.go33
3 files changed, 84 insertions, 0 deletions
diff --git a/commands/choose.go b/commands/choose.go
new file mode 100644
index 0000000..5bf36b1
--- /dev/null
+++ b/commands/choose.go
@@ -0,0 +1,48 @@
+package commands
+
+import (
+	"fmt"
+	"strings"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+type Choose struct{}
+
+func init() {
+	register(Choose{})
+}
+
+func (Choose) Aliases() []string {
+	return []string{"choose"}
+}
+
+func (Choose) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil
+}
+
+func (Choose) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) < 5 || len(args)%4 != 1 {
+		return chooseUsage(args[0])
+	}
+
+	choices := []widgets.Choice{}
+	for i := 0; i+4 < len(args); i+=4 {
+		if args[i+1] != "-o" {
+			return chooseUsage(args[0])
+		}
+		choices = append(choices, widgets.Choice{
+			Key:     args[i+2],
+			Text:    args[i+3],
+			Command: strings.Split(args[i+4], " "),
+		})
+	}
+
+	aerc.RegisterChoices(choices)
+
+	return nil
+}
+
+func chooseUsage(cmd string) error {
+	return fmt.Errorf("Usage: %s -o <key> <text> <command> [-o <key> <text> <command>]...", cmd)
+}
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 0294784..d2e52a9 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -77,6 +77,9 @@ These commands work in any context.
 	passed as one argument to the command, unless it is empty, in which case no
 	extra argument is added.
 
+*choose* -o <key> <text> <command> [-o <key> <text> <command>]...
+	Prompts the user to choose from various options.
+
 *quit*
 	Exits aerc.
 
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 {
rew DeVault <sir@cmpwn.com> 2019-03-30 11:58:24 -0400 Implement :pipe' href='/akspecs/aerc/commit/commands/account/pipe.go?h=0.5.0&id=700dea23fa75f213af2f99449db994008eab9d21'>700dea2 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64