You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
980 B
Go

package app
import (
"github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math"
)
type menuList struct {
choices []menuItem
highlight int
}
func (m *menuList) handleEvent(e tcell.Event) change {
switch t := e.(type) {
case *tcell.EventKey:
key := t.Key()
switch key {
case tcell.KeyEnter:
return m.choices[m.highlight].onSelect
case tcell.KeyDown:
m.highlight = (m.highlight + 1) % len(m.choices)
return nil
case tcell.KeyUp:
if m.highlight == 0 {
m.highlight = len(m.choices) - 1
} else {
m.highlight--
}
return nil
}
}
return nil
}
func (m *menuList) draw(b *buffer, _ *state) {
for i, choice := range m.choices {
if i == m.highlight {
b.writeString("▷ "+choice.name, math.Vec{0, i}, tcell.StyleDefault)
} else {
b.writeString(" "+choice.name, math.Vec{0, i}, tcell.StyleDefault)
}
}
}
func (m *menuList) setFocus(bool) {}
type menuItem struct {
name string
onSelect change
}