Debian Codegraph @

commands/choose.go

raw
1 package commands
2
3 import (
4 "fmt"
5
6 "git.sr.ht/~rjarry/aerc/app"
7 )
8
9 type Choose struct {
10 Unused struct{} `opt:"-"`
11 }
12
13 func init() {
14 Register(Choose{})
15 }
16
17 func (Choose) Description() string {
18 return "Prompt to choose from various options."
19 }
20
21 func (Choose) Context() CommandContext {
22 return GLOBAL
23 }
24
25 func (Choose) Aliases() []string {
26 return []string{"choose"}
27 }
28
29 func (Choose) Execute(args []string) error {
30 if len(args) < 5 || len(args)%4 != 1 {
31 return chooseUsage(args[0])
32 }
33
34 choices := []app.Choice{}
35 for i := 0; i+4 < len(args); i += 4 {
36 if args[i+1] != "-o" {
37 return chooseUsage(args[0])
38 }
39 choices = append(choices, app.Choice{
40 Key: args[i+2],
41 Text: args[i+3],
42 Command: args[i+4],
43 })
44 }
45
46 app.RegisterChoices(choices)
47
48 return nil
49 }
50
51 func chooseUsage(cmd string) error {
52 return fmt.Errorf("Usage: %s -o <key> <text> <command> [-o <key> <text> <command>]...", cmd)
53 }