aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msgview
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/msgview
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/msgview')
-rw-r--r--commands/msgview/close.go21
-rw-r--r--commands/msgview/msgview.go14
-rw-r--r--commands/msgview/next-part.go7
-rw-r--r--commands/msgview/next.go83
-rw-r--r--commands/msgview/open-link.go6
-rw-r--r--commands/msgview/open.go7
-rw-r--r--commands/msgview/save.go6
-rw-r--r--commands/msgview/toggle-headers.go7
-rw-r--r--commands/msgview/toggle-key-passthrough.go7
9 files changed, 34 insertions, 124 deletions
diff --git a/commands/msgview/close.go b/commands/msgview/close.go
deleted file mode 100644
index 95960ef3..00000000
--- a/commands/msgview/close.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package msgview
-
-import (
- "git.sr.ht/~rjarry/aerc/app"
-)
-
-type Close struct{}
-
-func init() {
- register(Close{})
-}
-
-func (Close) Aliases() []string {
- return []string{"close"}
-}
-
-func (Close) Execute(args []string) error {
- mv, _ := app.SelectedTabContent().(*app.MessageViewer)
- app.RemoveTab(mv, true)
- return nil
-}
diff --git a/commands/msgview/msgview.go b/commands/msgview/msgview.go
deleted file mode 100644
index fb32d8e1..00000000
--- a/commands/msgview/msgview.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package msgview
-
-import (
- "git.sr.ht/~rjarry/aerc/commands"
-)
-
-var MessageViewCommands *commands.Commands
-
-func register(cmd commands.Command) {
- if MessageViewCommands == nil {
- MessageViewCommands = commands.NewCommands()
- }
- MessageViewCommands.Register(cmd)
-}
diff --git a/commands/msgview/next-part.go b/commands/msgview/next-part.go
index 77eb008d..b5375d75 100644
--- a/commands/msgview/next-part.go
+++ b/commands/msgview/next-part.go
@@ -2,6 +2,7 @@ package msgview
import (
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type NextPrevPart struct {
@@ -9,7 +10,11 @@ type NextPrevPart struct {
}
func init() {
- register(NextPrevPart{})
+ commands.Register(NextPrevPart{})
+}
+
+func (NextPrevPart) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (NextPrevPart) Aliases() []string {
diff --git a/commands/msgview/next.go b/commands/msgview/next.go
deleted file mode 100644
index c953cd5d..00000000
--- a/commands/msgview/next.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package msgview
-
-import (
- "errors"
- "fmt"
- "strconv"
- "strings"
-
- "git.sr.ht/~rjarry/aerc/app"
- "git.sr.ht/~rjarry/aerc/commands/account"
- "git.sr.ht/~rjarry/aerc/lib"
- "git.sr.ht/~rjarry/aerc/models"
- "git.sr.ht/~rjarry/aerc/worker/types"
-)
-
-type NextPrevMsg struct {
- Amount int `opt:"n" default:"1" metavar:"N[%]" action:"ParseAmount"`
- Percent bool
-}
-
-func init() {
- register(NextPrevMsg{})
-}
-
-func (np *NextPrevMsg) ParseAmount(arg string) error {
- if strings.HasSuffix(arg, "%") {
- np.Percent = true
- arg = strings.TrimSuffix(arg, "%")
- }
- i, err := strconv.ParseInt(arg, 10, 64)
- if err != nil {
- return err
- }
- np.Amount = int(i)
- return nil
-}
-
-func (NextPrevMsg) Aliases() []string {
- return []string{"next", "next-message", "prev", "prev-message"}
-}
-
-func (np NextPrevMsg) Execute(args []string) error {
- cmd := account.NextPrevMsg{Amount: np.Amount, Percent: np.Percent}
- err := cmd.Execute(args)
- if err != nil {
- return err
- }
-
- mv, _ := app.SelectedTabContent().(*app.MessageViewer)
- acct := mv.SelectedAccount()
- if acct == nil {
- return errors.New("No account selected")
- }
- store := mv.Store()
- if store == nil {
- return fmt.Errorf("Cannot perform action. No message store set.")
- }
- executeNextPrev := 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 {
- executeNextPrev(nextMsg)
- } else {
- store.FetchHeaders([]uint32{store.SelectedUid()},
- func(msg types.WorkerMessage) {
- if m, ok := msg.(*types.MessageInfo); ok {
- executeNextPrev(m.Info)
- }
- })
- }
-
- return nil
-}
diff --git a/commands/msgview/open-link.go b/commands/msgview/open-link.go
index b13f5d4f..e64e6e2f 100644
--- a/commands/msgview/open-link.go
+++ b/commands/msgview/open-link.go
@@ -16,7 +16,11 @@ type OpenLink struct {
}
func init() {
- register(OpenLink{})
+ commands.Register(OpenLink{})
+}
+
+func (OpenLink) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (OpenLink) Aliases() []string {
diff --git a/commands/msgview/open.go b/commands/msgview/open.go
index a8443404..3943b21a 100644
--- a/commands/msgview/open.go
+++ b/commands/msgview/open.go
@@ -8,6 +8,7 @@ import (
"path/filepath"
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/log"
)
@@ -18,7 +19,11 @@ type Open struct {
}
func init() {
- register(Open{})
+ commands.Register(Open{})
+}
+
+func (Open) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (Open) Options() string {
diff --git a/commands/msgview/save.go b/commands/msgview/save.go
index bcaf9eb2..d9320293 100644
--- a/commands/msgview/save.go
+++ b/commands/msgview/save.go
@@ -26,7 +26,11 @@ type Save struct {
}
func init() {
- register(Save{})
+ commands.Register(Save{})
+}
+
+func (Save) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (Save) Options() string {
diff --git a/commands/msgview/toggle-headers.go b/commands/msgview/toggle-headers.go
index 37f5b22f..201fce90 100644
--- a/commands/msgview/toggle-headers.go
+++ b/commands/msgview/toggle-headers.go
@@ -2,12 +2,17 @@ package msgview
import (
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
)
type ToggleHeaders struct{}
func init() {
- register(ToggleHeaders{})
+ commands.Register(ToggleHeaders{})
+}
+
+func (ToggleHeaders) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (ToggleHeaders) Aliases() []string {
diff --git a/commands/msgview/toggle-key-passthrough.go b/commands/msgview/toggle-key-passthrough.go
index b4c5f2c7..1b3f8628 100644
--- a/commands/msgview/toggle-key-passthrough.go
+++ b/commands/msgview/toggle-key-passthrough.go
@@ -2,13 +2,18 @@ package msgview
import (
"git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/state"
)
type ToggleKeyPassthrough struct{}
func init() {
- register(ToggleKeyPassthrough{})
+ commands.Register(ToggleKeyPassthrough{})
+}
+
+func (ToggleKeyPassthrough) Context() commands.CommandContext {
+ return commands.MESSAGE_VIEWER
}
func (ToggleKeyPassthrough) Aliases() []string {