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"
|
2022-12-31 20:58:45 -05:00
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
2022-12-28 22:22:36 -05:00
|
|
|
"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"
|
2022-12-31 13:42:30 -05:00
|
|
|
"github.com/mrusme/gobbs/models/reply"
|
2022-12-28 22:22:36 -05:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-12-31 11:55:06 -05:00
|
|
|
var (
|
2022-12-28 22:22:36 -05:00
|
|
|
viewportStyle = lipgloss.NewStyle().
|
2022-12-31 16:57:11 -05:00
|
|
|
Margin(0, 0, 0, 0).
|
|
|
|
Padding(0, 0).
|
|
|
|
BorderTop(false).
|
|
|
|
BorderLeft(false).
|
|
|
|
BorderRight(false).
|
|
|
|
BorderBottom(false)
|
2022-12-28 22:22:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeyMap struct {
|
2022-12-30 23:57:03 -05:00
|
|
|
Refresh key.Binding
|
|
|
|
Select key.Binding
|
2023-01-01 20:53:28 -05:00
|
|
|
Esc key.Binding
|
|
|
|
Quit key.Binding
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultKeyMap = KeyMap{
|
|
|
|
Refresh: key.NewBinding(
|
2023-01-01 20:13:02 -05:00
|
|
|
key.WithKeys("ctrl+r"),
|
|
|
|
key.WithHelp("ctrl+r", "refresh"),
|
2022-12-28 22:22:36 -05:00
|
|
|
),
|
|
|
|
Select: key.NewBinding(
|
2023-01-01 20:13:02 -05:00
|
|
|
key.WithKeys("r", "enter"),
|
|
|
|
key.WithHelp("r/enter", "read"),
|
2022-12-28 22:22:36 -05:00
|
|
|
),
|
2023-01-01 20:53:28 -05:00
|
|
|
Esc: key.NewBinding(
|
|
|
|
key.WithKeys("esc"),
|
2022-12-30 21:15:25 -05:00
|
|
|
key.WithHelp("esc", "close"),
|
|
|
|
),
|
2023-01-01 20:53:28 -05:00
|
|
|
Quit: key.NewBinding(
|
|
|
|
key.WithKeys("q"),
|
|
|
|
),
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
keymap KeyMap
|
|
|
|
list list.Model
|
|
|
|
items []list.Item
|
|
|
|
viewport viewport.Model
|
2022-12-31 20:58:45 -05:00
|
|
|
textarea textarea.Model
|
2022-12-28 22:22:36 -05:00
|
|
|
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
|
|
|
|
|
2023-01-01 20:53:28 -05:00
|
|
|
focused string
|
|
|
|
buffer string
|
|
|
|
replyIDs []string
|
|
|
|
viewcache string
|
|
|
|
viewcacheTextareaXY []int
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
2022-12-31 21:09:43 -05:00
|
|
|
// TODO: Doesn't seem to be working
|
|
|
|
// return m.refresh()
|
|
|
|
return nil
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewModel(c *ctx.Ctx) Model {
|
|
|
|
m := Model{
|
2023-01-01 20:53:28 -05:00
|
|
|
ctx: c,
|
|
|
|
keymap: DefaultKeyMap,
|
|
|
|
focused: "list",
|
|
|
|
buffer: "",
|
|
|
|
viewcache: "",
|
|
|
|
viewcacheTextareaXY: []int{0, 0, 0, 0},
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
2022-12-31 18:32:04 -05:00
|
|
|
listDelegate := list.NewDefaultDelegate()
|
|
|
|
listDelegate.Styles.NormalTitle = m.ctx.Theme.PostsList.Item.Focused
|
|
|
|
listDelegate.Styles.DimmedTitle = m.ctx.Theme.PostsList.Item.Blurred
|
|
|
|
listDelegate.Styles.SelectedTitle = m.ctx.Theme.PostsList.Item.Selected
|
|
|
|
listDelegate.Styles.NormalDesc = m.ctx.Theme.PostsList.ItemDetail.Focused
|
|
|
|
listDelegate.Styles.DimmedDesc = m.ctx.Theme.PostsList.ItemDetail.Blurred
|
|
|
|
listDelegate.Styles.SelectedDesc = m.ctx.Theme.PostsList.ItemDetail.Selected
|
|
|
|
|
|
|
|
m.list = list.New(m.items, listDelegate, 0, 0)
|
2022-12-31 16:57:11 -05:00
|
|
|
m.list.SetShowTitle(false)
|
|
|
|
m.list.SetShowStatusBar(false)
|
2022-12-31 20:58:45 -05:00
|
|
|
|
|
|
|
m.textarea = textarea.New()
|
|
|
|
m.textarea.Placeholder = "Type in your reply ..."
|
2022-12-31 21:09:43 -05:00
|
|
|
m.textarea.Prompt = ""
|
2022-12-31 20:58:45 -05:00
|
|
|
|
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):
|
2022-12-31 20:58:45 -05:00
|
|
|
if m.focused == "list" {
|
|
|
|
m.ctx.Loading = true
|
|
|
|
cmds = append(cmds, m.refresh())
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
case key.Matches(msg, m.keymap.Select):
|
2022-12-31 20:58:45 -05:00
|
|
|
if m.focused == "list" {
|
|
|
|
i, ok := m.list.SelectedItem().(post.Post)
|
|
|
|
if ok {
|
2023-01-01 20:13:02 -05:00
|
|
|
m.ctx.Loading = true
|
2022-12-31 20:58:45 -05:00
|
|
|
cmds = append(cmds, m.loadItem(&i))
|
|
|
|
}
|
2023-01-01 20:13:02 -05:00
|
|
|
} else if m.focused == "post" {
|
|
|
|
m.focused = "reply"
|
2023-01-01 20:53:28 -05:00
|
|
|
|
2023-01-01 21:22:16 -05:00
|
|
|
m.ctx.Logger.Debugln("caching view")
|
|
|
|
m.ctx.Logger.Debugf("buffer: %s", m.buffer)
|
|
|
|
m.viewcache = m.buildView(false)
|
2023-01-01 20:53:28 -05:00
|
|
|
|
2023-01-01 20:13:02 -05:00
|
|
|
return m, m.textarea.Focus()
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
2022-12-30 21:15:25 -05:00
|
|
|
|
2023-01-01 20:53:28 -05:00
|
|
|
case key.Matches(msg, m.keymap.Esc), key.Matches(msg, m.keymap.Quit):
|
2023-01-01 20:13:02 -05:00
|
|
|
if m.focused == "list" {
|
|
|
|
return m, tea.Quit
|
|
|
|
} else if m.focused == "post" {
|
2022-12-31 21:09:43 -05:00
|
|
|
// Let's make sure we reset the texarea
|
|
|
|
m.textarea.Reset()
|
2022-12-31 17:24:22 -05:00
|
|
|
m.focused = "list"
|
2022-12-30 21:15:25 -05:00
|
|
|
return m, nil
|
2023-01-01 20:53:28 -05:00
|
|
|
} else if m.focused == "reply" && key.Matches(msg, m.keymap.Esc) {
|
2022-12-31 20:58:45 -05:00
|
|
|
m.focused = "post"
|
2023-01-01 03:13:08 -05:00
|
|
|
m.buffer = ""
|
2022-12-31 20:58:45 -05:00
|
|
|
return m, nil
|
2022-12-30 21:15:25 -05:00
|
|
|
}
|
2023-01-01 03:13:08 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
switch msg.String() {
|
|
|
|
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
|
|
|
|
if m.focused == "post" {
|
|
|
|
m.buffer += msg.String()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if m.focused != "reply" {
|
|
|
|
m.buffer = ""
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2022-12-31 17:18:53 -05:00
|
|
|
m.ctx.Theme.PostsList.List.Focused.Width(listWidth)
|
|
|
|
m.ctx.Theme.PostsList.List.Blurred.Width(listWidth)
|
|
|
|
m.ctx.Theme.PostsList.List.Focused.Height(listHeight)
|
|
|
|
m.ctx.Theme.PostsList.List.Blurred.Height(listHeight)
|
2022-12-28 22:22:36 -05:00
|
|
|
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
|
2022-12-31 20:58:45 -05:00
|
|
|
m.viewport.Height = viewportHeight + 1
|
2022-12-28 22:22:36 -05:00
|
|
|
// cmds = append(cmds, viewport.Sync(m.viewport))
|
|
|
|
|
|
|
|
case []list.Item:
|
|
|
|
m.items = msg
|
|
|
|
m.list.SetItems(m.items)
|
|
|
|
m.ctx.Loading = false
|
2023-01-01 20:13:02 -05:00
|
|
|
return m, nil
|
2022-12-31 13:42:30 -05:00
|
|
|
|
|
|
|
case *post.Post:
|
|
|
|
m.viewport.SetContent(m.renderViewport(msg))
|
|
|
|
m.ctx.Loading = false
|
|
|
|
return m, nil
|
|
|
|
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
2022-12-31 17:24:22 -05:00
|
|
|
if m.focused == "list" {
|
2022-12-31 17:18:53 -05:00
|
|
|
m.list, cmd = m.list.Update(msg)
|
2022-12-31 17:24:22 -05:00
|
|
|
} else if m.focused == "post" {
|
|
|
|
m.viewport, cmd = m.viewport.Update(msg)
|
2022-12-31 20:58:45 -05:00
|
|
|
} else if m.focused == "reply" {
|
|
|
|
if !m.textarea.Focused() {
|
|
|
|
cmds = append(cmds, m.textarea.Focus())
|
|
|
|
}
|
|
|
|
m.textarea, cmd = m.textarea.Update(msg)
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|
2022-12-31 17:18:53 -05:00
|
|
|
cmds = append(cmds, cmd)
|
2022-12-28 22:22:36 -05:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
2023-01-01 21:22:16 -05:00
|
|
|
return m.buildView(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) buildView(cached bool) string {
|
2022-12-30 21:15:25 -05:00
|
|
|
var view strings.Builder = strings.Builder{}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2023-01-01 21:22:16 -05:00
|
|
|
if cached && m.focused == "reply" && m.viewcache != "" {
|
2023-01-01 20:53:28 -05:00
|
|
|
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-01 20:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m.ctx.Logger.Debugln("View()")
|
2022-12-31 17:18:53 -05:00
|
|
|
var l string = ""
|
2022-12-31 17:24:22 -05:00
|
|
|
if m.focused == "list" {
|
2022-12-31 17:18:53 -05:00
|
|
|
l = m.ctx.Theme.PostsList.List.Focused.Render(m.list.View())
|
2022-12-31 17:24:22 -05:00
|
|
|
} else {
|
|
|
|
l = m.ctx.Theme.PostsList.List.Blurred.Render(m.list.View())
|
2022-12-31 17:18:53 -05:00
|
|
|
}
|
2022-12-30 21:15:25 -05:00
|
|
|
view.WriteString(lipgloss.JoinHorizontal(
|
2022-12-28 22:22:36 -05:00
|
|
|
lipgloss.Top,
|
2022-12-31 17:18:53 -05:00
|
|
|
l,
|
2022-12-30 21:15:25 -05:00
|
|
|
))
|
|
|
|
|
2022-12-31 20:58:45 -05:00
|
|
|
if m.focused == "post" || m.focused == "reply" {
|
2023-01-01 01:42:42 -05:00
|
|
|
var style lipgloss.Style
|
|
|
|
if m.focused == "post" {
|
|
|
|
style = m.ctx.Theme.DialogBox.Titlebar.Focused
|
|
|
|
} else {
|
|
|
|
style = m.ctx.Theme.DialogBox.Titlebar.Blurred
|
|
|
|
}
|
|
|
|
titlebar := style.Align(lipgloss.Center).
|
2022-12-31 11:55:06 -05:00
|
|
|
Width(m.viewport.Width + 4).
|
|
|
|
Render("Post")
|
|
|
|
|
2022-12-31 16:57:11 -05:00
|
|
|
bottombar := m.ctx.Theme.DialogBox.Bottombar.
|
2022-12-31 11:55:06 -05:00
|
|
|
Width(m.viewport.Width + 4).
|
2023-01-01 03:13:08 -05:00
|
|
|
Render("[#]r reply · esc close")
|
2022-12-30 21:15:25 -05:00
|
|
|
|
|
|
|
ui := lipgloss.JoinVertical(
|
|
|
|
lipgloss.Center,
|
2022-12-31 11:55:06 -05:00
|
|
|
titlebar,
|
2022-12-30 21:15:25 -05:00
|
|
|
viewportStyle.Render(m.viewport.View()),
|
2022-12-31 11:55:06 -05:00
|
|
|
bottombar,
|
2022-12-30 21:15:25 -05:00
|
|
|
)
|
|
|
|
|
2023-01-01 01:42:42 -05:00
|
|
|
var tmp string
|
|
|
|
if m.focused == "post" {
|
|
|
|
tmp = helpers.PlaceOverlay(3, 2,
|
|
|
|
m.ctx.Theme.DialogBox.Window.Focused.Render(ui),
|
|
|
|
view.String(), true)
|
|
|
|
} else {
|
|
|
|
tmp = helpers.PlaceOverlay(3, 2,
|
|
|
|
m.ctx.Theme.DialogBox.Window.Blurred.Render(ui),
|
|
|
|
view.String(), true)
|
|
|
|
}
|
2022-12-31 20:58:45 -05:00
|
|
|
|
|
|
|
view = strings.Builder{}
|
|
|
|
view.WriteString(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.focused == "reply" {
|
2023-01-01 03:13:08 -05:00
|
|
|
title := "Reply"
|
|
|
|
if m.buffer != "" && m.buffer != "0" {
|
|
|
|
title += " to reply #" + m.buffer
|
|
|
|
}
|
2023-01-01 01:42:42 -05:00
|
|
|
titlebar := m.ctx.Theme.DialogBox.Titlebar.Focused.
|
2022-12-31 20:58:45 -05:00
|
|
|
Align(lipgloss.Center).
|
|
|
|
Width(m.viewport.Width - 2).
|
2023-01-01 03:13:08 -05:00
|
|
|
Render(title)
|
2022-12-31 20:58:45 -05:00
|
|
|
|
2023-01-01 21:12:02 -05:00
|
|
|
textareaWidth := m.viewport.Width - 2
|
|
|
|
textareaHeight := 6
|
|
|
|
m.textarea.SetWidth(textareaWidth)
|
|
|
|
m.textarea.SetHeight(textareaHeight)
|
2022-12-31 20:58:45 -05:00
|
|
|
|
|
|
|
bottombar := m.ctx.Theme.DialogBox.Bottombar.
|
|
|
|
Width(m.viewport.Width - 2).
|
2023-01-01 01:42:42 -05:00
|
|
|
Render("ctrl+enter reply · esc close")
|
2022-12-31 20:58:45 -05:00
|
|
|
|
|
|
|
replyWindow := lipgloss.JoinVertical(
|
|
|
|
lipgloss.Center,
|
|
|
|
titlebar,
|
|
|
|
m.textarea.View(),
|
|
|
|
bottombar,
|
|
|
|
)
|
|
|
|
|
2023-01-01 21:12:02 -05:00
|
|
|
replyWindowX := 5
|
|
|
|
replyWindowY := m.ctx.Screen[1] - 21
|
|
|
|
|
|
|
|
tmp := helpers.PlaceOverlay(replyWindowX, replyWindowY,
|
2023-01-01 01:42:42 -05:00
|
|
|
m.ctx.Theme.DialogBox.Window.Focused.Render(replyWindow),
|
2022-12-31 20:58:45 -05:00
|
|
|
view.String(), true)
|
|
|
|
|
2023-01-01 21:12:02 -05:00
|
|
|
m.viewcacheTextareaXY[0] = replyWindowX + 1
|
|
|
|
m.viewcacheTextareaXY[1] = replyWindowY + 2
|
|
|
|
m.viewcacheTextareaXY[2] = textareaWidth
|
|
|
|
m.viewcacheTextareaXY[3] = textareaHeight
|
2023-01-01 20:53:28 -05:00
|
|
|
|
2022-12-31 20:58:45 -05:00
|
|
|
view = strings.Builder{}
|
|
|
|
view.WriteString(tmp)
|
2022-12-30 21:15:25 -05:00
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2023-01-01 20:13:02 -05:00
|
|
|
m.viewcache = view.String()
|
|
|
|
return m.viewcache
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 13:42:30 -05:00
|
|
|
func (m *Model) loadItem(p *post.Post) tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
m.a.LoadPost(p)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 22:22:36 -05:00
|
|
|
|
2022-12-31 13:42:30 -05:00
|
|
|
func (m *Model) renderViewport(p *post.Post) string {
|
|
|
|
var out string = ""
|
2022-12-30 03:30:03 -05:00
|
|
|
|
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"
|
2022-12-31 13:42:30 -05:00
|
|
|
if p.Subject[len(p.Subject)-1:] == "?" {
|
2022-12-30 22:07:09 -05:00
|
|
|
adj = "asks"
|
|
|
|
}
|
|
|
|
|
2022-12-31 13:42:30 -05:00
|
|
|
body, err := m.glam.Render(p.Body)
|
2022-12-30 21:15:25 -05:00
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
2022-12-31 13:42:30 -05:00
|
|
|
body = p.Body
|
2022-12-30 22:07:09 -05:00
|
|
|
}
|
2022-12-31 13:42:30 -05:00
|
|
|
out += fmt.Sprintf(
|
2023-01-01 01:23:46 -05:00
|
|
|
" %s\n\n %s\n%s",
|
2022-12-31 16:57:11 -05:00
|
|
|
m.ctx.Theme.Post.Author.Render(
|
2022-12-31 13:42:30 -05:00
|
|
|
fmt.Sprintf("%s %s:", p.Author.Name, adj),
|
2022-12-30 22:07:09 -05:00
|
|
|
),
|
2022-12-31 16:57:11 -05:00
|
|
|
m.ctx.Theme.Post.Subject.Render(p.Subject),
|
2022-12-30 22:07:09 -05:00
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
2023-01-01 03:13:08 -05:00
|
|
|
m.replyIDs = []string{p.ID}
|
|
|
|
out += m.renderReplies(0, p.Author.Name, &p.Replies)
|
2022-12-31 13:42:30 -05:00
|
|
|
|
2022-12-31 17:24:22 -05:00
|
|
|
m.focused = "post"
|
2022-12-31 13:42:30 -05:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) renderReplies(
|
|
|
|
level int,
|
|
|
|
inReplyTo string,
|
|
|
|
replies *[]reply.Reply,
|
|
|
|
) string {
|
|
|
|
var out string = ""
|
|
|
|
|
|
|
|
if replies == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, re := range *replies {
|
|
|
|
var err error = nil
|
|
|
|
var body string = ""
|
|
|
|
var author string = ""
|
|
|
|
|
|
|
|
if re.Deleted {
|
2022-12-31 20:58:45 -05:00
|
|
|
body = "\n DELETED\n\n"
|
2022-12-31 13:42:30 -05:00
|
|
|
author = "DELETED"
|
|
|
|
} else {
|
|
|
|
body, err = m.glam.Render(re.Body)
|
|
|
|
if err != nil {
|
|
|
|
m.ctx.Logger.Error(err)
|
|
|
|
body = re.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
author = re.Author.Name
|
2022-12-30 22:07:09 -05:00
|
|
|
}
|
2023-01-01 03:13:08 -05:00
|
|
|
|
|
|
|
m.replyIDs = append(m.replyIDs, re.ID)
|
|
|
|
idx := len(m.replyIDs) - 1
|
|
|
|
|
2022-12-31 13:42:30 -05:00
|
|
|
out += fmt.Sprintf(
|
2023-01-01 01:23:46 -05:00
|
|
|
"\n\n %s %s%s%s\n%s",
|
2022-12-31 16:57:11 -05:00
|
|
|
m.ctx.Theme.Reply.Author.Render(
|
2022-12-31 13:42:30 -05:00
|
|
|
author,
|
|
|
|
),
|
2022-12-31 20:58:45 -05:00
|
|
|
lipgloss.NewStyle().
|
|
|
|
Foreground(m.ctx.Theme.Reply.Author.GetBackground()).
|
|
|
|
Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)),
|
2023-01-01 01:42:42 -05:00
|
|
|
strings.Repeat(" ", (m.viewport.Width-len(author)-len(inReplyTo)-28)),
|
2023-01-01 01:23:46 -05:00
|
|
|
lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("#777777")).
|
|
|
|
Render(fmt.Sprintf("#%d", idx)),
|
2022-12-30 22:07:09 -05:00
|
|
|
body,
|
|
|
|
)
|
2022-12-31 13:42:30 -05:00
|
|
|
|
2023-01-01 01:23:46 -05:00
|
|
|
idx++
|
2023-01-01 03:13:08 -05:00
|
|
|
out += m.renderReplies(level+1, re.Author.Name, &re.Replies)
|
2022-12-30 21:15:25 -05:00
|
|
|
}
|
|
|
|
|
2022-12-31 13:42:30 -05:00
|
|
|
return out
|
2022-12-28 22:22:36 -05:00
|
|
|
}
|