2022-12-28 22:22:36 -05:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
// "fmt"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
2023-01-02 13:25:09 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/cmd"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2022-12-31 19:03:53 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/header"
|
2022-12-28 22:27:46 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/views/posts"
|
2023-01-02 13:25:09 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/windowmanager"
|
|
|
|
"github.com/mrusme/gobbs/ui/windows/postdialog"
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
"github.com/mrusme/gobbs/ui/views"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
2023-01-02 15:29:29 -05:00
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
2022-12-28 22:22:36 -05:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeyMap struct {
|
2023-01-02 13:25:09 -05:00
|
|
|
Up key.Binding
|
|
|
|
Down key.Binding
|
|
|
|
Close key.Binding
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultKeyMap = KeyMap{
|
2023-01-01 20:53:28 -05:00
|
|
|
// Up: key.NewBinding(
|
|
|
|
// key.WithKeys("k", "up"),
|
|
|
|
// key.WithHelp("↑/k", "move up"),
|
|
|
|
// ),
|
|
|
|
// Down: key.NewBinding(
|
|
|
|
// key.WithKeys("j", "down"),
|
|
|
|
// key.WithHelp("↓/j", "move down"),
|
|
|
|
// ),
|
2023-01-02 13:25:09 -05:00
|
|
|
Close: key.NewBinding(
|
|
|
|
key.WithKeys("q", "esc"),
|
|
|
|
key.WithHelp("q/esc", "close"),
|
|
|
|
),
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
2022-12-31 19:03:53 -05:00
|
|
|
keymap KeyMap
|
|
|
|
header header.Model
|
|
|
|
views []views.View
|
|
|
|
currentView int
|
2023-01-02 13:25:09 -05:00
|
|
|
wm *windowmanager.WM
|
2022-12-31 19:03:53 -05:00
|
|
|
ctx *ctx.Ctx
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2022-12-31 19:03:53 -05:00
|
|
|
keymap: DefaultKeyMap,
|
|
|
|
currentView: 0,
|
2023-01-02 13:25:09 -05:00
|
|
|
wm: windowmanager.New(),
|
2022-12-31 19:03:53 -05:00
|
|
|
ctx: c,
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-31 19:03:53 -05:00
|
|
|
m.header = header.NewModel(m.ctx)
|
2022-12-31 19:06:39 -05:00
|
|
|
m.views = append(m.views, posts.NewModel(m.ctx))
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
2023-01-02 13:25:09 -05:00
|
|
|
return tea.Batch(
|
|
|
|
tea.EnterAltScreen,
|
|
|
|
cmd.New(cmd.ViewFocus, "*").Tea(),
|
|
|
|
cmd.New(cmd.ViewRefreshData, "*").Tea(),
|
|
|
|
)
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
cmds := make([]tea.Cmd, 0)
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2023-01-02 14:24:07 -05:00
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch {
|
2023-01-02 13:25:09 -05:00
|
|
|
case key.Matches(msg, m.keymap.Close):
|
|
|
|
m.ctx.Logger.Debug("close received")
|
|
|
|
if !m.wm.CloseFocused() {
|
|
|
|
m.ctx.Logger.Debug("CloseFocused() was false, quitting")
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
return m, nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.setSizes(msg.Width, msg.Height)
|
|
|
|
for i := range m.views {
|
|
|
|
v, cmd := m.views[i].Update(msg)
|
|
|
|
m.views[i] = v
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
2023-01-02 13:25:09 -05:00
|
|
|
ccmds := m.wm.UpdateAll(tea.WindowSizeMsg{
|
|
|
|
Width: m.ctx.Content[0],
|
|
|
|
Height: m.ctx.Content[1],
|
|
|
|
})
|
|
|
|
cmds = append(cmds, ccmds...)
|
|
|
|
|
|
|
|
case cmd.Command:
|
|
|
|
var ccmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg.Call {
|
|
|
|
case cmd.WinOpen:
|
|
|
|
m.ctx.Logger.Debugln("received WinOpen")
|
|
|
|
ccmds = m.wm.Open(
|
|
|
|
msg.Target,
|
|
|
|
postdialog.NewModel(m.ctx),
|
|
|
|
[4]int{3, 2, 10, 6},
|
2023-01-02 15:29:29 -05:00
|
|
|
&msg,
|
2023-01-02 13:25:09 -05:00
|
|
|
)
|
|
|
|
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
|
|
|
|
default:
|
2023-01-02 15:29:29 -05:00
|
|
|
if msg.Call < cmd.ViewFocus {
|
|
|
|
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)
|
|
|
|
ccmds = m.wm.UpdateAll(msg)
|
|
|
|
}
|
2023-01-02 13:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmds = append(cmds, ccmds...)
|
2023-01-02 14:24:07 -05:00
|
|
|
|
2023-01-02 15:29:29 -05:00
|
|
|
case spinner.TickMsg:
|
|
|
|
// Do nothing
|
|
|
|
|
2023-01-02 14:24:07 -05:00
|
|
|
default:
|
|
|
|
m.ctx.Logger.Debugf("updating all with default: %v\n", msg)
|
|
|
|
cmds = append(cmds, m.wm.UpdateAll(msg)...)
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:25:09 -05:00
|
|
|
v, vcmd := m.views[m.currentView].Update(msg)
|
2022-12-31 19:03:53 -05:00
|
|
|
m.views[m.currentView] = v
|
2023-01-02 13:25:09 -05:00
|
|
|
cmds = append(cmds, vcmd)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2023-01-02 13:25:09 -05:00
|
|
|
header, hcmd := m.header.Update(msg)
|
2022-12-31 19:03:53 -05:00
|
|
|
m.header = header
|
2023-01-02 13:25:09 -05:00
|
|
|
cmds = append(cmds, hcmd)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
|
|
|
s := strings.Builder{}
|
2023-01-01 01:59:10 -05:00
|
|
|
s.WriteString(m.header.View() + "\n")
|
2022-12-31 19:03:53 -05:00
|
|
|
s.WriteString(m.views[m.currentView].View())
|
2023-01-02 13:25:09 -05:00
|
|
|
|
|
|
|
return m.wm.View(s.String())
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) setSizes(winWidth int, winHeight int) {
|
|
|
|
(*m.ctx).Screen[0] = winWidth
|
|
|
|
(*m.ctx).Screen[1] = winHeight
|
|
|
|
m.ctx.Content[0] = m.ctx.Screen[0]
|
|
|
|
m.ctx.Content[1] = m.ctx.Screen[1] - 5
|
|
|
|
}
|