2022-08-15 21:58:43 +02:00
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2022-09-19 23:58:24 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2022-08-15 21:58:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AccountSwitcher interface {
|
|
|
|
SwitchAccount(*widgets.AccountView) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SwitchAccount struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(SwitchAccount{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (SwitchAccount) Aliases() []string {
|
|
|
|
return []string{"switch-account"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (SwitchAccount) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
return aerc.AccountNames()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (SwitchAccount) Execute(aerc *widgets.Aerc, args []string) error {
|
2022-09-19 23:58:24 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "np")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var next, prev bool
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'n':
|
|
|
|
next = true
|
|
|
|
prev = false
|
|
|
|
case 'p':
|
|
|
|
next = false
|
|
|
|
prev = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
posargs := args[optind:]
|
|
|
|
// NOT ((prev || next) XOR (len(posargs) == 1))
|
|
|
|
if (prev || next) == (len(posargs) == 1) {
|
2022-08-15 21:58:43 +02:00
|
|
|
name := ""
|
|
|
|
if acct := aerc.SelectedAccount(); acct != nil {
|
|
|
|
name = fmt.Sprintf("Current account: %s. ", acct.Name())
|
|
|
|
}
|
2022-09-19 23:58:24 +02:00
|
|
|
return errors.New(name + "Usage: switch-account [-np] <account-name>")
|
2022-08-15 21:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switcher, ok := aerc.SelectedTabContent().(AccountSwitcher)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("this tab cannot switch accounts")
|
|
|
|
}
|
|
|
|
|
2022-09-19 23:58:24 +02:00
|
|
|
var acct *widgets.AccountView
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case prev:
|
|
|
|
acct, err = aerc.PrevAccount()
|
|
|
|
case next:
|
|
|
|
acct, err = aerc.NextAccount()
|
|
|
|
default:
|
|
|
|
acct, err = aerc.Account(posargs[0])
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-08-15 21:58:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-09-19 23:58:24 +02:00
|
|
|
|
|
|
|
return switcher.SwitchAccount(acct)
|
2022-08-15 21:58:43 +02:00
|
|
|
}
|