From fa210d8e2388780aa05cac010a1b8627e733d9ae Mon Sep 17 00:00:00 2001 From: nbsp Date: Mon, 6 Jan 2025 01:06:35 +0200 Subject: [PATCH] copy over vaxis list widget --- app/management.go | 9 +++-- app/menu.go | 9 +++-- ui/list.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 ui/list.go diff --git a/app/management.go b/app/management.go index c75ba6a..9e6edbf 100644 --- a/app/management.go +++ b/app/management.go @@ -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": diff --git a/app/menu.go b/app/menu.go index 3f5e778..88649a3 100644 --- a/app/menu.go +++ b/app/menu.go @@ -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: diff --git a/ui/list.go b/ui/list.go new file mode 100644 index 0000000..06ff849 --- /dev/null +++ b/ui/list.go @@ -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 +}