File: scrollbar.go

package info (click to toggle)
golang-github-jesseduffield-gocui 0.4%2Bgit20250605.fc53879%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: makefile: 3
file content (28 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package gocui

import "math"

// returns start and height of scrollbar
// `max` is the maximum possible value of `position`
func calcScrollbar(listSize int, pageSize int, position int, scrollAreaSize int) (int, int) {
	height := calcScrollbarHeight(listSize, pageSize, scrollAreaSize)
	// assume we can't scroll past the last item
	maxPosition := listSize - pageSize
	if maxPosition <= 0 {
		return 0, height
	}
	if position == maxPosition {
		return scrollAreaSize - height, height
	}
	// we only want to show the scrollbar at the top or bottom positions if we're at the end. Hence the .Ceil (for moving the scrollbar once we scroll down) and the -1 (for pretending there's a smaller range than we actually have, with the above condition ensuring we snap to the bottom once we're at the end of the list)
	start := int(math.Ceil(((float64(position) / float64(maxPosition)) * float64(scrollAreaSize-height-1))))
	return start, height
}

func calcScrollbarHeight(listSize int, pageSize int, scrollAreaSize int) int {
	if pageSize >= listSize {
		return scrollAreaSize
	}

	return int((float64(pageSize) / float64(listSize)) * float64(scrollAreaSize))
}