2019-06-02 07:15:04 +02:00
|
|
|
package msg
|
2019-03-21 04:23:38 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-05-14 22:34:42 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
2019-03-21 04:23:38 +01:00
|
|
|
|
2019-12-18 06:34:04 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
2019-03-21 04:23:38 +01:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Delete struct{}
|
|
|
|
|
2019-03-21 04:23:38 +01:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Delete{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"delete", "delete-message"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-03-21 04:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-03-21 04:23:38 +01:00
|
|
|
if len(args) != 1 {
|
2019-05-20 00:23:34 +02:00
|
|
|
return errors.New("Usage: :delete")
|
2019-03-21 04:23:38 +01:00
|
|
|
}
|
2019-06-02 07:15:04 +02:00
|
|
|
|
2019-12-18 06:34:04 +01:00
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-21 04:23:38 +01:00
|
|
|
}
|
2019-12-18 06:34:04 +01:00
|
|
|
uids, err := h.uids()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 09:42:24 +02:00
|
|
|
}
|
2019-12-18 06:34:04 +01:00
|
|
|
acct, err := h.account()
|
2019-07-10 02:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-03 03:46:05 +02:00
|
|
|
}
|
2019-12-18 06:34:04 +01:00
|
|
|
store.Delete(uids, func(msg types.WorkerMessage) {
|
2019-05-14 22:34:42 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2019-12-18 06:34:04 +01: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
|
|
|
|
}
|
|
|
|
nextMv := widgets.NewMessageViewer(acct, aerc.Config(), store, next)
|
|
|
|
aerc.ReplaceTab(mv, nextMv, next.Envelope.Subject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acct.Messages().Scroll()
|
2019-03-21 04:23:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-18 06:34:04 +01: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
|
|
|
|
}
|