| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/app" |
| 9 |
) |
| 10 |
|
| 11 |
type ChangeTab struct { |
| 12 |
Tab string `opt:"tab" minus:"true" complete:"CompleteTab" desc:"Tab name."` |
| 13 |
} |
| 14 |
|
| 15 |
func init() { |
| 16 |
Register(ChangeTab{}) |
| 17 |
} |
| 18 |
|
| 19 |
func (ChangeTab) Description() string { |
| 20 |
return "Change the focus to the specified tab." |
| 21 |
} |
| 22 |
|
| 23 |
func (ChangeTab) Context() CommandContext { |
| 24 |
return GLOBAL |
| 25 |
} |
| 26 |
|
| 27 |
func (ChangeTab) Aliases() []string { |
| 28 |
return []string{"ct", "change-tab"} |
| 29 |
} |
| 30 |
|
| 31 |
func (*ChangeTab) CompleteTab(arg string) []string { |
| 32 |
return FilterList(app.TabNames(), arg, nil) |
| 33 |
} |
| 34 |
|
| 35 |
func (c ChangeTab) Execute(args []string) error { |
| 36 |
if c.Tab == "-" { |
| 37 |
ok := app.SelectPreviousTab() |
| 38 |
if !ok { |
| 39 |
return errors.New("No previous tab to return to") |
| 40 |
} |
| 41 |
} else { |
| 42 |
n, err := strconv.Atoi(c.Tab) |
| 43 |
if err == nil { |
| 44 |
if strings.HasPrefix(c.Tab, "+") || strings.HasPrefix(c.Tab, "-") { |
| 45 |
app.SelectTabAtOffset(n) |
| 46 |
} else { |
| 47 |
ok := app.SelectTabIndex(n) |
| 48 |
if !ok { |
| 49 |
return errors.New("No tab with that index") |
| 50 |
} |
| 51 |
} |
| 52 |
} else { |
| 53 |
ok := app.SelectTab(c.Tab) |
| 54 |
if !ok { |
| 55 |
return errors.New("No tab with that name") |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
return nil |
| 60 |
} |