1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00

Refactored postcreate to use ToolKit

This commit is contained in:
マリウス 2023-01-03 16:46:16 -05:00
parent 6a0e33a358
commit 36a47727c7
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
5 changed files with 171 additions and 204 deletions

View File

@ -19,6 +19,7 @@ type MsgHandling struct {
OnAnyUncaughtKey func(m interface{}, k tea.KeyMsg) (bool, []tea.Cmd) OnAnyUncaughtKey func(m interface{}, k tea.KeyMsg) (bool, []tea.Cmd)
OnViewResize func(m interface{}) (bool, []tea.Cmd) OnViewResize func(m interface{}) (bool, []tea.Cmd)
OnWinOpenCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd) OnWinOpenCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
OnWinCloseCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
OnWinRefreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd) OnWinRefreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
OnWinFreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd) OnWinFreshDataCmd func(m interface{}, c cmd.Command) (bool, []tea.Cmd)
} }
@ -81,6 +82,10 @@ func (tk *ToolKit) HandleMsg(m interface{}, msg tea.Msg) (bool, []tea.Cmd) {
if tk.mh.OnWinOpenCmd != nil { if tk.mh.OnWinOpenCmd != nil {
return tk.mh.OnWinOpenCmd(m, msg) return tk.mh.OnWinOpenCmd(m, msg)
} }
case cmd.WinClose:
if tk.mh.OnWinCloseCmd != nil {
return tk.mh.OnWinCloseCmd(m, msg)
}
case cmd.WinRefreshData: case cmd.WinRefreshData:
if tk.mh.OnWinRefreshDataCmd != nil { if tk.mh.OnWinRefreshDataCmd != nil {
return tk.mh.OnWinRefreshDataCmd(m, msg) return tk.mh.OnWinRefreshDataCmd(m, msg)

View File

@ -0,0 +1,80 @@
package postcreate
import (
"strconv"
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/cmd"
)
func handleSubmit(mi interface{}) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
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)
irtIRT = rply.InReplyTo // TODO: THis is empty? Why?
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)
cmds = append(cmds, cmd.New(
cmd.MsgError,
WIN_ID,
cmd.Arg{Name: "error", Value: err},
).Tea())
return true, cmds
}
m.textarea.Reset()
m.replyToIdx = 0
cmds = append(cmds, cmd.New(cmd.WMCloseWin, WIN_ID).Tea())
return true, cmds
}
func handleWinOpenCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
if c.Target == WIN_ID {
m.xywh = c.GetArg("xywh").([4]int)
m.replyToIdx = c.GetArg("replyToIdx").(int)
m.replyTo = c.GetArg("replyTo").(string)
m.replyToIface = c.GetArg(m.replyTo)
cmds = append(cmds, m.textarea.Focus())
return true, cmds
}
return false, cmds
}
func handleWinCloseCmd(mi interface{}, c cmd.Command) (bool, []tea.Cmd) {
var m *Model = mi.(*Model)
var cmds []tea.Cmd
if c.Target == WIN_ID {
m.textarea.Reset()
return true, cmds
}
return false, cmds
}

View File

@ -1,47 +1,21 @@
package postcreate package postcreate
import ( import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea" "github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mrusme/gobbs/aggregator" "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" "github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/helpers" "github.com/mrusme/gobbs/ui/toolkit"
) )
var ( var (
WIN_ID = "postcreate" WIN_ID = "postcreate"
) )
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"),
key.WithHelp("ctrl+s", "send"),
),
}
type Model struct { type Model struct {
ctx *ctx.Ctx ctx *ctx.Ctx
keymap KeyMap tk *toolkit.ToolKit
wh [2]int
focused bool
xywh [4]int xywh [4]int
textarea textarea.Model textarea textarea.Model
@ -63,8 +37,11 @@ func (m Model) Init() tea.Cmd {
func NewModel(c *ctx.Ctx) Model { func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
ctx: c, ctx: c,
keymap: DefaultKeyMap, tk: toolkit.New(
focused: false, WIN_ID,
c.Theme,
c.Logger,
),
xywh: [4]int{0, 0, 0, 0}, xywh: [4]int{0, 0, 0, 0},
replyToIdx: 0, replyToIdx: 0,
@ -79,100 +56,31 @@ func NewModel(c *ctx.Ctx) Model {
m.textarea.Placeholder = "Type in your reply ..." m.textarea.Placeholder = "Type in your reply ..."
m.textarea.Prompt = "" m.textarea.Prompt = ""
m.tk.KeymapAdd("submit", "submit", "ctrl+s")
m.a, _ = aggregator.New(m.ctx) m.a, _ = aggregator.New(m.ctx)
m.tk.SetViewFunc(buildView)
m.tk.SetMsgHandling(toolkit.MsgHandling{
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
{
ID: "submit",
Handler: handleSubmit,
},
},
OnWinOpenCmd: handleWinOpenCmd,
OnWinCloseCmd: handleWinCloseCmd,
})
return m return m
} }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmds []tea.Cmd
switch msg := msg.(type) { ret, cmds := m.tk.HandleMsg(&m, msg)
case tea.KeyMsg: if ret {
switch { return m, tea.Batch(cmds...)
case key.Matches(msg, m.keymap.Reply):
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)
irtIRT = rply.InReplyTo // TODO: THis is empty? Why?
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)
return m, cmd.New(
cmd.MsgError,
WIN_ID,
cmd.Arg{Name: "error", Value: err},
).Tea()
}
m.textarea.Reset()
m.replyToIdx = 0
return m, cmd.New(cmd.WMCloseWin, WIN_ID).Tea()
}
case tea.WindowSizeMsg:
m.wh[0] = msg.Width
m.wh[1] = msg.Height
m.ctx.Logger.Debugf("received WindowSizeMsg: %v\n", m.wh)
case cmd.Command:
m.ctx.Logger.Debugf("got command: %v\n", msg)
switch msg.Call {
case cmd.WinOpen:
if msg.Target == WIN_ID {
m.xywh = msg.GetArg("xywh").([4]int)
m.replyToIdx = msg.GetArg("replyToIdx").(int)
m.replyTo = msg.GetArg("replyTo").(string)
m.replyToIface = msg.GetArg(m.replyTo)
return m, m.textarea.Focus()
}
case cmd.WinClose:
if msg.Target == WIN_ID {
m.textarea.Reset()
return m, nil
}
case cmd.WinFocus:
if msg.Target == WIN_ID ||
msg.Target == "*" {
m.Focus()
}
return m, nil
case cmd.WinBlur:
if msg.Target == WIN_ID ||
msg.Target == "*" {
m.Blur()
}
return m, nil
default:
m.ctx.Logger.Debugf("received unhandled command: %v\n", msg)
}
case cursor.BlinkMsg:
m.ctx.Logger.Debugf("textarea is focused: %v\n", m.textarea.Focused())
// default:
// m.ctx.Logger.Debugf("received unhandled msg: %v\n", msg)
} }
var tcmd tea.Cmd var tcmd tea.Cmd
@ -185,81 +93,3 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }
func (m *Model) Focus() {
m.focused = true
m.viewcache = m.buildView(false)
}
func (m *Model) Blur() {
m.focused = false
m.viewcache = m.buildView(false)
}
func (m Model) View() string {
return m.buildView(true)
}
func (m Model) buildView(cached bool) string {
var view strings.Builder = strings.Builder{}
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)
}
title := "Reply"
if m.replyToIdx != 0 {
title += fmt.Sprintf(" to reply #%d", m.replyToIdx)
}
var style lipgloss.Style
if m.focused {
style = m.ctx.Theme.DialogBox.Titlebar.Focused
} else {
style = m.ctx.Theme.DialogBox.Titlebar.Blurred
}
titlebar := style.Align(lipgloss.Center).
Width(m.wh[0]).
Render(title)
textareaWidth := m.wh[0] - 2
textareaHeight := 6
m.textarea.SetWidth(textareaWidth)
m.textarea.SetHeight(textareaHeight)
bottombar := m.ctx.Theme.DialogBox.Bottombar.
Width(m.wh[0]).
Render("ctrl+s send · esc close")
replyWindow := lipgloss.JoinVertical(
lipgloss.Center,
titlebar,
m.textarea.View(),
bottombar,
)
var tmp string
if m.focused {
tmp = m.ctx.Theme.DialogBox.Window.Focused.Render(replyWindow)
} else {
tmp = m.ctx.Theme.DialogBox.Window.Blurred.Render(replyWindow)
}
m.viewcacheTextareaXY[0] = 1
m.viewcacheTextareaXY[1] = 2
m.viewcacheTextareaXY[2] = textareaWidth
m.viewcacheTextareaXY[3] = textareaHeight
view = strings.Builder{}
view.WriteString(tmp)
return view.String()
}

View File

@ -0,0 +1,51 @@
package postcreate
import (
"fmt"
"github.com/mrusme/gobbs/ui/helpers"
)
func (m Model) View() string {
return m.tk.View(&m, true)
}
func buildView(mi interface{}, cached bool) string {
var m *Model = mi.(*Model)
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)
}
title := "Reply"
if m.replyToIdx != 0 {
title += fmt.Sprintf(" to reply #%d", m.replyToIdx)
}
textareaWidth := m.tk.ViewWidth() - 2
textareaHeight := 6
m.textarea.SetWidth(textareaWidth)
m.textarea.SetHeight(textareaHeight)
m.viewcacheTextareaXY[0] = 1
m.viewcacheTextareaXY[1] = 2
m.viewcacheTextareaXY[2] = textareaWidth
m.viewcacheTextareaXY[3] = textareaHeight
m.ctx.Logger.Debugln("View()")
m.ctx.Logger.Debugf("IsFocused: %v\n", m.tk.IsFocused())
return m.tk.Dialog(
title,
m.textarea.View(),
)
}

View File

@ -27,6 +27,7 @@ var (
type Model struct { type Model struct {
ctx *ctx.Ctx ctx *ctx.Ctx
tk *toolkit.ToolKit tk *toolkit.ToolKit
viewport viewport.Model viewport viewport.Model
a *aggregator.Aggregator a *aggregator.Aggregator
@ -57,11 +58,11 @@ func NewModel(c *ctx.Ctx) Model {
replyIDs: []string{}, replyIDs: []string{},
} }
m.tk.SetViewFunc(buildView)
m.a, _ = aggregator.New(m.ctx)
m.tk.KeymapAdd("reply", "reply", "r") m.tk.KeymapAdd("reply", "reply", "r")
m.a, _ = aggregator.New(m.ctx)
m.tk.SetViewFunc(buildView)
m.tk.SetMsgHandling(toolkit.MsgHandling{ m.tk.SetMsgHandling(toolkit.MsgHandling{
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{ OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
{ {