84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
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,
|
|
ui.NewList(list, 0),
|
|
"↑↓/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)
|
|
case "Down", "j", "Ctrl+n":
|
|
browse.list.Down()
|
|
case "Up", "k", "Ctrl+p":
|
|
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":
|
|
ui.ViewChange <- NewMainMenu(3)
|
|
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})
|
|
}
|