aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msg/move.go
blob: 5ef9390af12462d9a5ea2d82cae1e63c613447a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package msg

import (
	"errors"
	"strings"
	"time"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/config"
	"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"
	"git.sr.ht/~sircmpwn/getopt"
)

type Move struct{}

func init() {
	register(Move{})
}

func (Move) Aliases() []string {
	return []string{"mv", "move"}
}

func (Move) Complete(args []string) []string {
	return commands.GetFolders(args)
}

func (Move) Execute(args []string) error {
	if len(args) == 1 {
		return errors.New("Usage: mv [-p] <folder>")
	}
	opts, optind, err := getopt.Getopts(args, "p")
	if err != nil {
		return err
	}
	var createParents bool
	for _, opt := range opts {
		if opt.Option == 'p' {
			createParents = true
		}
	}

	h := newHelper()
	acct, err := h.account()
	if err != nil {
		return err
	}
	store, err := h.store()
	if err != nil {
		return err
	}
	msgs, err := h.messages()
	if err != nil {
		return err
	}
	var uids []uint32
	for _, msg := range msgs {
		uids = append(uids, msg.Uid)
	}
	marker := store.Marker()
	marker.ClearVisualMark()
	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:
			handleDone(acct, next, "Messages moved to "+joinedArgs, store)
		case *types.Error:
			app.PushError(msg.Error.Error())
			marker.Remark()
		}
	})

	return nil
}

func handleDone(
	acct *app.AccountView,
	next *models.MessageInfo,
	message string,
	store *lib.MessageStore,
) {
	h := newHelper()
	app.PushStatus(message, 10*time.Second)
	mv, isMsgView := h.msgProvider.(*app.MessageViewer)
	switch {
	case isMsgView && !config.Ui.NextMessageOnDelete:
		app.RemoveTab(h.msgProvider, true)
	case isMsgView:
		if next == nil {
			app.RemoveTab(h.msgProvider, true)
			acct.Messages().Select(-1)
			ui.Invalidate()
			return
		}
		lib.NewMessageStoreView(next, 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, next.Envelope.Subject, true)
			})
	default:
		if next == nil {
			// We moved the last message, select the new last message
			// instead of the first message
			acct.Messages().Select(-1)
		}
	}
}