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

Refactored dialog/window handling, impl draft

This commit is contained in:
マリウス 2023-01-02 13:25:09 -05:00
parent 2f036b9bcc
commit 3792b4078e
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
4 changed files with 450 additions and 389 deletions

View File

@ -5,9 +5,12 @@ import (
"strings" "strings"
"github.com/mrusme/gobbs/ui/cmd"
"github.com/mrusme/gobbs/ui/ctx" "github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/header" "github.com/mrusme/gobbs/ui/header"
"github.com/mrusme/gobbs/ui/views/posts" "github.com/mrusme/gobbs/ui/views/posts"
"github.com/mrusme/gobbs/ui/windowmanager"
"github.com/mrusme/gobbs/ui/windows/postdialog"
"github.com/mrusme/gobbs/ui/views" "github.com/mrusme/gobbs/ui/views"
@ -18,7 +21,7 @@ import (
type KeyMap struct { type KeyMap struct {
Up key.Binding Up key.Binding
Down key.Binding Down key.Binding
Quit key.Binding Close key.Binding
} }
var DefaultKeyMap = KeyMap{ var DefaultKeyMap = KeyMap{
@ -30,10 +33,10 @@ var DefaultKeyMap = KeyMap{
// key.WithKeys("j", "down"), // key.WithKeys("j", "down"),
// key.WithHelp("↓/j", "move down"), // key.WithHelp("↓/j", "move down"),
// ), // ),
// Quit: key.NewBinding( Close: key.NewBinding(
// key.WithKeys("q", "ctrl+q", "escape"), key.WithKeys("q", "esc"),
// key.WithHelp("q/esc", "quit"), key.WithHelp("q/esc", "close"),
// ), ),
} }
type Model struct { type Model struct {
@ -41,6 +44,7 @@ type Model struct {
header header.Model header header.Model
views []views.View views []views.View
currentView int currentView int
wm *windowmanager.WM
ctx *ctx.Ctx ctx *ctx.Ctx
} }
@ -48,6 +52,7 @@ func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
keymap: DefaultKeyMap, keymap: DefaultKeyMap,
currentView: 0, currentView: 0,
wm: windowmanager.New(),
ctx: c, ctx: c,
} }
@ -58,7 +63,11 @@ func NewModel(c *ctx.Ctx) Model {
} }
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
return tea.Batch(tea.EnterAltScreen) return tea.Batch(
tea.EnterAltScreen,
cmd.New(cmd.ViewFocus, "*").Tea(),
cmd.New(cmd.ViewRefreshData, "*").Tea(),
)
} }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -67,8 +76,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
// case key.Matches(msg, m.keymap.Quit): case key.Matches(msg, m.keymap.Close):
// return m, tea.Quit 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
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
@ -78,15 +92,39 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.views[i] = v m.views[i] = v
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
} }
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},
)
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
default:
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)
ccmds = m.wm.UpdateAll(msg)
} }
v, cmd := m.views[m.currentView].Update(msg) cmds = append(cmds, ccmds...)
m.views[m.currentView] = v }
cmds = append(cmds, cmd)
header, cmd := m.header.Update(msg) v, vcmd := m.views[m.currentView].Update(msg)
m.views[m.currentView] = v
cmds = append(cmds, vcmd)
header, hcmd := m.header.Update(msg)
m.header = header m.header = header
cmds = append(cmds, cmd) cmds = append(cmds, hcmd)
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }
@ -95,7 +133,8 @@ func (m Model) View() string {
s := strings.Builder{} s := strings.Builder{}
s.WriteString(m.header.View() + "\n") s.WriteString(m.header.View() + "\n")
s.WriteString(m.views[m.currentView].View()) s.WriteString(m.views[m.currentView].View())
return s.String()
return m.wm.View(s.String())
} }
func (m Model) setSizes(winWidth int, winHeight int) { func (m Model) setSizes(winWidth int, winHeight int) {

View File

@ -2,7 +2,6 @@ package posts
import ( import (
"fmt" "fmt"
"strconv"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
@ -14,6 +13,7 @@ import (
"github.com/mrusme/gobbs/aggregator" "github.com/mrusme/gobbs/aggregator"
"github.com/mrusme/gobbs/models/post" "github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply" "github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/cmd"
"github.com/mrusme/gobbs/ui/ctx" "github.com/mrusme/gobbs/ui/ctx"
) )
@ -30,8 +30,8 @@ var (
type KeyMap struct { type KeyMap struct {
Refresh key.Binding Refresh key.Binding
Select key.Binding Select key.Binding
Esc key.Binding // Esc key.Binding
Quit key.Binding // Quit key.Binding
Reply key.Binding Reply key.Binding
} }
@ -44,13 +44,13 @@ var DefaultKeyMap = KeyMap{
key.WithKeys("r", "enter"), key.WithKeys("r", "enter"),
key.WithHelp("r/enter", "read"), key.WithHelp("r/enter", "read"),
), ),
Esc: key.NewBinding( // Esc: key.NewBinding(
key.WithKeys("esc"), // key.WithKeys("esc"),
key.WithHelp("esc", "close"), // key.WithHelp("esc", "close"),
), // ),
Quit: key.NewBinding( // Quit: key.NewBinding(
key.WithKeys("q"), // key.WithKeys("q"),
), // ),
Reply: key.NewBinding( Reply: key.NewBinding(
key.WithKeys("ctrl+s"), key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "reply"), key.WithHelp("ctrl+s", "reply"),
@ -60,6 +60,7 @@ var DefaultKeyMap = KeyMap{
type Model struct { type Model struct {
ctx *ctx.Ctx ctx *ctx.Ctx
keymap KeyMap keymap KeyMap
focused bool
list list.Model list list.Model
items []list.Item items []list.Item
viewport viewport.Model viewport viewport.Model
@ -68,7 +69,7 @@ type Model struct {
a *aggregator.Aggregator a *aggregator.Aggregator
glam *glamour.TermRenderer glam *glamour.TermRenderer
wm []string // wm []string
buffer string buffer string
replyIDs []string replyIDs []string
@ -91,8 +92,9 @@ func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
ctx: c, ctx: c,
keymap: DefaultKeyMap, keymap: DefaultKeyMap,
focused: false,
wm: []string{WM_ROOT_ID}, // wm: []string{WM_ROOT_ID},
buffer: "", buffer: "",
replyIDs: []string{}, replyIDs: []string{},
@ -130,117 +132,122 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch { switch {
case key.Matches(msg, m.keymap.Refresh): case key.Matches(msg, m.keymap.Refresh):
if m.WMisFocused("list") { // if m.WMisFocused("list") {
m.ctx.Loading = true m.ctx.Loading = true
cmds = append(cmds, m.refresh()) cmds = append(cmds, m.refresh())
} // }
case key.Matches(msg, m.keymap.Select): case key.Matches(msg, m.keymap.Select):
switch m.WMFocused() { // switch m.WMFocused() {
//
case "list": // case "list":
i, ok := m.list.SelectedItem().(post.Post) i, ok := m.list.SelectedItem().(post.Post)
if ok { if ok {
m.ctx.Loading = true // m.ctx.Loading = true
cmds = append(cmds, m.loadItem(&i)) // cmds = append(cmds, m.loadItem(&i))
cmd := cmd.New(cmd.WinOpen, "post", cmd.Arg{
Name: "post",
Value: &i,
})
cmds = append(cmds, cmd.Tea())
} }
//
// case "post":
// if m.buffer != "" {
// replyToID, err := strconv.Atoi(m.buffer)
// if err != nil {
// // TODO: Handle error
// }
//
// if replyToID >= len(m.replyIDs) {
// // TODO: Handle error
// }
// }
// m.WMOpen("reply")
//
// m.ctx.Logger.Debugln("caching view")
// m.ctx.Logger.Debugf("buffer: %s", m.buffer)
// m.viewcache = m.buildView(false)
//
// return m, m.textarea.Focus()
// }
case "post": // case key.Matches(msg, m.keymap.Esc), key.Matches(msg, m.keymap.Quit):
if m.buffer != "" { // switch m.WMFocused() {
replyToID, err := strconv.Atoi(m.buffer) //
if err != nil { // case "list":
// TODO: Handle error // return m, tea.Quit
} //
// case "post":
if replyToID >= len(m.replyIDs) { // // Let's make sure we reset the texarea
// TODO: Handle error // m.textarea.Reset()
} // m.WMClose("post")
} // return m, nil
m.WMOpen("reply") //
// case "reply":
m.ctx.Logger.Debugln("caching view") // if key.Matches(msg, m.keymap.Esc) {
m.ctx.Logger.Debugf("buffer: %s", m.buffer) // m.buffer = ""
m.viewcache = m.buildView(false) // m.WMClose("reply")
// return m, nil
return m, m.textarea.Focus() // }
} // }
case key.Matches(msg, m.keymap.Esc), key.Matches(msg, m.keymap.Quit):
switch m.WMFocused() {
case "list":
return m, tea.Quit
case "post":
// Let's make sure we reset the texarea
m.textarea.Reset()
m.WMClose("post")
return m, nil
case "reply":
if key.Matches(msg, m.keymap.Esc) {
m.buffer = ""
m.WMClose("reply")
return m, nil
}
}
case key.Matches(msg, m.keymap.Reply): case key.Matches(msg, m.keymap.Reply):
if m.WMisFocused("reply") { // if m.WMisFocused("reply") {
replyToIdx, _ := strconv.Atoi(m.buffer) // replyToIdx, _ := strconv.Atoi(m.buffer)
//
// m.ctx.Logger.Debugf("replyToIdx: %d", replyToIdx)
//
// var irtID string = ""
// var irtIRT string = ""
// var irtSysIDX int = 0
//
// if replyToIdx == 0 {
// irtID = m.activePost.ID
// irtSysIDX = m.activePost.SysIDX
// } else {
// irt := m.allReplies[(replyToIdx - 1)]
// irtID = strconv.Itoa(replyToIdx + 1)
// irtIRT = irt.InReplyTo
// irtSysIDX = irt.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)
// }
//
// m.textarea.Reset()
// m.buffer = ""
// m.WMClose("reply")
// return m, nil
// }
m.ctx.Logger.Debugf("replyToIdx: %d", replyToIdx) // default:
// switch msg.String() {
var irtID string = "" // case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
var irtIRT string = "" // if m.WMisFocused("post") {
var irtSysIDX int = 0 // m.buffer += msg.String()
// return m, nil
if replyToIdx == 0 { // }
irtID = m.activePost.ID // default:
irtSysIDX = m.activePost.SysIDX // if m.WMFocused() != "reply" {
} else { // m.buffer = ""
irt := m.allReplies[(replyToIdx - 1)] // }
irtID = strconv.Itoa(replyToIdx + 1) // }
irtIRT = irt.InReplyTo
irtSysIDX = irt.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)
}
m.textarea.Reset()
m.buffer = ""
m.WMClose("reply")
return m, nil
}
default:
switch msg.String() {
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
if m.WMisFocused("post") {
m.buffer += msg.String()
return m, nil
}
default:
if m.WMFocused() != "reply" {
m.buffer = ""
}
}
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
listWidth := m.ctx.Content[0] - 2 listWidth := m.ctx.Content[0] - 2
listHeight := m.ctx.Content[1] - 1 listHeight := m.ctx.Content[1] - 1
viewportWidth := m.ctx.Content[0] - 9 // viewportWidth := m.ctx.Content[0] - 9
viewportHeight := m.ctx.Content[1] - 10 // viewportHeight := m.ctx.Content[1] - 10
m.ctx.Theme.PostsList.List.Focused.Width(listWidth) m.ctx.Theme.PostsList.List.Focused.Width(listWidth)
m.ctx.Theme.PostsList.List.Blurred.Width(listWidth) m.ctx.Theme.PostsList.List.Blurred.Width(listWidth)
@ -251,12 +258,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
listHeight-2, listHeight-2,
) )
viewportStyle.Width(viewportWidth) // viewportStyle.Width(viewportWidth)
viewportStyle.Height(viewportHeight) // viewportStyle.Height(viewportHeight)
m.viewport = viewport.New(viewportWidth-4, viewportHeight-4) // m.viewport = viewport.New(viewportWidth-4, viewportHeight-4)
m.viewport.Width = viewportWidth - 4 // m.viewport.Width = viewportWidth - 4
m.viewport.Height = viewportHeight + 1 // m.viewport.Height = viewportHeight + 1
// cmds = append(cmds, viewport.Sync(m.viewport)) // // cmds = append(cmds, viewport.Sync(m.viewport))
case []list.Item: case []list.Item:
m.items = msg m.items = msg
@ -264,28 +271,47 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.Loading = false m.ctx.Loading = false
return m, nil return m, nil
case *post.Post: // case *post.Post:
m.viewport.SetContent(m.renderViewport(msg)) // m.viewport.SetContent(m.renderViewport(msg))
m.WMOpen("post") // m.WMOpen("post")
m.ctx.Loading = false // m.ctx.Loading = false
// return m, nil
case cmd.Command:
switch msg.Call {
case cmd.ViewFocus:
if msg.Target == "*" {
m.focused = true
}
return m, nil return m, nil
case cmd.ViewBlur:
if msg.Target == "*" {
m.focused = false
}
return m, nil
case cmd.ViewRefreshData:
if msg.Target == "*" {
m.ctx.Loading = true
cmds = append(cmds, m.refresh())
}
}
} }
var cmd tea.Cmd var lcmd tea.Cmd
switch m.WMFocused() { // switch m.WMFocused() {
case "list": // case "list":
m.list, cmd = m.list.Update(msg) m.list, lcmd = m.list.Update(msg)
case "post": // case "post":
m.viewport, cmd = m.viewport.Update(msg) // m.viewport, lcmd = m.viewport.Update(msg)
case "reply": // case "reply":
if !m.textarea.Focused() { // if !m.textarea.Focused() {
cmds = append(cmds, m.textarea.Focus()) // cmds = append(cmds, m.textarea.Focus())
} // }
m.textarea, cmd = m.textarea.Update(msg) // m.textarea, lcmd = m.textarea.Update(msg)
} // }
cmds = append(cmds, cmd) cmds = append(cmds, lcmd)
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }

View File

@ -1,14 +1,9 @@
package posts package posts
import ( import (
"fmt"
"strings" "strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/mrusme/gobbs/models/post"
"github.com/mrusme/gobbs/models/reply"
"github.com/mrusme/gobbs/ui/helpers"
) )
func (m Model) View() string { func (m Model) View() string {
@ -18,21 +13,21 @@ func (m Model) View() string {
func (m Model) buildView(cached bool) string { func (m Model) buildView(cached bool) string {
var view strings.Builder = strings.Builder{} var view strings.Builder = strings.Builder{}
if cached && m.WMisFocused("reply") && m.viewcache != "" { // if cached && m.WMisFocused("reply") && m.viewcache != "" {
m.ctx.Logger.Debugln("Cached View()") // m.ctx.Logger.Debugln("Cached View()")
//
m.textarea.SetWidth(m.viewcacheTextareaXY[2]) // m.textarea.SetWidth(m.viewcacheTextareaXY[2])
m.textarea.SetHeight(m.viewcacheTextareaXY[3]) // m.textarea.SetHeight(m.viewcacheTextareaXY[3])
//
return helpers.PlaceOverlay( // return helpers.PlaceOverlay(
m.viewcacheTextareaXY[0], m.viewcacheTextareaXY[1], // m.viewcacheTextareaXY[0], m.viewcacheTextareaXY[1],
m.textarea.View(), m.viewcache, // m.textarea.View(), m.viewcache,
false) // false)
} // }
//
m.ctx.Logger.Debugln("View()") m.ctx.Logger.Debugln("Posts.View()")
var l string = "" var l string = ""
if m.WMisFocused("list") { if m.focused {
l = m.ctx.Theme.PostsList.List.Focused.Render(m.list.View()) l = m.ctx.Theme.PostsList.List.Focused.Render(m.list.View())
} else { } else {
l = m.ctx.Theme.PostsList.List.Blurred.Render(m.list.View()) l = m.ctx.Theme.PostsList.List.Blurred.Render(m.list.View())
@ -42,179 +37,179 @@ func (m Model) buildView(cached bool) string {
l, l,
)) ))
if m.WMisOpen("post") { // if m.WMisOpen("post") {
var style lipgloss.Style // var style lipgloss.Style
if m.WMisFocused("post") { // if m.WMisFocused("post") {
style = m.ctx.Theme.DialogBox.Titlebar.Focused // style = m.ctx.Theme.DialogBox.Titlebar.Focused
} else { // } else {
style = m.ctx.Theme.DialogBox.Titlebar.Blurred // style = m.ctx.Theme.DialogBox.Titlebar.Blurred
} // }
titlebar := style.Align(lipgloss.Center). // titlebar := style.Align(lipgloss.Center).
Width(m.viewport.Width + 4). // Width(m.viewport.Width + 4).
Render("Post") // Render("Post")
//
bottombar := m.ctx.Theme.DialogBox.Bottombar. // bottombar := m.ctx.Theme.DialogBox.Bottombar.
Width(m.viewport.Width + 4). // Width(m.viewport.Width + 4).
Render("[#]r reply · esc close") // Render("[#]r reply · esc close")
//
ui := lipgloss.JoinVertical( // ui := lipgloss.JoinVertical(
lipgloss.Center, // lipgloss.Center,
titlebar, // titlebar,
viewportStyle.Render(m.viewport.View()), // viewportStyle.Render(m.viewport.View()),
bottombar, // bottombar,
) // )
//
var tmp string // var tmp string
if m.WMisFocused("post") { // if m.WMisFocused("post") {
tmp = helpers.PlaceOverlay(3, 2, // tmp = helpers.PlaceOverlay(3, 2,
m.ctx.Theme.DialogBox.Window.Focused.Render(ui), // m.ctx.Theme.DialogBox.Window.Focused.Render(ui),
view.String(), true) // view.String(), true)
} else { // } else {
tmp = helpers.PlaceOverlay(3, 2, // tmp = helpers.PlaceOverlay(3, 2,
m.ctx.Theme.DialogBox.Window.Blurred.Render(ui), // m.ctx.Theme.DialogBox.Window.Blurred.Render(ui),
view.String(), true) // view.String(), true)
} // }
//
view = strings.Builder{} // view = strings.Builder{}
view.WriteString(tmp) // view.WriteString(tmp)
} // }
//
if m.WMisOpen("reply") { // if m.WMisOpen("reply") {
title := "Reply" // title := "Reply"
if m.buffer != "" && m.buffer != "0" { // if m.buffer != "" && m.buffer != "0" {
title += " to reply #" + m.buffer // title += " to reply #" + m.buffer
} // }
titlebar := m.ctx.Theme.DialogBox.Titlebar.Focused. // titlebar := m.ctx.Theme.DialogBox.Titlebar.Focused.
Align(lipgloss.Center). // Align(lipgloss.Center).
Width(m.viewport.Width - 2). // Width(m.viewport.Width - 2).
Render(title) // Render(title)
//
textareaWidth := m.viewport.Width - 2 // textareaWidth := m.viewport.Width - 2
textareaHeight := 6 // textareaHeight := 6
m.textarea.SetWidth(textareaWidth) // m.textarea.SetWidth(textareaWidth)
m.textarea.SetHeight(textareaHeight) // m.textarea.SetHeight(textareaHeight)
//
bottombar := m.ctx.Theme.DialogBox.Bottombar. // bottombar := m.ctx.Theme.DialogBox.Bottombar.
Width(m.viewport.Width - 2). // Width(m.viewport.Width - 2).
Render("ctrl+enter reply · esc close") // Render("ctrl+enter reply · esc close")
//
replyWindow := lipgloss.JoinVertical( // replyWindow := lipgloss.JoinVertical(
lipgloss.Center, // lipgloss.Center,
titlebar, // titlebar,
m.textarea.View(), // m.textarea.View(),
bottombar, // bottombar,
) // )
//
replyWindowX := 5 // replyWindowX := 5
replyWindowY := m.ctx.Screen[1] - 21 // replyWindowY := m.ctx.Screen[1] - 21
//
tmp := helpers.PlaceOverlay(replyWindowX, replyWindowY, // tmp := helpers.PlaceOverlay(replyWindowX, replyWindowY,
m.ctx.Theme.DialogBox.Window.Focused.Render(replyWindow), // m.ctx.Theme.DialogBox.Window.Focused.Render(replyWindow),
view.String(), true) // view.String(), true)
//
m.viewcacheTextareaXY[0] = replyWindowX + 1 // m.viewcacheTextareaXY[0] = replyWindowX + 1
m.viewcacheTextareaXY[1] = replyWindowY + 2 // m.viewcacheTextareaXY[1] = replyWindowY + 2
m.viewcacheTextareaXY[2] = textareaWidth // m.viewcacheTextareaXY[2] = textareaWidth
m.viewcacheTextareaXY[3] = textareaHeight // m.viewcacheTextareaXY[3] = textareaHeight
//
view = strings.Builder{} // view = strings.Builder{}
view.WriteString(tmp) // view.WriteString(tmp)
} // }
m.viewcache = view.String() m.viewcache = view.String()
return m.viewcache return m.viewcache
} }
func (m *Model) renderViewport(p *post.Post) string { // func (m *Model) renderViewport(p *post.Post) string {
var out string = "" // var out string = ""
//
var err error // var err error
m.glam, err = glamour.NewTermRenderer( // m.glam, err = glamour.NewTermRenderer(
glamour.WithAutoStyle(), // glamour.WithAutoStyle(),
glamour.WithWordWrap(m.viewport.Width), // glamour.WithWordWrap(m.viewport.Width),
) // )
if err != nil { // if err != nil {
m.ctx.Logger.Error(err) // m.ctx.Logger.Error(err)
m.glam = nil // m.glam = nil
} // }
//
adj := "writes" // adj := "writes"
if p.Subject[len(p.Subject)-1:] == "?" { // if p.Subject[len(p.Subject)-1:] == "?" {
adj = "asks" // adj = "asks"
} // }
//
body, err := m.glam.Render(p.Body) // body, err := m.glam.Render(p.Body)
if err != nil { // if err != nil {
m.ctx.Logger.Error(err) // m.ctx.Logger.Error(err)
body = p.Body // body = p.Body
} // }
out += fmt.Sprintf( // out += fmt.Sprintf(
" %s\n\n %s\n%s", // " %s\n\n %s\n%s",
m.ctx.Theme.Post.Author.Render( // m.ctx.Theme.Post.Author.Render(
fmt.Sprintf("%s %s:", p.Author.Name, adj), // fmt.Sprintf("%s %s:", p.Author.Name, adj),
), // ),
m.ctx.Theme.Post.Subject.Render(p.Subject), // m.ctx.Theme.Post.Subject.Render(p.Subject),
body, // body,
) // )
//
m.replyIDs = []string{p.ID} // m.replyIDs = []string{p.ID}
m.activePost = p // m.activePost = p
out += m.renderReplies(0, p.Author.Name, &p.Replies) // out += m.renderReplies(0, p.Author.Name, &p.Replies)
//
return out // return out
} // }
//
func (m *Model) renderReplies( // func (m *Model) renderReplies(
level int, // level int,
inReplyTo string, // inReplyTo string,
replies *[]reply.Reply, // replies *[]reply.Reply,
) string { // ) string {
var out string = "" // var out string = ""
//
if replies == nil { // if replies == nil {
return "" // return ""
} // }
//
for ri, re := range *replies { // for ri, re := range *replies {
var err error = nil // var err error = nil
var body string = "" // var body string = ""
var author string = "" // var author string = ""
//
if re.Deleted { // if re.Deleted {
body = "\n DELETED\n\n" // body = "\n DELETED\n\n"
author = "DELETED" // author = "DELETED"
} else { // } else {
body, err = m.glam.Render(re.Body) // body, err = m.glam.Render(re.Body)
if err != nil { // if err != nil {
m.ctx.Logger.Error(err) // m.ctx.Logger.Error(err)
body = re.Body // body = re.Body
} // }
//
author = re.Author.Name // author = re.Author.Name
} // }
//
m.replyIDs = append(m.replyIDs, re.ID) // m.replyIDs = append(m.replyIDs, re.ID)
m.allReplies = append(m.allReplies, &(*replies)[ri]) // m.allReplies = append(m.allReplies, &(*replies)[ri])
idx := len(m.replyIDs) - 1 // idx := len(m.replyIDs) - 1
//
out += fmt.Sprintf( // out += fmt.Sprintf(
"\n\n %s %s%s%s\n%s", // "\n\n %s %s%s%s\n%s",
m.ctx.Theme.Reply.Author.Render( // m.ctx.Theme.Reply.Author.Render(
author, // author,
), // ),
lipgloss.NewStyle(). // lipgloss.NewStyle().
Foreground(m.ctx.Theme.Reply.Author.GetBackground()). // Foreground(m.ctx.Theme.Reply.Author.GetBackground()).
Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)), // Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)),
strings.Repeat(" ", (m.viewport.Width-len(author)-len(inReplyTo)-28)), // strings.Repeat(" ", (m.viewport.Width-len(author)-len(inReplyTo)-28)),
lipgloss.NewStyle(). // lipgloss.NewStyle().
Foreground(lipgloss.Color("#777777")). // Foreground(lipgloss.Color("#777777")).
Render(fmt.Sprintf("#%d", idx)), // Render(fmt.Sprintf("#%d", idx)),
body, // body,
) // )
//
idx++ // idx++
out += m.renderReplies(level+1, re.Author.Name, &re.Replies) // out += m.renderReplies(level+1, re.Author.Name, &re.Replies)
} // }
//
return out // return out
} // }

View File

@ -1,47 +1,48 @@
package posts package posts
var WM_ROOT_ID = "list" //
// var WM_ROOT_ID = "list"
func (m *Model) WMOpen(id string) bool { //
if m.WMisOpen(id) { // func (m *Model) WMOpen(id string) bool {
if m.WMisFocused(id) { // if m.WMisOpen(id) {
return true // if m.WMisFocused(id) {
} // return true
return false // }
} // return false
// }
m.wm = append(m.wm, id) //
return true // m.wm = append(m.wm, id)
} // return true
// }
func (m *Model) WMCloseFocused() bool { //
return m.WMClose(m.WMFocused()) // func (m *Model) WMCloseFocused() bool {
} // return m.WMClose(m.WMFocused())
// }
func (m *Model) WMClose(id string) bool { //
for i := len(m.wm) - 1; i > 0; i-- { // func (m *Model) WMClose(id string) bool {
if m.wm[i] == id { // for i := len(m.wm) - 1; i > 0; i-- {
m.wm = append(m.wm[:i], m.wm[i+1:]...) // if m.wm[i] == id {
return true // m.wm = append(m.wm[:i], m.wm[i+1:]...)
} // return true
} // }
// }
return false //
} // return false
// }
func (m *Model) WMFocused() string { //
return m.wm[len(m.wm)-1] // func (m *Model) WMFocused() string {
} // return m.wm[len(m.wm)-1]
// }
func (m *Model) WMisOpen(id string) bool { //
for _, openID := range m.wm { // func (m *Model) WMisOpen(id string) bool {
if openID == id { // for _, openID := range m.wm {
return true // if openID == id {
} // return true
} // }
return false // }
} // return false
// }
func (m *Model) WMisFocused(id string) bool { //
return id == m.WMFocused() // func (m *Model) WMisFocused(id string) bool {
} // return id == m.WMFocused()
// }