neofeels/app/browse.go

84 lines
1.8 KiB
Go
Raw Permalink Normal View History

2025-01-07 00:55:07 +00:00
package app
import (
"fmt"
"strconv"
"git.sr.ht/~rockorager/vaxis"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
)
type Browse struct {
title string
list ui.List
help string
posts []ttbp.Post
}
func NewBrowse() *Browse {
users := ttbp.GetUsers()
posts := []ttbp.Post{}
for _, user := range users {
posts = append(posts, ttbp.GetPostsForUser(user.Name)...)
}
posts = ttbp.SortPostsByRecent(posts)
list := []string{}
for _, post := range posts {
list = append(list, fmt.Sprintf(
"~%-15s %s (%d words)",
post.Author,
post.LastEdited.Format("2006-01-02 15:04"),
post.Words,
))
}
return &Browse{
title,
2025-01-09 02:54:03 +00:00
ui.NewList(list, 0),
2025-01-07 00:55:07 +00:00
"↑↓/kj move ↵ enter q return",
posts,
}
}
func (browse *Browse) 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:55:07 +00:00
browse.list.Down()
2025-01-07 22:44:43 +00:00
case "Up", "k", "Ctrl+p":
2025-01-07 00:55:07 +00:00
browse.list.Up()
case "End", "Shift+g":
browse.list.End()
case "Home", "g":
browse.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
browse.list.SetIndex(i)
case "q", "h", "Left":
2025-01-09 02:54:03 +00:00
ui.ViewChange <- NewMainMenu(3)
2025-01-07 00:55:07 +00:00
case "Enter", "l", "Right":
showPost(state, browse.posts[browse.list.Index()])
}
processed = true
}
browse.Draw(state)
return
}
func (browse *Browse) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: browse.title})
browse.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 25,
Row: win.Height/2 - 2,
Width: 50,
Height: 10,
})
win.New(win.Width/2-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: browse.help})
}