neofeels/app/neighbors.go

84 lines
1.9 KiB
Go

package app
import (
"fmt"
"strconv"
"git.sr.ht/~rockorager/vaxis"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
"github.com/dustin/go-humanize"
)
type Neighbors struct {
title string
list ui.List
help string
neighbors []ttbp.User
}
func NewNeighbors() *Neighbors {
users := ttbp.SortUsersByRecent(ttbp.GetUsers())
list := []string{}
for _, user := range users {
publishDir := ""
if user.Publishing {
publishDir = fmt.Sprintf("%s%s/%s", ttbp.PathLive, user.Name, user.PublishDir)
}
list = append(list, fmt.Sprintf(
"~%-14s %-15s %-46s",
user.Name,
"("+humanize.Time(user.LastPublished)+")",
publishDir,
))
}
return &Neighbors{
title,
ui.NewList(list),
"↑↓/kj move ↵ enter q return",
users,
}
}
func (neighbors *Neighbors) 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":
neighbors.list.Down()
case "Up", "k":
neighbors.list.Up()
case "End", "Shift+g":
neighbors.list.End()
case "Home", "g":
neighbors.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
neighbors.list.SetIndex(i)
case "q", "h", "Left":
ui.ViewChange <- NewMainMenu()
case "Enter", "l", "Right":
ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name)
}
processed = true
}
neighbors.Draw(state)
return
}
func (neighbors *Neighbors) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: neighbors.title})
neighbors.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 40,
Row: win.Height/2 - 2,
Width: 80,
Height: 10,
})
win.New(win.Width/2-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: neighbors.help})
}