Debian Codegraph @

commands/compose/switch.go

raw
1 package compose
2
3 import (
4 "errors"
5
6 "git.sr.ht/~rjarry/aerc/app"
7 "git.sr.ht/~rjarry/aerc/commands"
8 )
9
10 type AccountSwitcher interface {
11 SwitchAccount(*app.AccountView) error
12 }
13
14 type SwitchAccount struct {
15 Prev bool `opt:"-p" desc:"Switch to previous account."`
16 Next bool `opt:"-n" desc:"Switch to next account."`
17 Account string `opt:"account" required:"false" complete:"CompleteAccount" desc:"Account name."`
18 }
19
20 func init() {
21 commands.Register(SwitchAccount{})
22 }
23
24 func (SwitchAccount) Description() string {
25 return "Change composing from the specified account."
26 }
27
28 func (SwitchAccount) Context() commands.CommandContext {
29 return commands.COMPOSE_EDIT | commands.COMPOSE_REVIEW
30 }
31
32 func (SwitchAccount) Aliases() []string {
33 return []string{"switch-account"}
34 }
35
36 func (*SwitchAccount) CompleteAccount(arg string) []string {
37 return commands.FilterList(app.AccountNames(), arg, nil)
38 }
39
40 func (s SwitchAccount) Execute(args []string) error {
41 if !s.Prev && !s.Next && s.Account == "" {
42 return errors.New("Usage: switch-account -n | -p | <account-name>")
43 }
44
45 switcher, ok := app.SelectedTabContent().(AccountSwitcher)
46 if !ok {
47 return errors.New("this tab cannot switch accounts")
48 }
49
50 var acct *app.AccountView
51 var err error
52
53 switch {
54 case s.Prev:
55 acct, err = app.PrevAccount()
56 case s.Next:
57 acct, err = app.NextAccount()
58 default:
59 acct, err = app.Account(s.Account)
60 }
61 if err != nil {
62 return err
63 }
64 if err = switcher.SwitchAccount(acct); err != nil {
65 return err
66 }
67 acct.UpdateStatus()
68
69 return nil
70 }