93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
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,
|
|
ui.NewList(list, 0),
|
|
"↑↓/kj move ↵ enter q return",
|
|
posts,
|
|
}
|
|
}
|
|
|
|
func formatPublishing(post ttbp.Post) string {
|
|
nopub := ""
|
|
if post.Nopub {
|
|
nopub = "(nopub)"
|
|
}
|
|
return fmt.Sprintf(
|
|
"%s %s",
|
|
post.Date.Format("2006-01-02"),
|
|
nopub,
|
|
)
|
|
}
|
|
|
|
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()
|
|
ui.ViewChange <- NewManagement(1)
|
|
case "Enter", "l", "Right":
|
|
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()]))
|
|
}
|
|
}
|
|
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,
|
|
Column: win.Width/2 - 14,
|
|
Row: win.Height/2 - 2,
|
|
Width: 28,
|
|
Height: 10,
|
|
})
|
|
win.New(win.Width/2-15, win.Height/2+9, 30, 1).Print(vaxis.Segment{Text: publishing.help})
|
|
}
|