diff options
author | Bence Ferdinandy <bence@ferdinandy.com> | 2022-11-12 11:18:11 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-13 17:14:04 +0100 |
commit | 5dfc1ff5df71a62d9e114a84ac036cf4c2cb8540 (patch) | |
tree | 967397513eef3a8241274c1ad78a2db071ce40b1 /commands/msg/move.go | |
parent | 1352942056940afb58d4e25d8094c08251fd54de (diff) | |
download | aerc-5dfc1ff5df71a62d9e114a84ac036cf4c2cb8540.tar.gz |
move and archive: common code for common operation
The code of Archive and Move are slightly different, even though they
essentially do the same thing sans the destination and archive always
creating the destination if it doesn't exist. Extract common code into
a function used by both. This will also result in Move now also
respecting next-message-on-delete.
Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
Acked-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/msg/move.go')
-rw-r--r-- | commands/msg/move.go | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/commands/msg/move.go b/commands/msg/move.go index 13b1fb7b..15494364 100644 --- a/commands/msg/move.go +++ b/commands/msg/move.go @@ -5,11 +5,13 @@ import ( "strings" "time" - "git.sr.ht/~sircmpwn/getopt" - "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/widgets" "git.sr.ht/~rjarry/aerc/worker/types" + "git.sr.ht/~sircmpwn/getopt" ) type Move struct{} @@ -42,32 +44,77 @@ func (Move) Execute(aerc *widgets.Aerc, args []string) error { } h := newHelper(aerc) + acct, err := h.account() + if err != nil { + return err + } store, err := h.store() if err != nil { return err } - uids, err := h.markedOrSelectedUids() + msgs, err := h.messages() if err != nil { return err } - _, isMsgView := h.msgProvider.(*widgets.MessageViewer) - if isMsgView { - aerc.RemoveTab(h.msgProvider) + var uids []uint32 + for _, msg := range msgs { + uids = append(uids, msg.Uid) } marker := store.Marker() marker.ClearVisualMark() - findNextNonDeleted(uids, store) + next := findNextNonDeleted(uids, store) joinedArgs := strings.Join(args[optind:], " ") + store.Move(uids, joinedArgs, createParents, func( msg types.WorkerMessage, ) { switch msg := msg.(type) { case *types.Done: - aerc.PushStatus("Message moved to "+joinedArgs, 10*time.Second) + handleDone(aerc, acct, next, "Messages moved to "+joinedArgs, store) case *types.Error: - marker.Remark() aerc.PushError(msg.Error.Error()) + marker.Remark() } }) + return nil } + +func handleDone( + aerc *widgets.Aerc, + acct *widgets.AccountView, + next *models.MessageInfo, + message string, + store *lib.MessageStore, +) { + h := newHelper(aerc) + aerc.PushStatus(message, 10*time.Second) + mv, isMsgView := h.msgProvider.(*widgets.MessageViewer) + switch { + case isMsgView && !aerc.Config().Ui.NextMessageOnDelete: + aerc.RemoveTab(h.msgProvider) + case isMsgView: + if next == nil { + aerc.RemoveTab(h.msgProvider) + acct.Messages().Select(-1) + ui.Invalidate() + return + } + lib.NewMessageStoreView(next, mv.MessageView().SeenFlagSet(), + store, aerc.Crypto, aerc.DecryptKeys, + func(view lib.MessageView, err error) { + if err != nil { + aerc.PushError(err.Error()) + return + } + nextMv := widgets.NewMessageViewer(acct, aerc.Config(), view) + aerc.ReplaceTab(mv, nextMv, next.Envelope.Subject) + }) + default: + if next == nil { + // We moved the last message, select the new last message + // instead of the first message + acct.Messages().Select(-1) + } + } +} |