neofeels/app/menu.go

107 lines
2.3 KiB
Go
Raw Normal View History

2025-01-05 16:40:26 +00:00
package app
import (
2025-01-05 19:07:39 +00:00
"os"
"os/exec"
2025-01-05 16:40:26 +00:00
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/list"
2025-01-05 19:07:39 +00:00
"git.sr.ht/~rockorager/vaxis/widgets/term"
2025-01-05 16:40:26 +00:00
"git.tilde.town/nbsp/neofeels/ui"
)
type MainMenu struct {
title string
list list.List
help string
}
func NewMainMenu() *MainMenu {
return &MainMenu{
` ___ __
/ _/__ ___ / /__
/ _/ -_) -_) (_-<
/_/ \__/\__/_/___/
neofeels 0.1.0`,
list.New([]string{
" record some feels ",
" manage your feels ",
" check out your neighbors ",
" browse global feels ",
" visit your subscriptions ",
" scribble some graffiti ",
" change your settings ",
" send some feedback ",
" see credits ",
" read documentation ",
}),
"↑↓/kj move ↵ enter q exit",
}
}
func (menu *MainMenu) Event(state *ui.State, event vaxis.Event) (processed bool) {
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
switch key.String() {
case "Ctrl+c", "Ctrl+d", "q":
close(ui.Quit)
case "Down", "j":
menu.list.Down()
case "Up", "k":
menu.list.Up()
case "End":
menu.list.End()
case "Home":
menu.list.Home()
case "Enter":
2025-01-05 19:07:39 +00:00
switch menu.list.Index() {
case 8:
ui.ViewChange <- NewCredits()
case 9:
showManpage(state)
}
2025-01-05 16:40:26 +00:00
}
processed = true
}
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: menu.title})
menu.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 14,
Row: win.Height/2 - 2,
Width: 28,
Height: 10,
})
win.New(win.Width/2-14, win.Height/2+9, 28, 1).Print(vaxis.Segment{Text: menu.help})
return
}
2025-01-05 19:07:39 +00:00
func showManpage(state *ui.State) {
vt := term.New()
vt.TERM = os.Getenv("TERM")
vt.Attach(state.PostEvent())
vt.Focus()
err := vt.Start(exec.Command("man", "neofeels"))
if err != nil {
panic(err)
}
defer vt.Close()
for ev := range state.Events() {
switch ev.(type) {
case term.EventClosed:
state.Window().Clear()
return
case vaxis.Redraw:
vt.Draw(state.Window())
state.Render()
continue
}
// for some reason vaxis doubles all events for Press/Release so this just ignores releases
if key, ok := ev.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
vt.Update(ev)
}
}
}