2023-01-02 15:33:12 -05:00
|
|
|
package postcreate
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-02 20:34:39 -05:00
|
|
|
"strconv"
|
2023-01-02 15:29:29 -05:00
|
|
|
"strings"
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
"github.com/charmbracelet/bubbles/cursor"
|
2023-01-02 15:29:29 -05:00
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/glamour"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/mrusme/gobbs/aggregator"
|
|
|
|
"github.com/mrusme/gobbs/models/post"
|
|
|
|
"github.com/mrusme/gobbs/models/reply"
|
|
|
|
"github.com/mrusme/gobbs/ui/cmd"
|
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2023-01-02 19:27:23 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/helpers"
|
2023-01-02 15:29:29 -05:00
|
|
|
)
|
|
|
|
|
2023-01-02 15:33:12 -05:00
|
|
|
var (
|
|
|
|
WIN_ID = "postcreate"
|
|
|
|
)
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
type KeyMap struct {
|
|
|
|
Refresh key.Binding
|
|
|
|
Select key.Binding
|
|
|
|
Esc key.Binding
|
|
|
|
Quit key.Binding
|
|
|
|
Reply key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultKeyMap = KeyMap{
|
|
|
|
Reply: key.NewBinding(
|
|
|
|
key.WithKeys("ctrl+s"),
|
2023-01-03 00:32:48 -05:00
|
|
|
key.WithHelp("ctrl+s", "send"),
|
2023-01-02 15:29:29 -05:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
ctx *ctx.Ctx
|
|
|
|
keymap KeyMap
|
2023-01-02 18:46:26 -05:00
|
|
|
wh [2]int
|
2023-01-02 15:29:29 -05:00
|
|
|
focused bool
|
2023-01-02 19:45:49 -05:00
|
|
|
xywh [4]int
|
2023-01-02 18:46:26 -05:00
|
|
|
textarea textarea.Model
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
a *aggregator.Aggregator
|
|
|
|
glam *glamour.TermRenderer
|
|
|
|
|
2023-01-02 20:34:39 -05:00
|
|
|
replyToIdx int
|
|
|
|
replyTo string
|
|
|
|
replyToIface interface{}
|
2023-01-02 18:46:26 -05:00
|
|
|
|
|
|
|
viewcache string
|
|
|
|
viewcacheTextareaXY []int
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Focus() {
|
|
|
|
m.focused = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Blur() {
|
|
|
|
m.focused = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-02 18:46:26 -05:00
|
|
|
ctx: c,
|
|
|
|
keymap: DefaultKeyMap,
|
|
|
|
focused: false,
|
2023-01-02 19:45:49 -05:00
|
|
|
xywh: [4]int{0, 0, 0, 0},
|
2023-01-02 18:46:26 -05:00
|
|
|
|
2023-01-02 20:34:39 -05:00
|
|
|
replyToIdx: 0,
|
|
|
|
replyTo: "",
|
|
|
|
replyToIface: nil,
|
2023-01-02 18:46:26 -05:00
|
|
|
|
|
|
|
viewcache: "",
|
|
|
|
viewcacheTextareaXY: []int{0, 0, 0, 0},
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
m.textarea = textarea.New()
|
|
|
|
m.textarea.Placeholder = "Type in your reply ..."
|
|
|
|
m.textarea.Prompt = ""
|
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.Reply):
|
2023-01-02 20:34:39 -05:00
|
|
|
|
|
|
|
var irtID string = ""
|
|
|
|
var irtIRT string = ""
|
|
|
|
var irtSysIDX int = 0
|
|
|
|
|
|
|
|
if m.replyToIdx == 0 {
|
|
|
|
pst := m.replyToIface.(post.Post)
|
|
|
|
irtID = pst.ID
|
|
|
|
irtSysIDX = pst.SysIDX
|
|
|
|
} else {
|
|
|
|
rply := m.replyToIface.(reply.Reply)
|
|
|
|
irtID = strconv.Itoa(m.replyToIdx + 1)
|
2023-01-02 23:30:22 -05:00
|
|
|
irtIRT = rply.InReplyTo // TODO: THis is empty? Why?
|
2023-01-02 20:34:39 -05:00
|
|
|
irtSysIDX = rply.SysIDX
|
|
|
|
}
|
|
|
|
|
|
|
|
r := reply.Reply{
|
|
|
|
ID: irtID,
|
|
|
|
InReplyTo: irtIRT,
|
|
|
|
Body: m.textarea.Value(),
|
|
|
|
SysIDX: irtSysIDX,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.a.CreateReply(&r)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
2023-01-03 00:32:48 -05:00
|
|
|
return m, cmd.New(
|
|
|
|
cmd.MsgError,
|
|
|
|
WIN_ID,
|
|
|
|
cmd.Arg{Name: "error", Value: err},
|
|
|
|
).Tea()
|
2023-01-02 20:34:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m.textarea.Reset()
|
|
|
|
m.replyToIdx = 0
|
|
|
|
return m, cmd.New(cmd.WMCloseWin, WIN_ID).Tea()
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
2023-01-02 18:46:26 -05:00
|
|
|
m.wh[0] = msg.Width
|
|
|
|
m.wh[1] = msg.Height
|
|
|
|
m.ctx.Logger.Debugf("received WindowSizeMsg: %v\n", m.wh)
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
case cmd.Command:
|
|
|
|
m.ctx.Logger.Debugf("got command: %v\n", msg)
|
|
|
|
switch msg.Call {
|
2023-01-02 18:46:26 -05:00
|
|
|
case cmd.WinOpen:
|
|
|
|
if msg.Target == WIN_ID {
|
2023-01-02 19:45:49 -05:00
|
|
|
m.xywh = msg.GetArg("xywh").([4]int)
|
2023-01-02 20:34:39 -05:00
|
|
|
m.replyToIdx = msg.GetArg("replyToIdx").(int)
|
|
|
|
m.replyTo = msg.GetArg("replyTo").(string)
|
|
|
|
m.replyToIface = msg.GetArg(m.replyTo)
|
2023-01-02 18:46:26 -05:00
|
|
|
return m, m.textarea.Focus()
|
|
|
|
}
|
|
|
|
case cmd.WinClose:
|
|
|
|
if msg.Target == WIN_ID {
|
|
|
|
m.textarea.Reset()
|
|
|
|
return m, nil
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
case cmd.WinFocus:
|
2023-01-02 18:46:26 -05:00
|
|
|
if msg.Target == WIN_ID ||
|
|
|
|
msg.Target == "*" {
|
2023-01-02 15:29:29 -05:00
|
|
|
m.focused = true
|
2023-01-02 19:27:23 -05:00
|
|
|
m.viewcache = m.buildView(false)
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case cmd.WinBlur:
|
2023-01-02 18:46:26 -05:00
|
|
|
if msg.Target == WIN_ID ||
|
|
|
|
msg.Target == "*" {
|
2023-01-02 15:29:29 -05:00
|
|
|
m.focused = false
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
m.ctx.Logger.Debugf("received unhandled command: %v\n", msg)
|
|
|
|
}
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
case cursor.BlinkMsg:
|
|
|
|
m.ctx.Logger.Debugf("textarea is focused: %v\n", m.textarea.Focused())
|
|
|
|
|
2023-01-02 19:27:23 -05:00
|
|
|
// default:
|
|
|
|
// m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg)
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
var tcmd tea.Cmd
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
if !m.textarea.Focused() {
|
|
|
|
cmds = append(cmds, m.textarea.Focus())
|
|
|
|
}
|
|
|
|
m.textarea, tcmd = m.textarea.Update(msg)
|
|
|
|
cmds = append(cmds, tcmd)
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) loadPost(p *post.Post) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
m.ctx.Logger.Debug("------ EXECUTED -----")
|
|
|
|
if err := m.a.LoadPost(p); err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
|
|
|
return m.buildView(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) buildView(cached bool) string {
|
|
|
|
var view strings.Builder = strings.Builder{}
|
|
|
|
|
2023-01-02 19:27:23 -05:00
|
|
|
if cached && m.viewcache != "" {
|
|
|
|
m.ctx.Logger.Debugln("Cached View()")
|
|
|
|
|
|
|
|
m.textarea.SetWidth(m.viewcacheTextareaXY[2])
|
|
|
|
m.textarea.SetHeight(m.viewcacheTextareaXY[3])
|
|
|
|
|
|
|
|
return helpers.PlaceOverlay(
|
|
|
|
m.viewcacheTextareaXY[0], m.viewcacheTextareaXY[1],
|
|
|
|
m.textarea.View(), m.viewcache,
|
|
|
|
false)
|
|
|
|
}
|
2023-01-02 18:46:26 -05:00
|
|
|
|
|
|
|
title := "Reply"
|
|
|
|
if m.replyToIdx != 0 {
|
|
|
|
title += fmt.Sprintf(" to reply #%d", m.replyToIdx)
|
2023-01-02 15:29:29 -05:00
|
|
|
}
|
2023-01-02 18:46:26 -05:00
|
|
|
titlebar := m.ctx.Theme.DialogBox.Titlebar.Focused.
|
|
|
|
Align(lipgloss.Center).
|
|
|
|
Width(m.wh[0]).
|
|
|
|
Render(title)
|
|
|
|
|
|
|
|
textareaWidth := m.wh[0] - 2
|
|
|
|
textareaHeight := 6
|
|
|
|
m.textarea.SetWidth(textareaWidth)
|
|
|
|
m.textarea.SetHeight(textareaHeight)
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
bottombar := m.ctx.Theme.DialogBox.Bottombar.
|
2023-01-02 18:46:26 -05:00
|
|
|
Width(m.wh[0]).
|
2023-01-03 00:32:48 -05:00
|
|
|
Render("ctrl+s send · esc close")
|
2023-01-02 15:29:29 -05:00
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
replyWindow := lipgloss.JoinVertical(
|
2023-01-02 15:29:29 -05:00
|
|
|
lipgloss.Center,
|
|
|
|
titlebar,
|
2023-01-02 18:46:26 -05:00
|
|
|
m.textarea.View(),
|
2023-01-02 15:29:29 -05:00
|
|
|
bottombar,
|
|
|
|
)
|
|
|
|
|
2023-01-02 18:46:26 -05:00
|
|
|
tmp := m.ctx.Theme.DialogBox.Window.Focused.Render(replyWindow)
|
|
|
|
|
2023-01-02 19:45:49 -05:00
|
|
|
m.viewcacheTextareaXY[0] = 1
|
|
|
|
m.viewcacheTextareaXY[1] = 2
|
2023-01-02 18:46:26 -05:00
|
|
|
m.viewcacheTextareaXY[2] = textareaWidth
|
|
|
|
m.viewcacheTextareaXY[3] = textareaHeight
|
2023-01-02 15:29:29 -05:00
|
|
|
|
|
|
|
view = strings.Builder{}
|
|
|
|
view.WriteString(tmp)
|
|
|
|
|
|
|
|
return view.String()
|
|
|
|
}
|