Implement :copy (aka :cp)

This commit is contained in:
Drew DeVault 2019-05-14 16:34:42 -04:00
parent b0b3287bbd
commit db213fd0ae
7 changed files with 101 additions and 3 deletions

38
commands/account/copy.go Normal file
View File

@ -0,0 +1,38 @@
package account
import (
"errors"
"time"
"github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc2/widgets"
"git.sr.ht/~sircmpwn/aerc2/worker/types"
)
func init() {
register("cp", Copy)
register("copy", Copy)
}
func Copy(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 {
return errors.New("Usage: mv <folder>")
}
acct := aerc.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
msg := acct.Messages().Selected()
store := acct.Messages().Store()
store.Copy([]uint32{msg.Uid}, args[1], func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages copied.", 10*time.Second)
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
}
})
return nil
}

View File

@ -2,8 +2,12 @@ package account
import (
"errors"
"time"
"github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc2/widgets"
"git.sr.ht/~sircmpwn/aerc2/worker/types"
)
func init() {
@ -20,6 +24,14 @@ func DeleteMessage(aerc *widgets.Aerc, args []string) error {
}
store := acct.Messages().Store()
msg := acct.Messages().Selected()
store.Delete([]uint32{msg.Uid})
store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages deleted.", 10*time.Second)
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
}
})
return nil
}

View File

@ -218,7 +218,8 @@ func (store *MessageStore) update() {
}
}
func (store *MessageStore) Delete(uids []uint32) {
func (store *MessageStore) Delete(uids []uint32,
cb func(msg types.WorkerMessage)) {
store.Lock()
var set imap.SeqSet
@ -229,6 +230,19 @@ func (store *MessageStore) Delete(uids []uint32) {
store.Unlock()
store.worker.PostAction(&types.DeleteMessages{Uids: set}, nil)
store.worker.PostAction(&types.DeleteMessages{Uids: set}, cb)
store.update()
}
func (store *MessageStore) Copy(uids []uint32, dest string,
cb func(msg types.WorkerMessage)) {
var set imap.SeqSet
for _, uid := range uids {
set.AddNum(uid)
}
store.worker.PostAction(&types.CopyMessages{
Destination: dest,
Uids: set,
}, cb)
}

View File

@ -110,6 +110,10 @@ func (acct *AccountView) Focus(focus bool) {
// TODO: Unfocus children I guess
}
func (acct *AccountView) Worker() *types.Worker {
return acct.worker
}
func (acct *AccountView) connected(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:

16
worker/imap/movecopy.go Normal file
View File

@ -0,0 +1,16 @@
package imap
import (
"git.sr.ht/~sircmpwn/aerc2/worker/types"
)
func (imapw *IMAPWorker) handleCopyMessages(msg *types.CopyMessages) {
if err := imapw.client.UidCopy(&msg.Uids, msg.Destination); err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
} else {
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
}
}

View File

@ -174,6 +174,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
w.handleFetchFullMessages(msg)
case *types.DeleteMessages:
w.handleDeleteMessages(msg)
case *types.CopyMessages:
w.handleCopyMessages(msg)
default:
return errUnsupported
}

View File

@ -97,6 +97,18 @@ type DeleteMessages struct {
Uids imap.SeqSet
}
type CopyMessages struct {
Message
Destination string
Uids imap.SeqSet
}
type MoveMessages struct {
Message
Destination string
Uids imap.SeqSet
}
// Messages
type CertificateApprovalRequest struct {