neofeels/app/menu.go

163 lines
3.5 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 19:42:53 +00:00
"path"
2025-01-05 23:06:35 +00:00
"strconv"
2025-01-05 19:42:53 +00:00
"time"
2025-01-05 19:07:39 +00:00
2025-01-05 16:40:26 +00:00
"git.sr.ht/~rockorager/vaxis"
2025-01-05 19:07:39 +00:00
"git.sr.ht/~rockorager/vaxis/widgets/term"
2025-01-07 20:32:19 +00:00
"git.tilde.town/nbsp/neofeels/ttbp"
2025-01-05 16:40:26 +00:00
"git.tilde.town/nbsp/neofeels/ui"
)
type MainMenu struct {
title string
2025-01-05 23:06:35 +00:00
list ui.List
2025-01-05 16:40:26 +00:00
help string
}
2025-01-05 19:42:53 +00:00
const title = ` ___ __
2025-01-05 16:40:26 +00:00
/ _/__ ___ / /__
/ _/ -_) -_) (_-<
/_/ \__/\__/_/___/
2025-01-05 19:42:53 +00:00
neofeels 0.1.0`
func NewMainMenu() *MainMenu {
return &MainMenu{
title,
2025-01-05 23:06:35 +00:00
ui.NewList([]string{
2025-01-05 23:11:39 +00:00
"record some feels",
2025-01-06 21:53:29 +00:00
"manage your feels",
"check out your neighbors",
2025-01-07 00:55:07 +00:00
"browse global feels",
2025-01-07 12:25:35 +00:00
"visit your subscriptions",
"scribble some graffiti", // TODO
"change your settings",
2025-01-05 23:11:39 +00:00
"see credits",
"read documentation",
2025-01-05 16:40:26 +00:00
}),
"↑↓/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()
2025-01-05 20:58:43 +00:00
case "End", "Shift+g":
2025-01-05 16:40:26 +00:00
menu.list.End()
2025-01-05 20:58:43 +00:00
case "Home", "g":
2025-01-05 16:40:26 +00:00
menu.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8":
2025-01-05 23:06:35 +00:00
i, _ := strconv.Atoi(key.String())
menu.list.SetIndex(i)
2025-01-05 20:58:43 +00:00
case "Enter", "l", "Right":
2025-01-05 19:07:39 +00:00
switch menu.list.Index() {
2025-01-05 19:42:53 +00:00
case 0:
newFeels(state)
2025-01-05 20:58:43 +00:00
case 1:
ui.ViewChange <- NewManagement()
2025-01-06 21:53:29 +00:00
case 2:
ui.ViewChange <- NewNeighbors()
2025-01-07 00:55:07 +00:00
case 3:
ui.ViewChange <- NewBrowse()
2025-01-07 12:25:35 +00:00
case 4:
ui.ViewChange <- NewSubscriptions()
case 6:
ui.ViewChange <- NewConfig()
case 7:
2025-01-05 19:07:39 +00:00
ui.ViewChange <- NewCredits()
case 8:
2025-01-05 19:07:39 +00:00
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+8, 28, 1).Print(vaxis.Segment{Text: menu.help})
2025-01-05 16:40:26 +00:00
return
}
2025-01-05 19:07:39 +00:00
func showManpage(state *ui.State) {
2025-01-05 19:12:06 +00:00
state.HideCursor()
2025-01-05 19:07:39 +00:00
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:
2025-01-05 19:12:06 +00:00
state.HideCursor()
2025-01-05 19:07:39 +00:00
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)
}
}
}
2025-01-05 19:42:53 +00:00
func newFeels(state *ui.State) {
state.HideCursor()
vt := term.New()
vt.TERM = os.Getenv("TERM")
vt.Attach(state.PostEvent())
vt.Focus()
2025-01-07 20:32:19 +00:00
now := time.Now()
err := vt.Start(exec.Command(os.ExpandEnv(os.Getenv("EDITOR")), path.Join(os.Getenv("HOME"), ".ttbp/entries", now.Format("20060102")+".txt")))
2025-01-05 19:42:53 +00:00
if err != nil {
panic(err)
}
defer vt.Close()
for ev := range state.Events() {
switch ev.(type) {
case term.EventClosed:
state.HideCursor()
state.Window().Clear()
2025-01-07 20:32:19 +00:00
ttbp.NewNopub(now)
ttbp.Publish(now)
2025-01-05 19:42:53 +00:00
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)
}
}
}