9fans.net/go@v0.0.5/draw/scroll.go (about) 1 package draw 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 ) 8 9 var mss struct { 10 lines int 11 percent float64 12 } 13 14 // Mousescrollsize computes the number of lines of text that 15 // should be scrolled in response to a mouse scroll wheel 16 // click. Maxlines is the number of lines visible in the text 17 // window. 18 // 19 // The default scroll increment is one line. This default can 20 // be overridden by setting the $mousescrollsize environment 21 // variable to an integer, which specifies a constant number of 22 // lines, or to a real number followed by a percent character, 23 // indicating that the scroll increment should be a percentage 24 // of the total number of lines in the window. For example, 25 // setting $mousescrollsize to 50% causes a half-window scroll 26 // increment. 27 func MouseScrollSize(maxlines int) int { 28 if mss.lines == 0 && mss.percent == 0 { 29 if s := strings.TrimSpace(os.Getenv("mousescrollsize")); s != "" { 30 if strings.HasSuffix(s, "%") { 31 mss.percent, _ = strconv.ParseFloat(strings.TrimSpace(s[:len(s)-1]), 64) 32 } else { 33 mss.lines, _ = strconv.Atoi(s) 34 } 35 } 36 if mss.lines == 0 && mss.percent == 0 { 37 mss.lines = 1 38 } 39 if mss.percent >= 100 { 40 mss.percent = 100 41 } 42 } 43 44 if mss.lines != 0 { 45 return mss.lines 46 } 47 return int(mss.percent * float64(maxlines) / 100.0) 48 }