aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msg/copy.go
blob: 3620238ac3487303d22e8755f73dd22418f83e4c (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
package msg

import (
	"fmt"
	"time"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/worker/types"
)

type Copy struct {
	CreateFolders bool   `opt:"-p"`
	Folder        string `opt:"folder" complete:"CompleteFolder"`
}

func init() {
	commands.Register(Copy{})
}

func (Copy) Context() commands.CommandContext {
	return commands.MESSAGE
}

func (Copy) Aliases() []string {
	return []string{"cp", "copy"}
}

func (*Copy) CompleteFolder(arg string) []string {
	return commands.GetFolders(arg)
}

func (c Copy) Execute(args []string) error {
	h := newHelper()
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
	store, err := h.store()
	if err != nil {
		return err
	}
	store.Copy(uids, c.Folder,
		c.CreateFolders, func(
			msg types.WorkerMessage,
		) {
			switch msg := msg.(type) {
			case *types.Done:
				var s string
				if len(uids) > 1 {
					s = "%d messages copied to %s"
				} else {
					s = "%d message copied to %s"
				}
				app.PushStatus(fmt.Sprintf(s, len(uids), c.Folder), 10*time.Second)
				store.Marker().ClearVisualMark()
			case *types.Error:
				app.PushError(msg.Error.Error())
			}
		})
	return nil
}