Add dirlist scrolling

Should fix #402
This commit is contained in:
Jeffas 2020-05-30 21:27:27 +01:00 committed by Reto Brunner
parent a69399a138
commit 3877b1aa71

View file

@ -26,6 +26,7 @@ type DirectoryList struct {
logger *log.Logger logger *log.Logger
selecting string selecting string
selected string selected string
scroll int
spinner *Spinner spinner *Spinner
worker *types.Worker worker *types.Worker
} }
@ -207,11 +208,17 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
return return
} }
row := 0 dirlist.ensureScroll(ctx.Height())
for _, name := range dirlist.dirs {
for i, name := range dirlist.dirs {
if i < dirlist.scroll {
continue
}
row := i - dirlist.scroll
if row >= ctx.Height() { if row >= ctx.Height() {
break break
} }
style := tcell.StyleDefault style := tcell.StyleDefault
if name == dirlist.selected { if name == dirlist.selected {
style = style.Reverse(true) style = style.Reverse(true)
@ -226,7 +233,32 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
}) })
ctx.Printf(0, row, style, dirString) ctx.Printf(0, row, style, dirString)
row++ }
}
func (dirlist *DirectoryList) ensureScroll(h int) {
selectingIdx := findString(dirlist.dirs, dirlist.selecting)
maxScroll := len(dirlist.dirs) - h
if maxScroll < 0 {
maxScroll = 0
}
if selectingIdx >= dirlist.scroll && selectingIdx < dirlist.scroll+h {
if dirlist.scroll > maxScroll {
dirlist.scroll = maxScroll
}
return
}
if selectingIdx >= dirlist.scroll+h {
dirlist.scroll = selectingIdx - h + 1
} else if selectingIdx < dirlist.scroll {
dirlist.scroll = selectingIdx
}
if dirlist.scroll > maxScroll {
dirlist.scroll = maxScroll
} }
} }