copy over vaxis list widget
parent
10d962e5ef
commit
fa210d8e23
|
@ -2,22 +2,22 @@ package app
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.sr.ht/~rockorager/vaxis"
|
||||
"git.sr.ht/~rockorager/vaxis/widgets/list"
|
||||
"git.tilde.town/nbsp/neofeels/ui"
|
||||
)
|
||||
|
||||
type Management struct {
|
||||
title string
|
||||
list list.List
|
||||
list ui.List
|
||||
help string
|
||||
}
|
||||
|
||||
func NewManagement() *Management {
|
||||
return &Management{
|
||||
title,
|
||||
list.New([]string{
|
||||
ui.NewList([]string{
|
||||
" read over feels ", // TODO
|
||||
" modify feels publishing ", // TODO
|
||||
" backup your feels ", // TODO
|
||||
|
@ -44,6 +44,9 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process
|
|||
management.list.End()
|
||||
case "Home", "g":
|
||||
management.list.Home()
|
||||
case "0", "1", "2", "3", "4", "5", "6", "7":
|
||||
i, _ := strconv.Atoi(key.String())
|
||||
management.list.SetIndex(i)
|
||||
case "q", "h", "Left":
|
||||
ui.ViewChange <- NewMainMenu()
|
||||
case "Enter", "l", "Right":
|
||||
|
|
|
@ -4,17 +4,17 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.sr.ht/~rockorager/vaxis"
|
||||
"git.sr.ht/~rockorager/vaxis/widgets/list"
|
||||
"git.sr.ht/~rockorager/vaxis/widgets/term"
|
||||
"git.tilde.town/nbsp/neofeels/ui"
|
||||
)
|
||||
|
||||
type MainMenu struct {
|
||||
title string
|
||||
list list.List
|
||||
list ui.List
|
||||
help string
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ const title = ` ___ __
|
|||
func NewMainMenu() *MainMenu {
|
||||
return &MainMenu{
|
||||
title,
|
||||
list.New([]string{
|
||||
ui.NewList([]string{
|
||||
" record some feels ",
|
||||
" manage your feels ",
|
||||
" check out your neighbors ", // TODO
|
||||
|
@ -56,6 +56,9 @@ func (menu *MainMenu) Event(state *ui.State, event vaxis.Event) (processed bool)
|
|||
menu.list.End()
|
||||
case "Home", "g":
|
||||
menu.list.Home()
|
||||
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
|
||||
i, _ := strconv.Atoi(key.String())
|
||||
menu.list.SetIndex(i)
|
||||
case "Enter", "l", "Right":
|
||||
switch menu.list.Index() {
|
||||
case 0:
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
/**
|
||||
* vaxis/widget/list, edited to support wraparounds and number seeking
|
||||
* soon to support confirmations, once i get around to that
|
||||
*/
|
||||
package ui
|
||||
|
||||
import (
|
||||
"git.sr.ht/~rockorager/vaxis"
|
||||
)
|
||||
|
||||
type List struct {
|
||||
index int
|
||||
items []string
|
||||
offset int
|
||||
}
|
||||
|
||||
func NewList(items []string) List {
|
||||
return List{
|
||||
items: items,
|
||||
}
|
||||
}
|
||||
|
||||
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: 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) SetItems(items []string) {
|
||||
m.items = items
|
||||
m.index = min(len(items)-1, m.index)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue