add command helpers
This commit is contained in:
parent
cf43066d5b
commit
881a9081a8
|
@ -0,0 +1,55 @@
|
|||
package msg
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.sr.ht/~sircmpwn/aerc/commands"
|
||||
"git.sr.ht/~sircmpwn/aerc/lib"
|
||||
"git.sr.ht/~sircmpwn/aerc/models"
|
||||
"git.sr.ht/~sircmpwn/aerc/widgets"
|
||||
)
|
||||
|
||||
type helper struct {
|
||||
msgProvider widgets.ProvidesMessages
|
||||
}
|
||||
|
||||
func newHelper(aerc *widgets.Aerc) *helper {
|
||||
return &helper{aerc.SelectedTab().(widgets.ProvidesMessages)}
|
||||
}
|
||||
|
||||
func (h *helper) uids() ([]uint32, error) {
|
||||
msgs, err := commands.MarkedOrSelected(h.msgProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uids := commands.UidsFromMessageInfos(msgs)
|
||||
return uids, nil
|
||||
}
|
||||
|
||||
func (h *helper) store() (*lib.MessageStore, error) {
|
||||
store := h.msgProvider.Store()
|
||||
if store == nil {
|
||||
return nil, errors.New("Cannot perform action. Messages still loading")
|
||||
}
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func (h *helper) account() (*widgets.AccountView, error) {
|
||||
acct := h.msgProvider.SelectedAccount()
|
||||
if acct == nil {
|
||||
return nil, errors.New("No account selected")
|
||||
}
|
||||
return acct, nil
|
||||
}
|
||||
|
||||
func (h *helper) messages() ([]*models.MessageInfo, error) {
|
||||
return commands.MarkedOrSelected(h.msgProvider)
|
||||
}
|
||||
|
||||
func (h *helper) messageUids() ([]uint32, error) {
|
||||
msgs, err := h.messages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return commands.UidsFromMessageInfos(msgs), nil
|
||||
}
|
|
@ -10,6 +10,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"git.sr.ht/~sircmpwn/aerc/models"
|
||||
"git.sr.ht/~sircmpwn/aerc/widgets"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
|
@ -148,3 +149,32 @@ func listDir(path string, hidden bool) []string {
|
|||
|
||||
return filtered
|
||||
}
|
||||
|
||||
// MarkedOrSelected returns either all marked messages if any are marked or the
|
||||
// selected message instead
|
||||
func MarkedOrSelected(pm widgets.ProvidesMessages) ([]*models.MessageInfo, error) {
|
||||
// marked has priority over the selected message
|
||||
marked, err := pm.MarkedMessages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(marked) > 0 {
|
||||
return marked, nil
|
||||
}
|
||||
msg, err := pm.SelectedMessage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []*models.MessageInfo{msg}, nil
|
||||
}
|
||||
|
||||
// UidsFromMessageInfos extracts a uid slice from a slice of MessageInfos
|
||||
func UidsFromMessageInfos(msgs []*models.MessageInfo) []uint32 {
|
||||
uids := make([]uint32, len(msgs))
|
||||
i := 0
|
||||
for _, msg := range msgs {
|
||||
uids[i] = msg.Uid
|
||||
i++
|
||||
}
|
||||
return uids
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue