2019-06-01 22:15:04 -07:00
|
|
|
package msg
|
2019-05-14 16:34:42 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-09-20 17:16:29 +01:00
|
|
|
"strings"
|
2020-05-28 10:32:32 -04:00
|
|
|
"time"
|
2019-05-14 16:34:42 -04:00
|
|
|
|
2019-06-08 19:41:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-05-14 16:34:42 -04:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-05-14 16:34:42 -04:00
|
|
|
)
|
|
|
|
|
2019-06-27 10:33:11 -07:00
|
|
|
type Copy struct{}
|
|
|
|
|
2019-05-14 16:34:42 -04:00
|
|
|
func init() {
|
2019-06-27 10:33:11 -07:00
|
|
|
register(Copy{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:34:03 -03:00
|
|
|
func (Copy) Aliases() []string {
|
2019-07-17 18:42:03 -04:00
|
|
|
return []string{"cp", "copy"}
|
2019-06-27 10:33:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:34:03 -03:00
|
|
|
func (Copy) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-09-12 12:01:16 +01:00
|
|
|
return commands.GetFolders(aerc, args)
|
2019-05-14 16:34:42 -04:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:34:03 -03:00
|
|
|
func (Copy) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-09-20 17:16:29 +01:00
|
|
|
if len(args) == 1 {
|
|
|
|
return errors.New("Usage: cp [-p] <folder>")
|
|
|
|
}
|
2019-06-09 19:14:04 -04:00
|
|
|
opts, optind, err := getopt.Getopts(args, "p")
|
2019-06-08 19:41:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-14 16:34:42 -04:00
|
|
|
}
|
2019-06-08 19:41:56 +02:00
|
|
|
var (
|
|
|
|
createParents bool
|
|
|
|
)
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'p':
|
|
|
|
createParents = true
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 06:34:03 +01:00
|
|
|
h := newHelper(aerc)
|
2020-05-09 11:50:30 +02:00
|
|
|
uids, err := h.markedOrSelectedUids()
|
2019-12-18 06:34:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 00:42:24 -07:00
|
|
|
}
|
2019-12-18 06:34:03 +01:00
|
|
|
store, err := h.store()
|
2019-07-09 17:04:21 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 06:34:03 +01:00
|
|
|
store.Copy(uids, strings.Join(args[optind:], " "),
|
2019-09-20 17:16:29 +01:00
|
|
|
createParents, func(
|
|
|
|
msg types.WorkerMessage) {
|
2019-06-08 19:41:56 +02:00
|
|
|
|
2019-09-20 17:16:29 +01:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
2020-05-28 10:32:32 -04:00
|
|
|
aerc.PushStatus("Messages copied.", 10*time.Second)
|
2022-06-10 17:24:12 -05:00
|
|
|
store.ClearVisualMark()
|
2019-09-20 17:16:29 +01:00
|
|
|
case *types.Error:
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(msg.Error.Error())
|
2019-09-20 17:16:29 +01:00
|
|
|
}
|
|
|
|
})
|
2019-05-14 16:34:42 -04:00
|
|
|
return nil
|
|
|
|
}
|