Check bounds of list

This commit is contained in:
2026-01-23 14:56:00 -06:00
parent c821a8fa06
commit b04cc91245

View File

@@ -227,6 +227,12 @@ func (w *SimpleList) MinW() int {
func (w *SimpleList) MinH() int { return 4 } func (w *SimpleList) MinH() int { return 4 }
func (w *SimpleList) getItemAt(idx int) {
for idx >= len(w.List) {
idx--
}
}
func (w *SimpleList) SetCursorWrap(b bool) { w.cursorWrap = b } func (w *SimpleList) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *SimpleList) MoveUp() bool { func (w *SimpleList) MoveUp() bool {
if w.cursor > 0 { if w.cursor > 0 {
@@ -328,7 +334,12 @@ func (w *SimpleList) SetItem(idx int, txt string) {
w.list[idx] = txt w.list[idx] = txt
} }
} }
func (w *SimpleList) SelectedIndex() int { return w.cursor } func (w *SimpleList) SelectedIndex() int {
if w.cursor >= len(w.list) {
w.cursor = len(w.list) - 1
}
return w.cursor
}
func (w *SimpleList) SetSelectedIndex(i int) { func (w *SimpleList) SetSelectedIndex(i int) {
if i < 0 { if i < 0 {
i = 0 i = 0
@@ -350,6 +361,11 @@ func (w *SimpleList) Log(txt string, args ...any) {
func (w *SimpleList) SetOnChange(c func(int, string) bool) { w.onChange = c } func (w *SimpleList) SetOnChange(c func(int, string) bool) { w.onChange = c }
func (w *SimpleList) GetSelectedItem() string { return w.list[w.cursor] } func (w *SimpleList) GetSelectedItem() string {
if w.cursor >= len(w.list) {
w.cursor = len(w.list) - 1
}
return w.list[w.cursor]
}
func (w *SimpleList) GetAllItems() []string { return w.list } func (w *SimpleList) GetAllItems() []string { return w.list }
func (w *SimpleList) GetAllItemStyles() map[int]tcell.Style { return w.itemsStyle } func (w *SimpleList) GetAllItemStyles() map[int]tcell.Style { return w.itemsStyle }