neofeels/app/publishing.go

102 lines
2.5 KiB
Go
Raw Normal View History

2025-01-08 03:13:48 +00:00
package app
import (
"fmt"
"os/user"
"strconv"
"git.sr.ht/~rockorager/vaxis"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
)
type Publishing struct {
title string
list ui.List
help string
posts []ttbp.Post
}
func NewPublishing() *Publishing {
user, _ := user.Current()
posts := ttbp.GetPostsForUser(user.Username)
list := []string{}
for _, post := range posts {
list = append(list, formatPublishing(post))
}
return &Publishing{
title,
2025-01-09 02:54:03 +00:00
ui.NewList(list, 0),
2025-01-10 19:10:45 +00:00
"↑↓/kj move n nopub m html q return",
2025-01-08 03:13:48 +00:00
posts,
}
}
func formatPublishing(post ttbp.Post) string {
2025-01-10 19:10:45 +00:00
status := ""
2025-01-08 03:13:48 +00:00
if post.Nopub {
2025-01-10 19:10:45 +00:00
status += "(nopub) "
}
if post.HTML {
status += "(html)"
2025-01-08 03:13:48 +00:00
}
return fmt.Sprintf(
"%s %s",
post.Date.Format("2006-01-02"),
2025-01-10 19:10:45 +00:00
status,
2025-01-08 03:13:48 +00:00
)
}
func (publishing *Publishing) 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":
publishing.list.Down()
case "Up", "k", "Ctrl+p":
publishing.list.Up()
case "End", "Shift+g":
publishing.list.End()
case "Home", "g":
publishing.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
publishing.list.SetIndex(i)
case "q", "h", "Left":
ttbp.Publish()
2025-01-09 02:54:03 +00:00
ui.ViewChange <- NewManagement(1)
2025-01-10 19:10:45 +00:00
case "n":
2025-01-08 03:13:48 +00:00
if len(publishing.list.Items()) > 0 {
publishing.posts[publishing.list.Index()].Nopub = !publishing.posts[publishing.list.Index()].Nopub
ttbp.ToggleNopub(publishing.posts[publishing.list.Index()].Date)
publishing.list.SetItem(publishing.list.Index(), formatPublishing(publishing.posts[publishing.list.Index()]))
}
2025-01-10 19:10:45 +00:00
case "m":
if len(publishing.list.Items()) > 0 {
publishing.posts[publishing.list.Index()].HTML = !publishing.posts[publishing.list.Index()].HTML
ttbp.ToggleHTML(publishing.posts[publishing.list.Index()].Date)
publishing.list.SetItem(publishing.list.Index(), formatPublishing(publishing.posts[publishing.list.Index()]))
}
2025-01-08 03:13:48 +00:00
}
processed = true
}
publishing.Draw(state)
return
}
func (publishing *Publishing) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: publishing.title})
publishing.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
2025-01-10 19:10:45 +00:00
Column: win.Width/2 - 15,
2025-01-08 03:13:48 +00:00
Row: win.Height/2 - 2,
2025-01-10 19:10:45 +00:00
Width: 30,
2025-01-08 03:13:48 +00:00
Height: 10,
})
2025-01-10 19:10:45 +00:00
win.New(win.Width/2-19, win.Height/2+9, 38, 1).Print(vaxis.Segment{Text: publishing.help})
2025-01-08 03:13:48 +00:00
}