~sircmpwn/aerc

commands/helper: remove duplicated method v1 PROPOSED

Reto Brunner: 2
 commands/helper: remove duplicated method
 Change MarkedMessages to return uids

 11 files changed, 36 insertions(+), 28 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/10535/mbox | git am -3
Learn more about email & git

[PATCH 1/2] commands/helper: remove duplicated method Export this patch

---
 commands/msg/copy.go          |  2 +-
 commands/msg/delete.go        |  2 +-
 commands/msg/modify-labels.go |  2 +-
 commands/msg/move.go          |  2 +-
 commands/msg/read.go          |  2 +-
 commands/msg/utils.go         | 10 +---------
 6 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/commands/msg/copy.go b/commands/msg/copy.go
index 3eca565..f3d4030 100644
--- a/commands/msg/copy.go
+++ b/commands/msg/copy.go
@@ -44,7 +44,7 @@ func (Copy) Execute(aerc *widgets.Aerc, args []string) error {
		}
	}
	h := newHelper(aerc)
	uids, err := h.uids()
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
diff --git a/commands/msg/delete.go b/commands/msg/delete.go
index 0d2c1c5..313e3ea 100644
--- a/commands/msg/delete.go
+++ b/commands/msg/delete.go
@@ -34,7 +34,7 @@ func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
	if err != nil {
		return err
	}
	uids, err := h.uids()
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
diff --git a/commands/msg/modify-labels.go b/commands/msg/modify-labels.go
index 92899ec..f91075a 100644
--- a/commands/msg/modify-labels.go
+++ b/commands/msg/modify-labels.go
@@ -34,7 +34,7 @@ func (ModifyLabels) Execute(aerc *widgets.Aerc, args []string) error {
	if err != nil {
		return err
	}
	uids, err := h.uids()
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
diff --git a/commands/msg/move.go b/commands/msg/move.go
index 9542613..830e752 100644
--- a/commands/msg/move.go
+++ b/commands/msg/move.go
@@ -49,7 +49,7 @@ func (Move) Execute(aerc *widgets.Aerc, args []string) error {
	if err != nil {
		return err
	}
	uids, err := h.uids()
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
diff --git a/commands/msg/read.go b/commands/msg/read.go
index 8b0c7b1..e49bb5e 100644
--- a/commands/msg/read.go
+++ b/commands/msg/read.go
@@ -54,7 +54,7 @@ func (Read) Execute(aerc *widgets.Aerc, args []string) error {
		// ignore commmand given, simply toggle all the read states
		return submitToggle(aerc, store, h)
	}
	msgUids, err := h.messageUids()
	msgUids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
diff --git a/commands/msg/utils.go b/commands/msg/utils.go
index 30a4394..ae25535 100644
--- a/commands/msg/utils.go
+++ b/commands/msg/utils.go
@@ -17,7 +17,7 @@ func newHelper(aerc *widgets.Aerc) *helper {
	return &helper{aerc.SelectedTab().(widgets.ProvidesMessages)}
}

func (h *helper) uids() ([]uint32, error) {
func (h *helper) markedOrSelectedUids() ([]uint32, error) {
	msgs, err := commands.MarkedOrSelected(h.msgProvider)
	if err != nil {
		return nil, err
@@ -45,11 +45,3 @@ func (h *helper) account() (*widgets.AccountView, error) {
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
}
-- 
2.26.2

[PATCH 2/2] Change MarkedMessages to return uids Export this patch

Especially if one tries to interact with all marked messages there could be
the case that not all headers are fetched yet, hence the messageInfo is still nil.

This segfaults a lot of commands which in principle only need the uid to complete.

If we switch to uids, this issue can be alleviated for those commands.
---
 commands/msg/utils.go      | 17 ++++++++++-------
 commands/util.go           | 17 +++++++++++++++--
 widgets/account.go         |  4 ++--
 widgets/msgviewer.go       |  4 ++--
 widgets/providesmessage.go |  2 +-
 5 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/commands/msg/utils.go b/commands/msg/utils.go
index ae25535..cad0f82 100644
--- a/commands/msg/utils.go
+++ b/commands/msg/utils.go
@@ -18,12 +18,7 @@ func newHelper(aerc *widgets.Aerc) *helper {
}

func (h *helper) markedOrSelectedUids() ([]uint32, error) {
	msgs, err := commands.MarkedOrSelected(h.msgProvider)
	if err != nil {
		return nil, err
	}
	uids := commands.UidsFromMessageInfos(msgs)
	return uids, nil
	return commands.MarkedOrSelected(h.msgProvider)
}

func (h *helper) store() (*lib.MessageStore, error) {
@@ -43,5 +38,13 @@ func (h *helper) account() (*widgets.AccountView, error) {
}

func (h *helper) messages() ([]*models.MessageInfo, error) {
	return commands.MarkedOrSelected(h.msgProvider)
	uid, err := commands.MarkedOrSelected(h.msgProvider)
	if err != nil {
		return nil, err
	}
	store, err := h.store()
	if err != nil {
		return nil, err
	}
	return commands.MsgInfoFromUids(store, uid)
}
diff --git a/commands/util.go b/commands/util.go
index 5529edb..e3395fd 100644
--- a/commands/util.go
+++ b/commands/util.go
@@ -10,6 +10,7 @@ import (
	"strings"
	"time"

	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"github.com/gdamore/tcell"
@@ -152,7 +153,7 @@ func listDir(path string, hidden bool) []string {

// MarkedOrSelected returns either all marked messages if any are marked or the
// selected message instead
func MarkedOrSelected(pm widgets.ProvidesMessages) ([]*models.MessageInfo, error) {
func MarkedOrSelected(pm widgets.ProvidesMessages) ([]uint32, error) {
	// marked has priority over the selected message
	marked, err := pm.MarkedMessages()
	if err != nil {
@@ -165,7 +166,7 @@ func MarkedOrSelected(pm widgets.ProvidesMessages) ([]*models.MessageInfo, error
	if err != nil {
		return nil, err
	}
	return []*models.MessageInfo{msg}, nil
	return []uint32{msg.Uid}, nil
}

// UidsFromMessageInfos extracts a uid slice from a slice of MessageInfos
@@ -178,3 +179,15 @@ func UidsFromMessageInfos(msgs []*models.MessageInfo) []uint32 {
	}
	return uids
}

func MsgInfoFromUids(store *lib.MessageStore, uids []uint32) ([]*models.MessageInfo, error) {
	infos := make([]*models.MessageInfo, len(uids))
	for i, uid := range uids {
		var ok bool
		infos[i], ok = store.Messages[uid]
		if !ok {
			return nil, fmt.Errorf("uid not found")
		}
	}
	return infos, nil
}
diff --git a/widgets/account.go b/widgets/account.go
index 31384a5..20ed345 100644
--- a/widgets/account.go
+++ b/widgets/account.go
@@ -215,9 +215,9 @@ func (acct *AccountView) SelectedMessage() (*models.MessageInfo, error) {
	return msg, nil
}

func (acct *AccountView) MarkedMessages() ([]*models.MessageInfo, error) {
func (acct *AccountView) MarkedMessages() ([]uint32, error) {
	store := acct.Store()
	return msgInfoFromUids(store, store.Marked())
	return store.Marked(), nil
}

func (acct *AccountView) SelectedMessagePart() *PartInfo {
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go
index e192ae6..0cfabd7 100644
--- a/widgets/msgviewer.go
+++ b/widgets/msgviewer.go
@@ -262,9 +262,9 @@ func (mv *MessageViewer) SelectedMessage() (*models.MessageInfo, error) {
	return mv.msg.MessageInfo(), nil
}

func (mv *MessageViewer) MarkedMessages() ([]*models.MessageInfo, error) {
func (mv *MessageViewer) MarkedMessages() ([]uint32, error) {
	store := mv.Store()
	return msgInfoFromUids(store, store.Marked())
	return store.Marked(), nil
}

func (mv *MessageViewer) ToggleHeaders() {
diff --git a/widgets/providesmessage.go b/widgets/providesmessage.go
index b06825f..6e00b1c 100644
--- a/widgets/providesmessage.go
+++ b/widgets/providesmessage.go
@@ -25,5 +25,5 @@ type ProvidesMessages interface {
	Store() *lib.MessageStore
	SelectedAccount() *AccountView
	SelectedMessage() (*models.MessageInfo, error)
	MarkedMessages() ([]*models.MessageInfo, error)
	MarkedMessages() ([]uint32, error)
}
-- 
2.26.2
View this thread in the archives