dirlist: simplify nextPrev() considerably

Assuming we always have a sorted dirlist
(other code depends on that already), we don't need to loop over the
dirStore.
Any filtering done should be performed elsewhere
This commit is contained in:
Reto Brunner 2019-07-04 11:17:23 +02:00 committed by Drew DeVault
parent 0abca31c15
commit b12eba55c3

View file

@ -133,32 +133,18 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
} }
func (dirlist *DirectoryList) nextPrev(delta int) { func (dirlist *DirectoryList) nextPrev(delta int) {
for i, dir := range dirlist.dirs { curIdx := sort.SearchStrings(dirlist.dirs, dirlist.selected)
if dir == dirlist.selected { if curIdx == len(dirlist.dirs) {
var j int return
ndirs := len(dirlist.dirs)
for j = i + delta; j != i; j += delta {
if j < 0 {
j = ndirs - 1
}
if j >= ndirs {
j = 0
}
name := dirlist.dirs[j]
if len(dirlist.acctConf.Folders) > 1 && name != dirlist.selected {
idx := sort.SearchStrings(dirlist.acctConf.Folders, name)
if idx == len(dirlist.acctConf.Folders) ||
dirlist.acctConf.Folders[idx] != name {
continue
}
}
break
}
dirlist.Select(dirlist.dirs[j])
break
}
} }
newIdx := curIdx + delta
ndirs := len(dirlist.dirs)
if newIdx < 0 {
newIdx = ndirs - 1
} else if newIdx >= ndirs {
newIdx = 0
}
dirlist.Select(dirlist.dirs[newIdx])
} }
func (dirlist *DirectoryList) Next() { func (dirlist *DirectoryList) Next() {