From 8c576798d5aa7afdbf0c3f787e3fbbcce0a82c39 Mon Sep 17 00:00:00 2001 From: Vitaly Ovchinnikov Date: Mon, 25 Sep 2023 15:38:06 +0000 Subject: export-mbox: preserve the user-visible sorting order when exporting Export messages to mbox format in the same order as they are displayed to the user. Both marked-only and "full" export modes are supported. Signed-off-by: Vitaly Ovchinnikov Acked-by: Robin Jarry --- commands/account/export-mbox.go | 53 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'commands') diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go index e9a663c5..a17b8a29 100644 --- a/commands/account/export-mbox.go +++ b/commands/account/export-mbox.go @@ -10,6 +10,7 @@ import ( "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/commands" + "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/log" mboxer "git.sr.ht/~rjarry/aerc/worker/mbox" "git.sr.ht/~rjarry/aerc/worker/types" @@ -66,7 +67,19 @@ func (ExportMbox) Execute(args []string) error { if msgProvider != nil { marked, err := msgProvider.MarkedMessages() if err == nil && len(marked) > 0 { - uids = marked + uids, err = sortMarkedUids(marked, store) + if err != nil { + return err + } + } + } + + // if no messages were marked, we export everything + if len(uids) == 0 { + var err error + uids, err = sortAllUids(store) + if err != nil { + return err } } @@ -86,11 +99,6 @@ func (ExportMbox) Execute(args []string) error { done := make(chan bool) - // if no messages were marked, we export everything - if len(uids) == 0 { - uids = make([]uint32, len(store.Uids())) - copy(uids, store.Uids()) - } t := time.Now() total := len(uids) @@ -150,3 +158,36 @@ func (ExportMbox) Execute(args []string) error { func exportFolderUsage(cmd string) error { return fmt.Errorf("Usage: %s ", cmd) } + +func sortMarkedUids(marked []uint32, store *lib.MessageStore) ([]uint32, error) { + lookup := map[uint32]bool{} + for _, uid := range marked { + lookup[uid] = true + } + uids := []uint32{} + iter := store.UidsIterator() + for iter.Next() { + uid, ok := iter.Value().(uint32) + if !ok { + return nil, errors.New("Invalid message UID value") + } + _, marked := lookup[uid] + if marked { + uids = append(uids, uid) + } + } + return uids, nil +} + +func sortAllUids(store *lib.MessageStore) ([]uint32, error) { + uids := []uint32{} + iter := store.UidsIterator() + for iter.Next() { + uid, ok := iter.Value().(uint32) + if !ok { + return nil, errors.New("Invalid message UID value") + } + uids = append(uids, uid) + } + return uids, nil +} -- cgit