2023-01-03 00:32:48 -05:00
|
|
|
package msgerror
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/mrusme/gobbs/aggregator"
|
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2023-01-03 16:58:49 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/toolkit"
|
2023-01-03 00:32:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
WIN_ID = "msgerror"
|
|
|
|
|
|
|
|
viewportStyle = lipgloss.NewStyle().
|
|
|
|
Margin(0, 0, 0, 0).
|
|
|
|
Padding(0, 0).
|
|
|
|
BorderTop(false).
|
|
|
|
BorderLeft(false).
|
|
|
|
BorderRight(false).
|
|
|
|
BorderBottom(false)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Model struct {
|
2023-01-03 16:58:49 -05:00
|
|
|
ctx *ctx.Ctx
|
|
|
|
tk *toolkit.ToolKit
|
|
|
|
xywh [4]int
|
|
|
|
|
2023-01-03 00:32:48 -05:00
|
|
|
viewport viewport.Model
|
|
|
|
|
|
|
|
a *aggregator.Aggregator
|
|
|
|
|
2023-01-05 12:21:26 -05:00
|
|
|
errs []error
|
2023-01-03 00:32:48 -05:00
|
|
|
|
|
|
|
viewcache string
|
|
|
|
viewcacheTextareaXY []int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-03 16:58:49 -05:00
|
|
|
ctx: c,
|
|
|
|
tk: toolkit.New(
|
|
|
|
WIN_ID,
|
|
|
|
c.Theme,
|
|
|
|
c.Logger,
|
|
|
|
),
|
|
|
|
xywh: [4]int{0, 0, 0, 0},
|
2023-01-03 00:32:48 -05:00
|
|
|
|
2023-01-05 12:21:26 -05:00
|
|
|
errs: []error{},
|
2023-01-03 00:32:48 -05:00
|
|
|
|
|
|
|
viewcache: "",
|
|
|
|
viewcacheTextareaXY: []int{0, 0, 0, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
2023-01-03 16:58:49 -05:00
|
|
|
m.tk.SetViewFunc(buildView)
|
|
|
|
m.tk.SetMsgHandling(toolkit.MsgHandling{
|
|
|
|
OnViewResize: handleViewResize,
|
|
|
|
OnMsgErrorCmd: handleMsgErrorCmd,
|
|
|
|
OnWinCloseCmd: handleWinCloseCmd,
|
|
|
|
})
|
2023-01-03 00:32:48 -05:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
2023-01-03 16:58:49 -05:00
|
|
|
ret, cmds := m.tk.HandleMsg(&m, msg)
|
|
|
|
if ret {
|
|
|
|
return m, tea.Batch(cmds...)
|
2023-01-03 00:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var vcmd tea.Cmd
|
|
|
|
|
2023-01-05 12:21:26 -05:00
|
|
|
m.setErrorContent()
|
2023-01-03 00:32:48 -05:00
|
|
|
m.viewport, vcmd = m.viewport.Update(msg)
|
|
|
|
cmds = append(cmds, vcmd)
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
2023-01-05 12:21:26 -05:00
|
|
|
|
2023-01-05 15:26:08 -05:00
|
|
|
func (m *Model) setErrorContent() {
|
2023-01-05 12:21:26 -05:00
|
|
|
if len(m.errs) > 0 {
|
|
|
|
var tmp string = ""
|
|
|
|
for _, err := range m.errs {
|
|
|
|
tmp += err.Error() + "\n"
|
|
|
|
}
|
|
|
|
m.viewport.SetContent(tmp)
|
|
|
|
}
|
|
|
|
}
|