2022-12-28 22:22:36 -05:00
|
|
|
package posts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-30 21:15:25 -05:00
|
|
|
"strings"
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-12-30 21:15:25 -05:00
|
|
|
"github.com/charmbracelet/glamour"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2022-12-30 03:22:54 -05:00
|
|
|
"github.com/mrusme/gobbs/aggregator"
|
2022-12-28 22:22:36 -05:00
|
|
|
"github.com/mrusme/gobbs/models/post"
|
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
2022-12-30 21:15:25 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/helpers"
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
listStyle = lipgloss.NewStyle().
|
|
|
|
Margin(0, 0, 0, 0).
|
|
|
|
Padding(1, 1).
|
2022-12-30 21:15:25 -05:00
|
|
|
Border(lipgloss.DoubleBorder()).
|
2022-12-28 22:22:36 -05:00
|
|
|
BorderForeground(lipgloss.Color("#874BFD")).
|
|
|
|
BorderTop(true).
|
|
|
|
BorderLeft(true).
|
|
|
|
BorderRight(true).
|
|
|
|
BorderBottom(true)
|
|
|
|
|
|
|
|
viewportStyle = lipgloss.NewStyle().
|
|
|
|
Margin(0, 0, 0, 0).
|
2022-12-30 21:15:25 -05:00
|
|
|
Padding(0, 0).
|
|
|
|
BorderTop(false).
|
|
|
|
BorderLeft(false).
|
|
|
|
BorderRight(false).
|
|
|
|
BorderBottom(false)
|
|
|
|
|
|
|
|
dialogBoxStyle = lipgloss.NewStyle().
|
2022-12-28 22:22:36 -05:00
|
|
|
Border(lipgloss.RoundedBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("#874BFD")).
|
2022-12-30 21:15:25 -05:00
|
|
|
Padding(1, 0).
|
2022-12-28 22:22:36 -05:00
|
|
|
BorderTop(true).
|
|
|
|
BorderLeft(true).
|
|
|
|
BorderRight(true).
|
|
|
|
BorderBottom(true)
|
2022-12-30 21:15:25 -05:00
|
|
|
|
|
|
|
buttonStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#FFF7DB")).
|
|
|
|
Background(lipgloss.Color("#888B7E")).
|
|
|
|
Padding(0, 3).
|
|
|
|
MarginTop(1)
|
|
|
|
|
|
|
|
activeButtonStyle = buttonStyle.Copy().
|
|
|
|
Foreground(lipgloss.Color("#FFF7DB")).
|
|
|
|
Background(lipgloss.Color("#F25D94")).
|
|
|
|
MarginRight(2).
|
|
|
|
Underline(true)
|
2022-12-30 22:07:09 -05:00
|
|
|
|
|
|
|
postAuthorStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#F25D94")).
|
|
|
|
Padding(0, 1)
|
|
|
|
|
|
|
|
postSubjectStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#FFFFFF")).
|
|
|
|
Background(lipgloss.Color("#F25D94")).
|
|
|
|
Padding(0, 1)
|
|
|
|
|
|
|
|
replyAuthorStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#FFFFFF")).
|
|
|
|
Background(lipgloss.Color("#888B7E")).
|
|
|
|
Padding(0, 1)
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeyMap struct {
|
|
|
|
Refresh key.Binding
|
|
|
|
Select key.Binding
|
|
|
|
SwitchFocus key.Binding
|
2022-12-30 21:15:25 -05:00
|
|
|
Close key.Binding
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultKeyMap = KeyMap{
|
|
|
|
Refresh: key.NewBinding(
|
|
|
|
key.WithKeys("r", "R"),
|
|
|
|
key.WithHelp("r/R", "refresh"),
|
|
|
|
),
|
|
|
|
Select: key.NewBinding(
|
|
|
|
key.WithKeys("enter"),
|
|
|
|
key.WithHelp("enter", "select"),
|
|
|
|
),
|
|
|
|
SwitchFocus: key.NewBinding(
|
|
|
|
key.WithKeys("tab"),
|
|
|
|
key.WithHelp("tab", "switch focus"),
|
|
|
|
),
|
2022-12-30 21:15:25 -05:00
|
|
|
Close: key.NewBinding(
|
|
|
|
key.WithKeys("esc"),
|
|
|
|
key.WithHelp("esc", "close"),
|
|
|
|
),
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
keymap KeyMap
|
|
|
|
list list.Model
|
|
|
|
items []list.Item
|
|
|
|
viewport viewport.Model
|
|
|
|
ctx *ctx.Ctx
|
2022-12-30 03:30:03 -05:00
|
|
|
a *aggregator.Aggregator
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 21:15:25 -05:00
|
|
|
glam *glamour.TermRenderer
|
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
focused int
|
|
|
|
focusables [2]tea.Model
|
2022-12-30 21:15:25 -05:00
|
|
|
|
|
|
|
viewportOpen bool
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2022-12-30 21:15:25 -05:00
|
|
|
keymap: DefaultKeyMap,
|
|
|
|
focused: 0,
|
|
|
|
viewportOpen: false,
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// m.focusables = append(m.focusables, m.list)
|
|
|
|
// m.focusables = append(m.focusables, m.viewport)
|
|
|
|
|
|
|
|
m.list = list.New(m.items, list.NewDefaultDelegate(), 0, 0)
|
|
|
|
m.list.Title = "Posts"
|
|
|
|
m.ctx = c
|
2022-12-30 03:30:03 -05:00
|
|
|
m.a, _ = aggregator.New(m.ctx)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
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.Refresh):
|
|
|
|
m.ctx.Loading = true
|
|
|
|
cmds = append(cmds, m.refresh())
|
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.SwitchFocus):
|
|
|
|
m.focused++
|
|
|
|
if m.focused >= len(m.focusables) {
|
|
|
|
m.focused = 0
|
|
|
|
}
|
|
|
|
// return m, nil
|
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.Select):
|
|
|
|
i, ok := m.list.SelectedItem().(post.Post)
|
|
|
|
if ok {
|
|
|
|
m.viewport.SetContent(m.renderViewport(&i))
|
|
|
|
return m, nil
|
|
|
|
}
|
2022-12-30 21:15:25 -05:00
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.Close):
|
|
|
|
if m.viewportOpen {
|
|
|
|
m.viewportOpen = false
|
|
|
|
return m, nil
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
2022-12-30 21:15:25 -05:00
|
|
|
listWidth := m.ctx.Content[0] - 2
|
2022-12-28 22:22:36 -05:00
|
|
|
listHeight := m.ctx.Content[1] - 1
|
2022-12-30 21:15:25 -05:00
|
|
|
viewportWidth := m.ctx.Content[0] - 9
|
|
|
|
viewportHeight := m.ctx.Content[1] - 10
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
listStyle.Width(listWidth)
|
|
|
|
listStyle.Height(listHeight)
|
|
|
|
m.list.SetSize(
|
|
|
|
listWidth-2,
|
|
|
|
listHeight-2,
|
|
|
|
)
|
|
|
|
|
|
|
|
viewportStyle.Width(viewportWidth)
|
|
|
|
viewportStyle.Height(viewportHeight)
|
|
|
|
m.viewport = viewport.New(viewportWidth-4, viewportHeight-4)
|
|
|
|
m.viewport.Width = viewportWidth - 4
|
|
|
|
m.viewport.Height = viewportHeight - 4
|
|
|
|
// cmds = append(cmds, viewport.Sync(m.viewport))
|
|
|
|
|
|
|
|
case []list.Item:
|
|
|
|
m.items = msg
|
|
|
|
m.list.SetItems(m.items)
|
|
|
|
m.ctx.Loading = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
|
|
if m.focused == 0 {
|
|
|
|
listStyle.BorderForeground(lipgloss.Color("#FFFFFF"))
|
|
|
|
viewportStyle.BorderForeground(lipgloss.Color("#874BFD"))
|
|
|
|
m.list, cmd = m.list.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
} else if m.focused == 1 {
|
|
|
|
listStyle.BorderForeground(lipgloss.Color("#874BFD"))
|
|
|
|
viewportStyle.BorderForeground(lipgloss.Color("#FFFFFF"))
|
|
|
|
m.viewport, cmd = m.viewport.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
2022-12-30 21:15:25 -05:00
|
|
|
var view strings.Builder = strings.Builder{}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 21:15:25 -05:00
|
|
|
view.WriteString(lipgloss.JoinHorizontal(
|
2022-12-28 22:22:36 -05:00
|
|
|
lipgloss.Top,
|
|
|
|
listStyle.Render(m.list.View()),
|
2022-12-30 21:15:25 -05:00
|
|
|
))
|
|
|
|
|
|
|
|
if m.viewportOpen {
|
|
|
|
okButton := activeButtonStyle.Render("[R]eply")
|
|
|
|
cancelButton := buttonStyle.Render("Close")
|
|
|
|
|
|
|
|
buttons := lipgloss.JoinHorizontal(lipgloss.Top, okButton, cancelButton)
|
|
|
|
ui := lipgloss.JoinVertical(
|
|
|
|
lipgloss.Center,
|
|
|
|
viewportStyle.Render(m.viewport.View()),
|
|
|
|
buttons,
|
|
|
|
)
|
|
|
|
|
|
|
|
return helpers.PlaceOverlay(3, 2, dialogBoxStyle.Render(ui), view.String())
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 21:15:25 -05:00
|
|
|
return view.String()
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) refresh() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
var items []list.Item
|
|
|
|
|
2022-12-30 03:30:03 -05:00
|
|
|
posts, errs := m.a.ListPosts()
|
2022-12-30 03:22:54 -05:00
|
|
|
if len(errs) > 0 {
|
|
|
|
fmt.Printf("%s", errs) // TODO: Implement error message
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
for _, post := range posts {
|
|
|
|
items = append(items, post)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) renderViewport(post *post.Post) string {
|
|
|
|
var vp string = ""
|
|
|
|
|
2022-12-30 03:30:03 -05:00
|
|
|
m.a.LoadPost(post)
|
|
|
|
|
2022-12-30 21:15:25 -05:00
|
|
|
var err error
|
|
|
|
m.glam, err = glamour.NewTermRenderer(
|
|
|
|
glamour.WithAutoStyle(),
|
|
|
|
glamour.WithWordWrap(m.viewport.Width),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
m.glam = nil
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-30 22:07:09 -05:00
|
|
|
adj := "writes"
|
|
|
|
if post.Subject[len(post.Subject)-1:] == "?" {
|
|
|
|
adj = "asks"
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := m.glam.Render(post.Body)
|
2022-12-30 21:15:25 -05:00
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
2022-12-30 22:07:09 -05:00
|
|
|
body = post.Body
|
|
|
|
}
|
|
|
|
vp = fmt.Sprintf(
|
|
|
|
" %s\n %s\n%s",
|
|
|
|
postAuthorStyle.Render(
|
|
|
|
fmt.Sprintf("%s %s:", post.Author.Name, adj),
|
|
|
|
),
|
|
|
|
postSubjectStyle.Render(post.Subject),
|
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, reply := range post.Replies {
|
|
|
|
body, err := m.glam.Render(reply.Body)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
body = reply.Body
|
|
|
|
}
|
|
|
|
vp = fmt.Sprintf(
|
|
|
|
"%s\n\n %s\n%s",
|
|
|
|
vp,
|
|
|
|
replyAuthorStyle.Render(
|
|
|
|
fmt.Sprintf("%s writes:", reply.Author.Name),
|
|
|
|
),
|
|
|
|
body,
|
|
|
|
)
|
2022-12-30 21:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m.viewportOpen = true
|
2022-12-30 22:07:09 -05:00
|
|
|
return vp
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|