gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/elvish/edit/ui/scrollbar.go (about) 1 package ui 2 3 func writeHorizontalScrollbar(b *Buffer, n, low, high, width int) { 4 slow, shigh := findScrollInterval(n, low, high, width) 5 for i := 0; i < width; i++ { 6 if slow <= i && i < shigh { 7 b.Write(' ', styleForScrollBarThumb.String()) 8 } else { 9 b.Write('━', styleForScrollBarArea.String()) 10 } 11 } 12 } 13 14 func renderScrollbar(n, low, high, height int) *Buffer { 15 slow, shigh := findScrollInterval(n, low, high, height) 16 // Logger.Printf("low = %d, high = %d, n = %d, slow = %d, shigh = %d", low, high, n, slow, shigh) 17 b := NewBuffer(1) 18 for i := 0; i < height; i++ { 19 if i > 0 { 20 b.Newline() 21 } 22 if slow <= i && i < shigh { 23 b.Write(' ', styleForScrollBarThumb.String()) 24 } else { 25 b.Write('│', styleForScrollBarArea.String()) 26 } 27 } 28 return b 29 } 30 31 func findScrollInterval(n, low, high, height int) (int, int) { 32 f := func(i int) int { 33 return int(float64(i)/float64(n)*float64(height) + 0.5) 34 } 35 scrollLow, scrollHigh := f(low), f(high) 36 if scrollLow == scrollHigh { 37 if scrollHigh == high { 38 scrollLow-- 39 } else { 40 scrollHigh++ 41 } 42 } 43 return scrollLow, scrollHigh 44 }