diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 15:27:56 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-16 17:29:17 +1000 |
commit | e68093fe9974296fe5afb22ec8e0adb1eb2f4316 (patch) | |
tree | 040dbfdb4dc438271d9f3febae410d25698f1a38 /vendor/github.com/jesseduffield/gocui/scrollbar.go | |
parent | b838b74801102b27782cc20c7396279630e77fba (diff) |
add scrollbars
Diffstat (limited to 'vendor/github.com/jesseduffield/gocui/scrollbar.go')
-rw-r--r-- | vendor/github.com/jesseduffield/gocui/scrollbar.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/vendor/github.com/jesseduffield/gocui/scrollbar.go b/vendor/github.com/jesseduffield/gocui/scrollbar.go new file mode 100644 index 000000000..3bdb4a45c --- /dev/null +++ b/vendor/github.com/jesseduffield/gocui/scrollbar.go @@ -0,0 +1,33 @@ +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 + } + height := int((float64(pageSize) / float64(listSize)) * float64(scrollAreaSize)) + minHeight := 2 + if height < minHeight { + return minHeight + } + + return height +} |