forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollbar.go
44 lines (41 loc) · 1.07 KB
/
scrollbar.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package ui
func writeHorizontalScrollbar(b *Buffer, n, low, high, width int) {
slow, shigh := findScrollInterval(n, low, high, width)
for i := 0; i < width; i++ {
if slow <= i && i < shigh {
b.Write(' ', styleForScrollBarThumb.String())
} else {
b.Write('━', styleForScrollBarArea.String())
}
}
}
func renderScrollbar(n, low, high, height int) *Buffer {
slow, shigh := findScrollInterval(n, low, high, height)
// Logger.Printf("low = %d, high = %d, n = %d, slow = %d, shigh = %d", low, high, n, slow, shigh)
b := NewBuffer(1)
for i := 0; i < height; i++ {
if i > 0 {
b.Newline()
}
if slow <= i && i < shigh {
b.Write(' ', styleForScrollBarThumb.String())
} else {
b.Write('│', styleForScrollBarArea.String())
}
}
return b
}
func findScrollInterval(n, low, high, height int) (int, int) {
f := func(i int) int {
return int(float64(i)/float64(n)*float64(height) + 0.5)
}
scrollLow, scrollHigh := f(low), f(high)
if scrollLow == scrollHigh {
if scrollHigh == high {
scrollLow--
} else {
scrollHigh++
}
}
return scrollLow, scrollHigh
}