aerc/commands/msg/delete.go

104 lines
2.1 KiB
Go
Raw Normal View History

package msg
2019-03-21 03:23:38 +00:00
import (
"errors"
2019-05-14 20:34:42 +00:00
"time"
2019-12-18 05:34:04 +00:00
"git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/models"
2019-05-18 00:57:10 +00:00
"git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/aerc/worker/types"
2019-03-21 03:23:38 +00:00
)
type Delete struct{}
2019-03-21 03:23:38 +00:00
func init() {
register(Delete{})
}
func (Delete) Aliases() []string {
return []string{"delete", "delete-message"}
}
func (Delete) Complete(aerc *widgets.Aerc, args []string) []string {
return nil
2019-03-21 03:23:38 +00:00
}
func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
2019-03-21 03:23:38 +00:00
if len(args) != 1 {
return errors.New("Usage: :delete")
2019-03-21 03:23:38 +00:00
}
2019-12-18 05:34:04 +00:00
h := newHelper(aerc)
store, err := h.store()
if err != nil {
return err
2019-03-21 03:23:38 +00:00
}
uids, err := h.markedOrSelectedUids()
2019-12-18 05:34:04 +00:00
if err != nil {
return err
}
2019-12-18 05:34:04 +00:00
acct, err := h.account()
if err != nil {
return err
2019-07-03 01:46:05 +00:00
}
2019-12-18 05:34:04 +00:00
store.Delete(uids, func(msg types.WorkerMessage) {
2019-05-14 20:34:42 +00:00
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages deleted.", 10*time.Second)
case *types.Error:
2020-04-24 16:59:40 +00:00
aerc.PushError(" " + msg.Error.Error())
2019-05-14 20:34:42 +00:00
}
})
2019-12-18 05:34:04 +00:00
//caution, can be nil
next := findNextNonDeleted(uids, store)
mv, isMsgView := h.msgProvider.(*widgets.MessageViewer)
if isMsgView {
if !aerc.Config().Ui.NextMessageOnDelete {
aerc.RemoveTab(h.msgProvider)
} else {
// no more messages in the list
if next == nil {
aerc.RemoveTab(h.msgProvider)
acct.Messages().Scroll()
return nil
}
lib.NewMessageStoreView(next, store, aerc.DecryptKeys,
func(view lib.MessageView) {
nextMv := widgets.NewMessageViewer(acct, aerc.Config(), view)
aerc.ReplaceTab(mv, nextMv, next.Envelope.Subject)
})
2019-12-18 05:34:04 +00:00
}
}
acct.Messages().Scroll()
2019-03-21 03:23:38 +00:00
return nil
}
2019-12-18 05:34:04 +00:00
func findNextNonDeleted(deleted []uint32, store *lib.MessageStore) *models.MessageInfo {
selected := store.Selected()
if !contains(deleted, selected.Uid) {
return selected
}
for {
store.Next()
next := store.Selected()
if next == selected || next == nil {
// the last message is in the deleted state or doesn't exist
return nil
}
return next
}
return nil // Never reached
}
func contains(uids []uint32, uid uint32) bool {
for _, item := range uids {
if item == uid {
return true
}
}
return false
}