2023-01-02 15:33:12 -05:00
|
|
|
package postshow
|
2023-01-02 13:24:48 -05:00
|
|
|
|
|
|
|
import (
|
2023-01-05 23:11:55 -05:00
|
|
|
"time"
|
|
|
|
|
2023-01-02 13:24:48 -05:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/glamour"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
2023-01-06 19:46:41 -05:00
|
|
|
"github.com/mrusme/neonmodem/aggregator"
|
|
|
|
"github.com/mrusme/neonmodem/models/post"
|
|
|
|
"github.com/mrusme/neonmodem/models/reply"
|
|
|
|
"github.com/mrusme/neonmodem/ui/cmd"
|
|
|
|
"github.com/mrusme/neonmodem/ui/ctx"
|
|
|
|
"github.com/mrusme/neonmodem/ui/toolkit"
|
2023-01-02 13:24:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-02 15:33:12 -05:00
|
|
|
WIN_ID = "postshow"
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-02 13:24:48 -05:00
|
|
|
viewportStyle = lipgloss.NewStyle().
|
2023-01-02 15:29:29 -05:00
|
|
|
Margin(0, 0, 0, 0).
|
|
|
|
Padding(0, 0).
|
|
|
|
BorderTop(false).
|
|
|
|
BorderLeft(false).
|
|
|
|
BorderRight(false).
|
|
|
|
BorderBottom(false)
|
2023-01-02 13:24:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Model struct {
|
2023-01-03 16:46:16 -05:00
|
|
|
ctx *ctx.Ctx
|
|
|
|
tk *toolkit.ToolKit
|
|
|
|
|
2023-01-02 13:24:48 -05:00
|
|
|
viewport viewport.Model
|
|
|
|
|
|
|
|
a *aggregator.Aggregator
|
|
|
|
glam *glamour.TermRenderer
|
|
|
|
|
|
|
|
buffer string
|
|
|
|
replyIDs []string
|
|
|
|
|
|
|
|
activePost *post.Post
|
|
|
|
allReplies []*reply.Reply
|
|
|
|
activeReply *reply.Reply
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-03 15:02:43 -05:00
|
|
|
ctx: c,
|
|
|
|
tk: toolkit.New(
|
|
|
|
WIN_ID,
|
|
|
|
c.Theme,
|
|
|
|
c.Logger,
|
|
|
|
),
|
2023-01-02 13:24:48 -05:00
|
|
|
|
|
|
|
buffer: "",
|
|
|
|
replyIDs: []string{},
|
|
|
|
}
|
|
|
|
|
2023-01-04 15:28:25 -05:00
|
|
|
m.tk.KeymapAdd("reply", "reply (prefix with #, e.g. '2r')", "r")
|
2023-01-05 15:26:27 -05:00
|
|
|
m.tk.KeymapAdd("open", "open", "o")
|
2023-01-10 14:36:07 -05:00
|
|
|
m.tk.KeymapAdd("older", "older replies", "z")
|
2023-01-03 15:16:34 -05:00
|
|
|
|
2023-01-03 16:46:16 -05:00
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
|
|
|
|
|
|
|
m.tk.SetViewFunc(buildView)
|
2023-01-03 16:27:01 -05:00
|
|
|
m.tk.SetMsgHandling(toolkit.MsgHandling{
|
|
|
|
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
|
|
|
|
{
|
|
|
|
ID: "reply",
|
|
|
|
Handler: handleReply,
|
|
|
|
},
|
2023-01-05 15:26:27 -05:00
|
|
|
{
|
|
|
|
ID: "open",
|
|
|
|
Handler: handleOpen,
|
|
|
|
},
|
2023-01-10 14:36:07 -05:00
|
|
|
{
|
|
|
|
ID: "older",
|
|
|
|
Handler: handleOlder,
|
|
|
|
},
|
2023-01-03 16:27:01 -05:00
|
|
|
},
|
|
|
|
OnAnyNumberKey: handleNumberKeys,
|
|
|
|
OnAnyUncaughtKey: handleUncaughtKeys,
|
|
|
|
OnViewResize: handleViewResize,
|
|
|
|
OnWinOpenCmd: handleWinOpenCmd,
|
2023-01-05 23:11:55 -05:00
|
|
|
OnWinRefreshDataCmd: handleWinRefreshDataCmd,
|
2023-01-03 16:27:01 -05:00
|
|
|
OnWinFreshDataCmd: handleWinFreshDataCmd,
|
|
|
|
})
|
|
|
|
|
2023-01-02 13:24:48 -05:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:27:01 -05:00
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
ret, cmds := m.tk.HandleMsg(&m, msg)
|
|
|
|
if ret {
|
|
|
|
return m, tea.Batch(cmds...)
|
2023-01-02 13:24:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
|
|
m.viewport, cmd = m.viewport.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
2023-01-03 16:47:17 -05:00
|
|
|
|
2023-01-05 23:11:55 -05:00
|
|
|
func (m *Model) loadPost(p *post.Post, delay ...time.Duration) tea.Cmd {
|
2023-01-03 16:47:17 -05:00
|
|
|
return func() tea.Msg {
|
2023-01-05 23:11:55 -05:00
|
|
|
if len(delay) == 1 {
|
|
|
|
time.Sleep(delay[0])
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:47:17 -05:00
|
|
|
if err := m.a.LoadPost(p); err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
c := cmd.New(
|
|
|
|
cmd.MsgError,
|
|
|
|
WIN_ID,
|
|
|
|
cmd.Arg{Name: "error", Value: err},
|
|
|
|
)
|
|
|
|
return *c
|
|
|
|
}
|
|
|
|
|
|
|
|
c := cmd.New(
|
|
|
|
cmd.WinFreshData,
|
|
|
|
WIN_ID,
|
|
|
|
cmd.Arg{Name: "post", Value: p},
|
|
|
|
)
|
|
|
|
return *c
|
|
|
|
}
|
|
|
|
}
|