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 subscriptions *ttbp.Subscriptions } func NewNeighbors() *Neighbors { users := ttbp.SortUsersByRecent(ttbp.GetUsers()) subscriptions := ttbp.GetSubscriptions() list := []string{} for _, user := range users { list = append(list, formatNeighbor(user, subscriptions)) } return &Neighbors{ title, ui.NewList(list), "↑↓/kj move ↵ enter s subscribe q return", users, 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 = "+" } return fmt.Sprintf( " %s ~%-14s %-15s %-43s", subscribed, user.Name, "("+humanize.Time(user.LastPublished)+")", publishDir, ) } 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", "Ctrl+n": neighbors.list.Down() case "Up", "k", "Ctrl+p": 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 "s": user := neighbors.neighbors[neighbors.list.Index()] if neighbors.subscriptions.IsSubscribed(user) { neighbors.subscriptions.Unsubscribe(user) } else { neighbors.subscriptions.Subscribe(user) } neighbors.subscriptions.Write() neighbors.list.SetItem(neighbors.list.Index(), formatNeighbor(user, neighbors.subscriptions)) 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-22, win.Height/2+9, 44, 1).Print(vaxis.Segment{Text: neighbors.help}) }