diff options
author | Robin Jarry <robin@jarry.cc> | 2022-09-19 23:58:24 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-09-20 20:55:29 +0200 |
commit | bb9ac43e13a96dc5bc501f60c3afb3718fe8085d (patch) | |
tree | e97b7a32ee94beccf7bd85e4a7fd961b3442bc62 /commands/compose/switch.go | |
parent | fcd75ab52973a914bfa5af8e92617c551b59fc1f (diff) | |
download | aerc-bb9ac43e13a96dc5bc501f60c3afb3718fe8085d.tar.gz |
switch: add -n and -p flags for relative switch
Allow switching to next or previous account with switch-account -n and
switch-account -p, respectively. By default, these are bound to Alt-n
and Alt-p.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'commands/compose/switch.go')
-rw-r--r-- | commands/compose/switch.go | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/commands/compose/switch.go b/commands/compose/switch.go index 5109ee05..58702b5b 100644 --- a/commands/compose/switch.go +++ b/commands/compose/switch.go @@ -5,6 +5,7 @@ import ( "fmt" "git.sr.ht/~rjarry/aerc/widgets" + "git.sr.ht/~sircmpwn/getopt" ) type AccountSwitcher interface { @@ -26,12 +27,29 @@ func (SwitchAccount) Complete(aerc *widgets.Aerc, args []string) []string { } func (SwitchAccount) Execute(aerc *widgets.Aerc, args []string) error { - if len(args) != 2 { + 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) { name := "" if acct := aerc.SelectedAccount(); acct != nil { name = fmt.Sprintf("Current account: %s. ", acct.Name()) } - return errors.New(name + "Usage: switch-account <account-name>") + return errors.New(name + "Usage: switch-account [-np] <account-name>") } switcher, ok := aerc.SelectedTabContent().(AccountSwitcher) @@ -39,9 +57,19 @@ func (SwitchAccount) Execute(aerc *widgets.Aerc, args []string) error { return errors.New("this tab cannot switch accounts") } - if acct, err := aerc.Account(args[1]); err != nil { + 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 { return err - } else { - return switcher.SwitchAccount(acct) } + + return switcher.SwitchAccount(acct) } |