neofeels/app/user.go

125 lines
2.7 KiB
Go
Raw Permalink Normal View History

2025-01-07 00:16:11 +00:00
package app
import (
"fmt"
"os"
"os/exec"
"path"
"strconv"
"git.sr.ht/~rockorager/vaxis"
"git.sr.ht/~rockorager/vaxis/widgets/term"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
)
type UserPage struct {
title string
list ui.List
help string
posts []ttbp.Post
2025-01-08 01:43:27 +00:00
self bool
2025-01-09 02:54:03 +00:00
index int // which index on the previous page to return to?
2025-01-07 00:16:11 +00:00
}
2025-01-09 02:54:03 +00:00
func NewUserPage(user string, self bool, index int) *UserPage {
2025-01-07 00:16:11 +00:00
posts := ttbp.GetPostsForUser(user)
list := []string{}
for _, post := range posts {
list = append(list, fmt.Sprintf(
"%s (%d words)",
post.Date.Format("2006-01-02"),
post.Words,
))
}
return &UserPage{
title,
2025-01-09 02:54:03 +00:00
ui.NewList(list, 0),
2025-01-07 00:16:11 +00:00
"↑↓/kj move ↵ enter q return",
posts,
2025-01-08 01:43:27 +00:00
self,
2025-01-09 02:54:03 +00:00
index,
2025-01-07 00:16:11 +00:00
}
}
func (user *UserPage) 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":
close(ui.Quit)
2025-01-07 22:44:43 +00:00
case "Down", "j", "Ctrl+n":
2025-01-07 00:16:11 +00:00
user.list.Down()
2025-01-07 22:44:43 +00:00
case "Up", "k", "Ctrl+p":
2025-01-07 00:16:11 +00:00
user.list.Up()
case "End", "Shift+g":
user.list.End()
case "Home", "g":
user.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
user.list.SetIndex(i)
case "q", "h", "Left":
2025-01-08 01:43:27 +00:00
if user.self {
2025-01-09 02:54:03 +00:00
ui.ViewChange <- NewManagement(0)
2025-01-08 01:43:27 +00:00
} else {
2025-01-09 02:54:03 +00:00
ui.ViewChange <- NewNeighbors(user.index)
2025-01-08 01:43:27 +00:00
}
2025-01-07 00:16:11 +00:00
case "Enter", "l", "Right":
showPost(state, user.posts[user.list.Index()])
}
processed = true
}
user.Draw(state)
return
}
func (user *UserPage) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: user.title})
user.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-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: user.help})
}
func showPost(state *ui.State, post ttbp.Post) {
state.HideCursor()
vt := term.New()
vt.TERM = os.Getenv("TERM")
vt.Attach(state.PostEvent())
vt.Focus()
2025-01-07 00:27:58 +00:00
pager := os.ExpandEnv(os.Getenv("PAGER"))
if pager == "" {
pager = "less"
}
err := vt.Start(exec.Command(pager, path.Join("/home", post.Author, ".ttbp/entries", post.Date.Format("20060102")+".txt")))
2025-01-07 00:16:11 +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()
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)
}
}
}