1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Do not panic when scrolling an empty list

This commit is contained in:
Armin 2021-02-24 23:39:38 +01:00
parent f976fe697a
commit c7a62b6f63
2 changed files with 25 additions and 0 deletions

View File

@ -96,6 +96,9 @@ func (self *List) ScrollAmount(amount int) {
} else {
self.SelectedRow += amount
}
if self.SelectedRow < 0 {
self.SelectedRow = 0
}
}
func (self *List) ScrollUp() {
@ -133,4 +136,7 @@ func (self *List) ScrollTop() {
func (self *List) ScrollBottom() {
self.SelectedRow = len(self.Rows) - 1
if self.SelectedRow < 0 {
self.SelectedRow = 0
}
}

19
widgets/list_test.go Normal file
View File

@ -0,0 +1,19 @@
package widgets_test
import (
"image"
"testing"
"github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func TestEmptyListPageChange(t *testing.T) {
l := widgets.NewList()
l.SetRect(0, 0, 10, 10)
buff := termui.NewBuffer(image.Rect(0, 0, 10, 10))
l.ScrollDown()
l.Draw(buff)
l.ScrollBottom()
l.Draw(buff)
}