store: implement the simplified index handling

Simplify the index handling for moving to the next or previous message.
Same for moving to the next or previous search results. Moving of the
index variable relies on the iterator package's StartIndex and EndIndex
functions.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-10-20 16:43:43 +02:00 committed by Robin Jarry
parent 206665a2d9
commit 88afe7bb4a
2 changed files with 35 additions and 32 deletions

View file

@ -21,6 +21,9 @@ func FixBounds(i, lower, upper int) int {
// WrapBounds will wrap the index i around its upper- or lower-bound if // WrapBounds will wrap the index i around its upper- or lower-bound if
// out-of-bound // out-of-bound
func WrapBounds(i, lower, upper int) int { func WrapBounds(i, lower, upper int) int {
if upper <= 0 {
return lower
}
switch { switch {
case i > upper: case i > upper:
i = lower + (i-upper-1)%upper i = lower + (i-upper-1)%upper

View file

@ -593,29 +593,20 @@ func (store *MessageStore) NextPrev(delta int) {
if len(uids) == 0 { if len(uids) == 0 {
return return
} }
iter := store.iterFactory.NewIterator(uids) iter := store.iterFactory.NewIterator(uids)
uid := store.SelectedUid() newIdx := store.FindIndexByUid(store.SelectedUid())
newIdx := store.FindIndexByUid(uid)
if newIdx < 0 { if newIdx < 0 {
store.Select(uids[iter.StartIndex()]) store.Select(uids[iter.StartIndex()])
return return
} }
newIdx = iterator.MoveIndex(
low, high := iter.EndIndex(), iter.StartIndex() newIdx,
sign := -1 delta,
if high < low { iter,
low, high = high, low iterator.FixBounds,
sign = 1 )
}
newIdx += sign * delta
if newIdx >= len(uids) {
newIdx = high
} else if newIdx < 0 {
newIdx = low
}
store.Select(uids[newIdx]) store.Select(uids[newIdx])
if store.BuildThreads() && store.ThreadedView() { if store.BuildThreads() && store.ThreadedView() {
@ -631,15 +622,7 @@ func (store *MessageStore) NextPrev(delta int) {
if store.marker != nil { if store.marker != nil {
store.marker.UpdateVisualMark() store.marker.UpdateVisualMark()
} }
store.updateResults()
nextResultIndex := len(store.results) - store.resultIndex - 2*delta
if nextResultIndex < 0 || nextResultIndex >= len(store.results) {
return
}
nextResultUid := store.results[nextResultIndex]
if nextResultUid == store.SelectedUid() {
store.resultIndex += delta
}
} }
func (store *MessageStore) Next() { func (store *MessageStore) Next() {
@ -690,18 +673,35 @@ func (store *MessageStore) ApplyClear() {
store.Sort(nil, nil) store.Sort(nil, nil)
} }
func (store *MessageStore) updateResults() {
if len(store.results) == 0 || store.resultIndex < 0 {
return
}
uid := store.SelectedUid()
for i, u := range store.results {
if uid == u {
store.resultIndex = i
break
}
}
}
func (store *MessageStore) nextPrevResult(delta int) { func (store *MessageStore) nextPrevResult(delta int) {
if len(store.results) == 0 { if len(store.results) == 0 {
return return
} }
store.resultIndex += delta iter := store.iterFactory.NewIterator(store.results)
if store.resultIndex >= len(store.results) {
store.resultIndex = 0
}
if store.resultIndex < 0 { if store.resultIndex < 0 {
store.resultIndex = len(store.results) - 1 store.resultIndex = iter.StartIndex()
} else {
store.resultIndex = iterator.MoveIndex(
store.resultIndex,
delta,
iter,
iterator.WrapBounds,
)
} }
store.Select(store.results[len(store.results)-store.resultIndex-1]) store.Select(store.results[store.resultIndex])
store.update(false) store.update(false)
} }