Compare commits

...

3 Commits

Author SHA1 Message Date
nbsp 3120210931
align in list 2025-01-06 01:11:39 +02:00
nbsp fa210d8e23
copy over vaxis list widget 2025-01-06 01:06:35 +02:00
nbsp 10d962e5ef
implement scary confirmation ui 2025-01-06 00:53:05 +02:00
3 changed files with 201 additions and 26 deletions

View File

@ -1,21 +1,23 @@
package app package app
import ( import (
"fmt"
"strconv"
"git.sr.ht/~rockorager/vaxis" "git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/list"
"git.tilde.town/nbsp/neofeels/ui" "git.tilde.town/nbsp/neofeels/ui"
) )
type Management struct { type Management struct {
title string title string
list list.List list ui.List
help string help string
} }
func NewManagement() *Management { func NewManagement() *Management {
return &Management{ return &Management{
title, title,
list.New([]string{ ui.NewList([]string{
"read over feels", // TODO "read over feels", // TODO
"modify feels publishing", // TODO "modify feels publishing", // TODO
"backup your feels", // TODO "backup your feels", // TODO
@ -42,14 +44,26 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process
management.list.End() management.list.End()
case "Home", "g": case "Home", "g":
management.list.Home() 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": case "q", "h", "Left":
ui.ViewChange <- NewMainMenu() ui.ViewChange <- NewMainMenu()
case "Enter", "l", "Right": case "Enter", "l", "Right":
switch management.list.Index() { switch management.list.Index() {
case 6:
management.Confirmation(state, func() {}, "feels purged")
case 7:
management.Confirmation(state, func() {}, "feels account wiped")
} }
} }
processed = true processed = true
} }
management.Draw(state)
return
}
func (management *Management) Draw(state *ui.State) {
win := state.Window() win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: management.title}) win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: management.title})
management.list.Draw(vaxis.Window{ management.list.Draw(vaxis.Window{
@ -60,6 +74,68 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process
Width: 28, Width: 28,
Height: 10, Height: 10,
}) })
win.New(win.Width/2-14, win.Height/2+7, 28, 1).Print(vaxis.Segment{Text: management.help}) win.New(win.Width/2-15, win.Height/2+7, 30, 1).Print(vaxis.Segment{Text: management.help})
return }
// TODO: abstract this to our own List type
// TODO: figure out a less janky way instead of nesting event channels
func (management *Management) Confirmation(state *ui.State, action func(), message string) {
management.Draw(state)
win := state.Window()
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " are you sure? ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
loop:
for ev := range state.Events() {
switch ev.(type) {
case vaxis.Resize, vaxis.Redraw:
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: " are you sure? ",
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
switch ev.(vaxis.Key).String() {
case "Enter", "l", "Right":
action()
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: fmt.Sprintf(" %-26s", message),
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
for ev := range state.Events() {
switch ev.(type) {
case vaxis.Resize, vaxis.Redraw:
management.Draw(state)
win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{
Text: fmt.Sprintf(" %26s", message),
Style: vaxis.Style{
Foreground: vaxis.IndexColor(1),
Attribute: vaxis.AttrReverse,
},
})
state.Render()
case vaxis.Key:
if ev.(vaxis.Key).EventType == vaxis.EventPress {
break loop
}
}
}
}
break loop
}
}
} }

View File

@ -4,17 +4,17 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"strconv"
"time" "time"
"git.sr.ht/~rockorager/vaxis" "git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/list"
"git.sr.ht/~rockorager/vaxis/widgets/term" "git.sr.ht/~rockorager/vaxis/widgets/term"
"git.tilde.town/nbsp/neofeels/ui" "git.tilde.town/nbsp/neofeels/ui"
) )
type MainMenu struct { type MainMenu struct {
title string title string
list list.List list ui.List
help string help string
} }
@ -27,7 +27,7 @@ const title = ` ___ __
func NewMainMenu() *MainMenu { func NewMainMenu() *MainMenu {
return &MainMenu{ return &MainMenu{
title, title,
list.New([]string{ ui.NewList([]string{
"record some feels", "record some feels",
"manage your feels ", "manage your feels ",
"check out your neighbors", // TODO "check out your neighbors", // TODO
@ -56,6 +56,9 @@ func (menu *MainMenu) Event(state *ui.State, event vaxis.Event) (processed bool)
menu.list.End() menu.list.End()
case "Home", "g": case "Home", "g":
menu.list.Home() 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": case "Enter", "l", "Right":
switch menu.list.Index() { switch menu.list.Index() {
case 0: case 0:

96
ui/list.go 100644
View File

@ -0,0 +1,96 @@
/**
* 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) 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: 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) 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
}