aboutsummaryrefslogtreecommitdiff
path: root/commands/close.go
diff options
context:
space:
mode:
authorAditya Srivastava <adityasri@ocf.berkeley.edu>2019-08-11 20:42:49 -0700
committerDrew DeVault <sir@cmpwn.com>2019-08-13 10:49:34 +0900
commitf0a0c5aa733fa66958c113465bfc5fdd3d7cc9f0 (patch)
tree51749e0d818357854791dbfdd8428d6c29035873 /commands/close.go
parent72204d1f24d4082acc750b73dcda6981edd21a38 (diff)
add close command at global level
Diffstat (limited to 'commands/close.go')
-rw-r--r--commands/close.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/commands/close.go b/commands/close.go
new file mode 100644
index 0000000..c885ee9
--- /dev/null
+++ b/commands/close.go
@@ -0,0 +1,59 @@
+package commands
+
+import (
+ "errors"
+ "fmt"
+
+ "git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+type Close struct{}
+
+func init() {
+ register(Close{})
+}
+
+func (_ Close) Aliases() []string {
+ return []string{"close", "abort"}
+}
+
+func (_ Close) Complete(aerc *widgets.Aerc, args []string) []string {
+ return nil
+}
+
+func (_ Close) Execute(aerc *widgets.Aerc, args []string) error {
+ if len(args) > 2 {
+ return errors.New("Usage: close [tab name]")
+ } else if len(args) == 1 {
+ return CloseTab(aerc, aerc.SelectedTabName())
+ } else {
+ tabname := args[1]
+ for _, tab := range aerc.TabNames() {
+ if tab == tabname {
+ return CloseTab(aerc, tabname)
+ }
+ }
+ return errors.New(fmt.Sprintf("Tab %s not found", tabname))
+ }
+ return nil
+}
+
+func CloseTab(aerc *widgets.Aerc, tabname string) error {
+ curTabIndex := aerc.SelectedTabIndex()
+ aerc.SelectTab(tabname)
+ switch tab := aerc.SelectedTab().(type) {
+ default:
+ aerc.RemoveTab(tab)
+ return nil
+ case *widgets.Terminal:
+ tab.Close(nil)
+ return nil
+ case *widgets.Composer:
+ aerc.RemoveTab(tab)
+ tab.Close()
+ return nil
+ case *widgets.AccountView:
+ aerc.SelectTabIndex(curTabIndex)
+ return errors.New("Cannot close account tab")
+ }
+}