neofeels/app/menu.go

157 lines
3.4 KiB
Go

package app
import (
"os"
"os/exec"
"path"
"strconv"
"time"
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/term"
"git.tilde.town/nbsp/neofeels/ui"
)
type MainMenu struct {
title string
list ui.List
help string
}
const title = ` ___ __
/ _/__ ___ / /__
/ _/ -_) -_) (_-<
/_/ \__/\__/_/___/
neofeels 0.1.0`
func NewMainMenu() *MainMenu {
return &MainMenu{
title,
ui.NewList([]string{
"record some feels",
"manage your feels",
"check out your neighbors",
"browse global feels",
"visit your subscriptions", // TODO
"scribble some graffiti", // TODO
"change your settings",
"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", "Shift+g":
menu.list.End()
case "Home", "g":
menu.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8":
i, _ := strconv.Atoi(key.String())
menu.list.SetIndex(i)
case "Enter", "l", "Right":
switch menu.list.Index() {
case 0:
newFeels(state)
case 1:
ui.ViewChange <- NewManagement()
case 2:
ui.ViewChange <- NewNeighbors()
case 3:
ui.ViewChange <- NewBrowse()
case 6:
ui.ViewChange <- NewConfig()
case 7:
ui.ViewChange <- NewCredits()
case 8:
showManpage(state)
}
}
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+8, 28, 1).Print(vaxis.Segment{Text: menu.help})
return
}
func showManpage(state *ui.State) {
state.HideCursor()
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.HideCursor()
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)
}
}
}
func newFeels(state *ui.State) {
state.HideCursor()
vt := term.New()
vt.TERM = os.Getenv("TERM")
vt.Attach(state.PostEvent())
vt.Focus()
err := vt.Start(exec.Command(os.ExpandEnv(os.Getenv("EDITOR")), path.Join(os.Getenv("HOME"), ".ttbp/entries", time.Now().Format("20060102")+".txt")))
if err != nil {
panic(err)
}
defer vt.Close()
for ev := range state.Events() {
switch ev.(type) {
case term.EventClosed:
state.HideCursor()
state.Window().Clear()
ui.ViewChange <- NewPosted()
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)
}
}
}