From bb9ac43e13a96dc5bc501f60c3afb3718fe8085d Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 19 Sep 2022 23:58:24 +0200 Subject: 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 Tested-by: Koni Marti --- commands/compose/switch.go | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'commands/compose') 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 ") + return errors.New(name + "Usage: switch-account [-np] ") } 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) } -- cgit