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 --- widgets/msglist.go | 56 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'widgets/msglist.go') diff --git a/widgets/msglist.go b/widgets/msglist.go index 7857a3d7..f53c0b5f 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -66,11 +66,13 @@ func (ml *MessageList) Draw(ctx *ui.Context) { ml.UpdateScroller(ml.height, len(store.Uids())) if store := ml.Store(); store != nil && len(store.Uids()) > 0 { - idx := store.FindIndexByUid(store.SelectedUid()) - if idx < 0 { - idx = len(store.Uids()) - 1 + iter := store.UidsIterator() + for i := 0; iter.Next(); i++ { + if store.SelectedUid() == iter.Value().(uint32) { + ml.EnsureScroll(i) + break + } } - ml.EnsureScroll(len(store.Uids()) - idx - 1) } textWidth := ctx.Width() @@ -87,23 +89,24 @@ func (ml *MessageList) Draw(ctx *ui.Context) { ) if store.ThreadedView() { - threads := store.Threads() - counter := len(store.Uids()) + iter := store.ThreadsIterator() + var i int = 0 - for i := len(threads) - 1; i >= 0; i-- { + for iter.Next() { + thread := iter.Value().(*types.Thread) var lastSubject string - err := threads[i].Walk(func(t *types.Thread, _ int, currentErr error) error { + err := thread.Walk(func(t *types.Thread, _ int, currentErr error) error { if currentErr != nil { return currentErr } if t.Hidden || t.Deleted { return nil } - counter-- - if counter > len(store.Uids())-1-ml.Scroll() { - // skip messages which are higher than the viewport + if i < ml.Scroll() { + i++ return nil } + i++ msg := store.Messages[t.Uid] var prefix string var subject string @@ -139,9 +142,13 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } } } else { - uids := store.Uids() - for i := len(uids) - 1 - ml.Scroll(); i >= 0; i-- { - uid := uids[i] + iter := store.UidsIterator() + for i := 0; iter.Next(); i++ { + if i < ml.Scroll() { + continue + } + uid := iter.Value().(uint32) + msg := store.Messages[uid] fmtCtx := format.Ctx{ FromAddress: acct.acct.From, @@ -395,13 +402,22 @@ func (ml *MessageList) Select(index int) { if len(uids) == 0 { return } - uidIdx := len(uids) - index - 1 - if uidIdx >= len(store.Uids()) { - uidIdx = 0 - } else if uidIdx < 0 { - uidIdx = len(store.Uids()) - 1 + + iter := store.UidsIterator() + + var uid uint32 + if index < 0 { + uid = uids[iter.EndIndex()] + } else { + uid = uids[iter.StartIndex()] + for i := 0; iter.Next(); i++ { + if i >= index { + uid = iter.Value().(uint32) + break + } + } } - store.Select(store.Uids()[uidIdx]) + store.Select(uid) ml.Invalidate() } -- cgit