neofeels/app/neighbors.go

105 lines
2.6 KiB
Go
Raw Normal View History

2025-01-06 21:53:29 +00:00
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 {
2025-01-07 11:50:02 +00:00
title string
list ui.List
help string
neighbors []ttbp.User
subscriptions *ttbp.Subscriptions
2025-01-06 21:53:29 +00:00
}
func NewNeighbors() *Neighbors {
users := ttbp.SortUsersByRecent(ttbp.GetUsers())
2025-01-07 11:50:02 +00:00
subscriptions := ttbp.GetSubscriptions()
2025-01-06 21:53:29 +00:00
list := []string{}
for _, user := range users {
2025-01-07 11:50:02 +00:00
list = append(list, formatNeighbor(user, subscriptions))
2025-01-06 21:53:29 +00:00
}
return &Neighbors{
title,
ui.NewList(list),
2025-01-07 11:50:02 +00:00
"↑↓/kj move ↵ enter s subscribe q return",
2025-01-06 21:53:29 +00:00
users,
2025-01-07 11:50:02 +00:00
subscriptions,
}
}
func formatNeighbor(user ttbp.User, subscriptions *ttbp.Subscriptions) string {
publishDir := ""
if user.Publishing {
publishDir = fmt.Sprintf("%s%s/%s", ttbp.PathLive, user.Name, user.PublishDir)
}
subscribed := " "
if subscriptions.IsSubscribed(user) {
subscribed = "+"
2025-01-06 21:53:29 +00:00
}
2025-01-07 11:50:02 +00:00
return fmt.Sprintf(
" %s ~%-14s %-15s %-43s",
subscribed,
user.Name,
"("+humanize.Time(user.LastPublished)+")",
publishDir,
)
2025-01-06 21:53:29 +00:00
}
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)
2025-01-07 22:44:43 +00:00
case "Down", "j", "Ctrl+n":
2025-01-06 21:53:29 +00:00
neighbors.list.Down()
2025-01-07 22:44:43 +00:00
case "Up", "k", "Ctrl+p":
2025-01-06 21:53:29 +00:00
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()
2025-01-07 11:50:02 +00:00
case "s":
user := neighbors.neighbors[neighbors.list.Index()]
if neighbors.subscriptions.IsSubscribed(user) {
neighbors.subscriptions.Unsubscribe(user)
} else {
neighbors.subscriptions.Subscribe(user)
}
2025-01-07 12:25:35 +00:00
neighbors.subscriptions.Write()
2025-01-07 11:50:02 +00:00
neighbors.list.SetItem(neighbors.list.Index(), formatNeighbor(user, neighbors.subscriptions))
2025-01-06 21:53:29 +00:00
case "Enter", "l", "Right":
2025-01-08 01:43:27 +00:00
ui.ViewChange <- NewUserPage(neighbors.neighbors[neighbors.list.Index()].Name, false)
2025-01-06 21:53:29 +00:00
}
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,
})
2025-01-07 11:50:02 +00:00
win.New(win.Width/2-22, win.Height/2+9, 44, 1).Print(vaxis.Segment{Text: neighbors.help})
2025-01-06 21:53:29 +00:00
}