diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-10-20 16:43:41 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-10-27 22:44:39 +0200 |
commit | c5face0b6f922781f1d2ae754c00fdee6c58af20 (patch) | |
tree | 13fd5d1026319f36921be6776b13ae87c68f16ec /widgets/msglist.go | |
parent | c83ffabf3853e5f06294bba78dc23bc7ff84b0af (diff) | |
download | aerc-c5face0b6f922781f1d2ae754c00fdee6c58af20.tar.gz |
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 <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'widgets/msglist.go')
-rw-r--r-- | widgets/msglist.go | 56 |
1 files changed, 36 insertions, 20 deletions
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() } |