From b12eba55c3cac7e754d1a17c4da4ffa230846af9 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Thu, 4 Jul 2019 11:17:23 +0200 Subject: 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 --- widgets/dirlist.go | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/widgets/dirlist.go b/widgets/dirlist.go index c7469d1..01adfe7 100644 --- a/widgets/dirlist.go +++ b/widgets/dirlist.go @@ -133,32 +133,18 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) { } func (dirlist *DirectoryList) nextPrev(delta int) { - for i, dir := range dirlist.dirs { - if dir == dirlist.selected { - var j int - 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 - } + curIdx := sort.SearchStrings(dirlist.dirs, dirlist.selected) + if curIdx == len(dirlist.dirs) { + return + } + 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() { -- cgit v1.2.3