106 lines
1.8 KiB
Go
106 lines
1.8 KiB
Go
/**
|
|
* vaxis/widget/list, edited to support wraparounds and number seeking
|
|
* soon to support confirmations, once i get around to that
|
|
*/
|
|
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"git.sr.ht/~rockorager/vaxis"
|
|
)
|
|
|
|
type List struct {
|
|
index int
|
|
items []string
|
|
offset int
|
|
}
|
|
|
|
func NewList(items []string, index int) List {
|
|
return List{
|
|
items: items,
|
|
index: index,
|
|
}
|
|
}
|
|
|
|
func (m *List) Draw(win vaxis.Window) {
|
|
_, height := win.Size()
|
|
if m.index >= m.offset+height {
|
|
m.offset = m.index - height + 1
|
|
} else if m.index < m.offset {
|
|
m.offset = m.index
|
|
}
|
|
|
|
defaultStyle := vaxis.Style{}
|
|
selectedStyle := vaxis.Style{Attribute: vaxis.AttrReverse}
|
|
|
|
index := m.index - m.offset
|
|
for i, subject := range m.items[m.offset:] {
|
|
var style vaxis.Style
|
|
if i == index {
|
|
style = selectedStyle
|
|
} else {
|
|
style = defaultStyle
|
|
}
|
|
win.Println(i, vaxis.Segment{Text: fmt.Sprintf(" %-"+strconv.Itoa(win.Width-2)+"s", subject), Style: style})
|
|
}
|
|
|
|
}
|
|
|
|
func (m *List) Down() {
|
|
if m.index == len(m.items)-1 {
|
|
m.index = 0
|
|
} else {
|
|
m.index++
|
|
}
|
|
}
|
|
|
|
func (m *List) Up() {
|
|
if m.index == 0 {
|
|
m.index = len(m.items) - 1
|
|
} else {
|
|
m.index--
|
|
}
|
|
}
|
|
|
|
func (m *List) Home() {
|
|
m.index = 0
|
|
}
|
|
|
|
func (m *List) End() {
|
|
m.index = len(m.items) - 1
|
|
}
|
|
|
|
func (m *List) PageDown(win vaxis.Window) {
|
|
_, height := win.Size()
|
|
m.index = min(len(m.items)-1, m.index+height)
|
|
}
|
|
|
|
func (m *List) PageUp(win vaxis.Window) {
|
|
_, height := win.Size()
|
|
m.index = max(0, m.index-height)
|
|
}
|
|
|
|
func (m *List) Items() []string {
|
|
return m.items
|
|
}
|
|
|
|
func (m *List) SetItems(items []string) {
|
|
m.items = items
|
|
m.index = min(len(items)-1, m.index)
|
|
}
|
|
|
|
func (m *List) SetItem(index int, item string) {
|
|
m.items[index] = item
|
|
}
|
|
|
|
// Returns the index of the currently selected item.
|
|
func (m *List) Index() int {
|
|
return m.index
|
|
}
|
|
|
|
func (m *List) SetIndex(index int) {
|
|
m.index = index
|
|
}
|