aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-12-04 23:39:49 +0100
committerRobin Jarry <robin@jarry.cc>2024-01-20 21:56:25 +0100
commit159fb38daf5336758abc425447cf2c2ed51de59a (patch)
treee7be3bea878b12e441332f89d7bc3c63db477c05 /commands/account
parentd2817371867e94b621de4054b235d53312db8073 (diff)
downloadaerc-159fb38daf5336758abc425447cf2c2ed51de59a.tar.gz
commands: refactor registration
Register all commands with the same function and store them in the same map. Use bit flags to determine in which contexts each command should be available. Remove duplicate commands now that the same command can be exposed in multiple contexts. Refactor API to allow executing commands from other commands without import cycles. Signed-off-by: Robin Jarry <robin@jarry.cc> Tested-by: Bence Ferdinandy <bence@ferdinandy.com> Tested-by: Johannes Thyssen Tishman <johannes@thyssentishman.com>
Diffstat (limited to 'commands/account')
-rw-r--r--commands/account/account.go14
-rw-r--r--commands/account/cf.go6
-rw-r--r--commands/account/check-mail.go7
-rw-r--r--commands/account/clear.go7
-rw-r--r--commands/account/compose.go6
-rw-r--r--commands/account/connection.go7
-rw-r--r--commands/account/expand-folder.go7
-rw-r--r--commands/account/export-mbox.go6
-rw-r--r--commands/account/import-mbox.go6
-rw-r--r--commands/account/mkdir.go6
-rw-r--r--commands/account/next-folder.go7
-rw-r--r--commands/account/next-result.go7
-rw-r--r--commands/account/next.go54
-rw-r--r--commands/account/recover.go6
-rw-r--r--commands/account/rmdir.go7
-rw-r--r--commands/account/search.go6
-rw-r--r--commands/account/select.go7
-rw-r--r--commands/account/sort.go6
-rw-r--r--commands/account/split.go7
-rw-r--r--commands/account/view.go7
20 files changed, 144 insertions, 42 deletions
diff --git a/commands/account/account.go b/commands/account/account.go
deleted file mode 100644
index fc17fa2d..00000000
--- a/commands/account/account.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package account
-
-import (
- "git.sr.ht/~rjarry/aerc/commands"
-)
-
-var AccountCommands *commands.Commands
-
-func register(cmd commands.Command) {
- if AccountCommands == nil {
- AccountCommands = commands.NewCommands()
- }
- AccountCommands.Register(cmd)
-}
diff --git a/commands/account/cf.go b/commands/account/cf.go
index 579ddd72..2f32e8bc 100644
--- a/commands/account/cf.go
+++ b/commands/account/cf.go
@@ -23,7 +23,11 @@ type ChangeFolder struct {
func init() {
history = make(map[string]string)
- register(ChangeFolder{})
+ commands.Register(ChangeFolder{})
+}
+
+func (ChangeFolder) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (ChangeFolder) Aliases() []string {
diff --git a/commands/account/check-mail.go b/commands/account/check-mail.go
index d31b2648..bc41a935 100644
--- a/commands/account/check-mail.go
+++ b/commands/account/check-mail.go
@@ -4,12 +4,17 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type CheckMail struct{}
func init() {
- register(CheckMail{})
+ commands.Register(CheckMail{})
+}
+
+func (CheckMail) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (CheckMail) Aliases() []string {
diff --git a/commands/account/clear.go b/commands/account/clear.go
index dec6bcd2..46335431 100644
--- a/commands/account/clear.go
+++ b/commands/account/clear.go
@@ -4,6 +4,7 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/state"
)
@@ -12,7 +13,11 @@ type Clear struct {
}
func init() {
- register(Clear{})
+ commands.Register(Clear{})
+}
+
+func (Clear) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (Clear) Aliases() []string {
diff --git a/commands/account/compose.go b/commands/account/compose.go
index fe86a179..e45f77c3 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -24,7 +24,11 @@ type Compose struct {
}
func init() {
- register(Compose{})
+ commands.Register(Compose{})
+}
+
+func (Compose) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (c *Compose) ParseHeader(arg string) error {
diff --git a/commands/account/connection.go b/commands/account/connection.go
index d633c1ce..75171cf4 100644
--- a/commands/account/connection.go
+++ b/commands/account/connection.go
@@ -4,6 +4,7 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/state"
"git.sr.ht/~rjarry/aerc/worker/types"
)
@@ -11,7 +12,11 @@ import (
type Connection struct{}
func init() {
- register(Connection{})
+ commands.Register(Connection{})
+}
+
+func (Connection) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (Connection) Aliases() []string {
diff --git a/commands/account/expand-folder.go b/commands/account/expand-folder.go
index c264872a..5bb686e0 100644
--- a/commands/account/expand-folder.go
+++ b/commands/account/expand-folder.go
@@ -4,12 +4,17 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type ExpandCollapseFolder struct{}
func init() {
- register(ExpandCollapseFolder{})
+ commands.Register(ExpandCollapseFolder{})
+}
+
+func (ExpandCollapseFolder) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (ExpandCollapseFolder) Aliases() []string {
diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go
index 6a36d375..53f22d87 100644
--- a/commands/account/export-mbox.go
+++ b/commands/account/export-mbox.go
@@ -22,7 +22,11 @@ type ExportMbox struct {
}
func init() {
- register(ExportMbox{})
+ commands.Register(ExportMbox{})
+}
+
+func (ExportMbox) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (ExportMbox) Aliases() []string {
diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go
index c38aeb8f..2d9c81f9 100644
--- a/commands/account/import-mbox.go
+++ b/commands/account/import-mbox.go
@@ -23,7 +23,11 @@ type ImportMbox struct {
}
func init() {
- register(ImportMbox{})
+ commands.Register(ImportMbox{})
+}
+
+func (ImportMbox) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (ImportMbox) Aliases() []string {
diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go
index 7e49ad8b..c08c6d4b 100644
--- a/commands/account/mkdir.go
+++ b/commands/account/mkdir.go
@@ -15,7 +15,11 @@ type MakeDir struct {
}
func init() {
- register(MakeDir{})
+ commands.Register(MakeDir{})
+}
+
+func (MakeDir) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (MakeDir) Aliases() []string {
diff --git a/commands/account/next-folder.go b/commands/account/next-folder.go
index f44abdc1..c16a7717 100644
--- a/commands/account/next-folder.go
+++ b/commands/account/next-folder.go
@@ -4,6 +4,7 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type NextPrevFolder struct {
@@ -11,7 +12,11 @@ type NextPrevFolder struct {
}
func init() {
- register(NextPrevFolder{})
+ commands.Register(NextPrevFolder{})
+}
+
+func (NextPrevFolder) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (NextPrevFolder) Aliases() []string {
diff --git a/commands/account/next-result.go b/commands/account/next-result.go
index d624e559..f36ba941 100644
--- a/commands/account/next-result.go
+++ b/commands/account/next-result.go
@@ -4,13 +4,18 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/ui"
)
type NextPrevResult struct{}
func init() {
- register(NextPrevResult{})
+ commands.Register(NextPrevResult{})
+}
+
+func (NextPrevResult) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (NextPrevResult) Aliases() []string {
diff --git a/commands/account/next.go b/commands/account/next.go
index 142f6151..262e308d 100644
--- a/commands/account/next.go
+++ b/commands/account/next.go
@@ -2,11 +2,16 @@ package account
import (
"errors"
+ "fmt"
"strconv"
"strings"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
+ "git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/ui"
+ "git.sr.ht/~rjarry/aerc/models"
+ "git.sr.ht/~rjarry/aerc/worker/types"
)
type NextPrevMsg struct {
@@ -15,7 +20,11 @@ type NextPrevMsg struct {
}
func init() {
- register(NextPrevMsg{})
+ commands.Register(NextPrevMsg{})
+}
+
+func (NextPrevMsg) Context() commands.CommandContext {
+ return commands.ACCOUNT | commands.MESSAGE_VIEWER
}
func (np *NextPrevMsg) ParseAmount(arg string) error {
@@ -40,23 +49,48 @@ func (np NextPrevMsg) Execute(args []string) error {
if acct == nil {
return errors.New("No account selected")
}
+ store := acct.Store()
+ if store == nil {
+ return fmt.Errorf("No message store set.")
+ }
n := np.Amount
if np.Percent {
n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
}
if args[0] == "prev-message" || args[0] == "prev" {
- store := acct.Store()
- if store != nil {
- store.NextPrev(-n)
- ui.Invalidate()
- }
+ store.NextPrev(-n)
} else {
- store := acct.Store()
- if store != nil {
- store.NextPrev(n)
- ui.Invalidate()
+ store.NextPrev(n)
+ }
+
+ if mv, ok := app.SelectedTabContent().(*app.MessageViewer); ok {
+ reloadViewer := func(nextMsg *models.MessageInfo) {
+ lib.NewMessageStoreView(nextMsg, mv.MessageView().SeenFlagSet(),
+ store, app.CryptoProvider(), app.DecryptKeys,
+ func(view lib.MessageView, err error) {
+ if err != nil {
+ app.PushError(err.Error())
+ return
+ }
+ nextMv := app.NewMessageViewer(acct, view)
+ app.ReplaceTab(mv, nextMv,
+ nextMsg.Envelope.Subject, true)
+ })
+ }
+ if nextMsg := store.Selected(); nextMsg != nil {
+ reloadViewer(nextMsg)
+ } else {
+ store.FetchHeaders([]uint32{store.SelectedUid()},
+ func(msg types.WorkerMessage) {
+ if m, ok := msg.(*types.MessageInfo); ok {
+ reloadViewer(m.Info)
+ }
+ })
}
}
+
+ ui.Invalidate()
+
return nil
}
diff --git a/commands/account/recover.go b/commands/account/recover.go
index a2170edd..f38d9920 100644
--- a/commands/account/recover.go
+++ b/commands/account/recover.go
@@ -20,7 +20,11 @@ type Recover struct {
}
func init() {
- register(Recover{})
+ commands.Register(Recover{})
+}
+
+func (Recover) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (Recover) Aliases() []string {
diff --git a/commands/account/rmdir.go b/commands/account/rmdir.go
index 8bf4d6b2..00366bd0 100644
--- a/commands/account/rmdir.go
+++ b/commands/account/rmdir.go
@@ -5,6 +5,7 @@ import (
"time"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
@@ -14,7 +15,11 @@ type RemoveDir struct {
}
func init() {
- register(RemoveDir{})
+ commands.Register(RemoveDir{})
+}
+
+func (RemoveDir) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (RemoveDir) Aliases() []string {
diff --git a/commands/account/search.go b/commands/account/search.go
index 10481e8e..be3b125f 100644
--- a/commands/account/search.go
+++ b/commands/account/search.go
@@ -34,7 +34,11 @@ type SearchFilter struct {
}
func init() {
- register(SearchFilter{})
+ commands.Register(SearchFilter{})
+}
+
+func (SearchFilter) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (SearchFilter) Aliases() []string {
diff --git a/commands/account/select.go b/commands/account/select.go
index 884b4bce..cbaaf6e9 100644
--- a/commands/account/select.go
+++ b/commands/account/select.go
@@ -4,6 +4,7 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type SelectMessage struct {
@@ -11,7 +12,11 @@ type SelectMessage struct {
}
func init() {
- register(SelectMessage{})
+ commands.Register(SelectMessage{})
+}
+
+func (SelectMessage) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (SelectMessage) Aliases() []string {
diff --git a/commands/account/sort.go b/commands/account/sort.go
index 3103a388..0e80aec8 100644
--- a/commands/account/sort.go
+++ b/commands/account/sort.go
@@ -18,7 +18,11 @@ type Sort struct {
}
func init() {
- register(Sort{})
+ commands.Register(Sort{})
+}
+
+func (Sort) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (Sort) Aliases() []string {
diff --git a/commands/account/split.go b/commands/account/split.go
index 8690d99a..4b01da2d 100644
--- a/commands/account/split.go
+++ b/commands/account/split.go
@@ -6,6 +6,7 @@ import (
"strings"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type Split struct {
@@ -14,7 +15,11 @@ type Split struct {
}
func init() {
- register(Split{})
+ commands.Register(Split{})
+}
+
+func (Split) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (s *Split) ParseSize(arg string) error {
diff --git a/commands/account/view.go b/commands/account/view.go
index 87705a1a..4177b906 100644
--- a/commands/account/view.go
+++ b/commands/account/view.go
@@ -5,6 +5,7 @@ import (
"errors"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/state"
"git.sr.ht/~rjarry/aerc/lib/templates"
@@ -16,7 +17,11 @@ type ViewMessage struct {
}
func init() {
- register(ViewMessage{})
+ commands.Register(ViewMessage{})
+}
+
+func (ViewMessage) Context() commands.CommandContext {
+ return commands.ACCOUNT
}
func (ViewMessage) Aliases() []string {