about summary refs log tree commit diff stats
path: root/widgets
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2020-05-30 21:27:27 +0100
committerReto Brunner <reto@labrat.space>2020-06-09 08:43:01 +0200
commit3877b1aa719569d3666bce227e351afcbc2628af (patch)
tree844dc66659aa7d9d649c5c2a6a2fbc41d943ff4a /widgets
parenta69399a138b46be45e0f6b1fd5bb89daea16e995 (diff)
downloadaerc-3877b1aa719569d3666bce227e351afcbc2628af.tar.gz
Add dirlist scrolling
Should fix #402
Diffstat (limited to 'widgets')
-rw-r--r--widgets/dirlist.go38
1 files changed, 35 insertions, 3 deletions
diff --git a/widgets/dirlist.go b/widgets/dirlist.go
index 600b38c..418d2ea 100644
--- a/widgets/dirlist.go
+++ b/widgets/dirlist.go
@@ -26,6 +26,7 @@ type DirectoryList struct {
 	logger    *log.Logger
 	selecting string
 	selected  string
+	scroll    int
 	spinner   *Spinner
 	worker    *types.Worker
 }
@@ -207,11 +208,17 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
 		return
 	}
 
-	row := 0
-	for _, name := range dirlist.dirs {
+	dirlist.ensureScroll(ctx.Height())
+
+	for i, name := range dirlist.dirs {
+		if i < dirlist.scroll {
+			continue
+		}
+		row := i - dirlist.scroll
 		if row >= ctx.Height() {
 			break
 		}
+
 		style := tcell.StyleDefault
 		if name == dirlist.selected {
 			style = style.Reverse(true)
@@ -226,7 +233,32 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
 		})
 
 		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
 	}
 }