147 lines
3.2 KiB
Go
147 lines
3.2 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"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
|
|
help string
|
|
}
|
|
|
|
const title = ` ___ __
|
|
/ _/__ ___ / /__
|
|
/ _/ -_) -_) (_-<
|
|
/_/ \__/\__/_/___/
|
|
neofeels 0.1.0`
|
|
|
|
func NewMainMenu() *MainMenu {
|
|
return &MainMenu{
|
|
title,
|
|
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":
|
|
switch menu.list.Index() {
|
|
case 0:
|
|
newFeels(state)
|
|
case 8:
|
|
ui.ViewChange <- NewCredits()
|
|
case 9:
|
|
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+9, 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.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)
|
|
}
|
|
}
|
|
}
|