From c5face0b6f922781f1d2ae754c00fdee6c58af20 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Thu, 20 Oct 2022 16:43:41 +0200 Subject: store: reverse message list order with iterators Reverse the order of the messages in the message list. The complexity of reversing the order is abstracted away by the iterators. To reverse the message list, add the following to your aerc.conf: [ui] reverse-msglist-order=true Thanks to |cos| for sharing his initial implementation of reversing the order in the message list [0]. [0]: https://git.netizen.se/aerc/commit/?h=topic/asc_sort_imap Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/threadbuilder.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'lib/threadbuilder.go') diff --git a/lib/threadbuilder.go b/lib/threadbuilder.go index 6cd98e87..75a80797 100644 --- a/lib/threadbuilder.go +++ b/lib/threadbuilder.go @@ -4,6 +4,7 @@ import ( "sync" "time" + "git.sr.ht/~rjarry/aerc/lib/iterator" "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" @@ -16,13 +17,15 @@ type ThreadBuilder struct { messageidToUid map[string]uint32 seen map[uint32]bool threadedUids []uint32 + iterFactory iterator.Factory } -func NewThreadBuilder() *ThreadBuilder { +func NewThreadBuilder(i iterator.Factory) *ThreadBuilder { tb := &ThreadBuilder{ threadBlocks: make(map[uint32]jwz.Threadable), messageidToUid: make(map[string]uint32), seen: make(map[uint32]bool), + iterFactory: i, } return tb } @@ -154,17 +157,20 @@ func (builder *ThreadBuilder) sortThreads(threads []*types.Thread, orderedUids [ // RebuildUids rebuilds the uids from the given slice of threads func (builder *ThreadBuilder) RebuildUids(threads []*types.Thread) { uids := make([]uint32, 0, len(threads)) - for i := len(threads) - 1; i >= 0; i-- { - _ = threads[i].Walk(func(t *types.Thread, level int, currentErr error) error { - uids = append(uids, t.Uid) - return nil - }) + iterT := builder.iterFactory.NewIterator(threads) + for iterT.Next() { + _ = iterT.Value().(*types.Thread).Walk( + func(t *types.Thread, level int, currentErr error) error { + uids = append(uids, t.Uid) + return nil + }) } - // copy in reverse as msgList displays backwards - for i, j := 0, len(uids)-1; i < j; i, j = i+1, j-1 { - uids[i], uids[j] = uids[j], uids[i] + result := make([]uint32, 0, len(uids)) + iterU := builder.iterFactory.NewIterator(uids) + for iterU.Next() { + result = append(result, iterU.Value().(uint32)) } - builder.threadedUids = uids + builder.threadedUids = result } // threadable implements the jwz.threadable interface which is required for the -- cgit