ref: 3e8f569b8cd631f999757ce37e8d72076f64860c
parent: 32956bab7e8c63735cc6a36ab2f8ce5e7997003b
parent: 599e772a2db508d33a93f347802d917f6b9d4e06
author: penny64 <penny64@noreply.codeberg.org>
date: Sun Oct 5 22:04:15 EDT 2025
Merge pull request 'Convert streams to templates' (#6) from convert_to_templates into main Reviewed-on: https://codeberg.org/penny64/hellclient/pulls/6
--- a/mastodon.go
+++ b/mastodon.go
@@ -227,49 +227,18 @@
fmt.Printf("\r%v\n", err)}
-func (hc *Hellclient) printPostS(ref postref, post *mastodon.Status) *mastodon.Status {- return hc.printPostDetailed(ref.prefix+ref.ref, post, "")
-}
-
-func (hc *Hellclient) printPost(postref string, post *mastodon.Status) *mastodon.Status {- return hc.printPostDetailed(postref, post, "")
-}
-
-func (hc *Hellclient) printPostDetailed(postref string, post *mastodon.Status, prefix string) *mastodon.Status {- post, plaintext := hc.RenderPostPlaintext(post, postref, prefix)
- fmt.Println(hyphenate(plaintext))
- return post
-}
-
-func (hc *Hellclient) renderPostS(ref postref, post *mastodon.Status) string {- _, plaintext := hc.RenderPostPlaintext(post, ref.prefix+ref.ref, "")
- return hyphenate(plaintext)
-}
-
-func (hc *Hellclient) RenderPostPlaintext(post *mastodon.Status, postref string, prefix string) (selectedPost *mastodon.Status, plaintext string) {- poststring := ""
- postfix := ""
- var media []mastodon.Attachment
+func (hc *Hellclient) RenderPostPlaintext(post *mastodon.Status, ref postref) (selectedPost *mastodon.Status, plaintext string) { if post.Reblog != nil {- poststring = hc.formatReblog(post, postref)
selectedPost = post.Reblog
- media = post.Reblog.MediaAttachments
+ plaintext = fmt.Sprintf("$username Reblogged %s> $boostuser $boostcontent $boosted_media_descriptions", ref.ref) } else {- poststring = hc.formatStatusDetailed(post, postref, prefix)
selectedPost = post
- media = post.MediaAttachments
+ plaintext = fmt.Sprintf("%s> $username $content $media_descriptions", ref.ref)}
-
- for _, item := range media {- if item.Description != "" {- postfix += fmt.Sprintf("\n🖼️[%s]", item.Description)- continue
- }
- postfix += "🖼️"
- }
-
- plaintext = fmt.Sprintf("%s %s", poststring, postfix)- return
+ formatter := &StatusFormatter{hc: hc, status: post, postContext: &ref}+ templater := newStatusTemplateRenderer(formatter)
+ plaintext, _ = templater.render(plaintext)
+ return selectedPost, plaintext
}
func StreamHomeTimeline(client *mastodon.Client, postMap map[string]*mastodon.Status, hc *Hellclient) {@@ -301,10 +270,11 @@
//Tell the timeline marker updater the most recent post
readchan <- &post.Status.ID
if hc.isPaused {- currentPostRef := hc.homeref.ref
+ currentPostRef := *hc.homeref
capturedPost := post
hc.actionBuffer = append(hc.actionBuffer, func() {- capturedPost.Status = hc.printPost(currentPostRef, capturedPost.Status)
+ _, plaintext := hc.RenderPostPlaintext(capturedPost.Status, currentPostRef)
+ fmt.Print(plaintext)
})
justIncrementPostref(hc.homeref, post.Status)
idmap[post.Status.ID] = post.Status
@@ -334,7 +304,7 @@
return
case *mastodon.DeleteEvent:
- deleted, ok := idmap[post.ID]
+ _, ok := idmap[post.ID]
//didn't have this in the cache
if !ok {capturedID := post.ID
@@ -349,10 +319,10 @@
}
if hc.isPaused { hc.actionBuffer = append(hc.actionBuffer, func() {- hc.printPostDetailed("", deleted, "Deleted:")+ //hc.printPostDetailed("", deleted, "Deleted:")})
} else {- hc.printPostDetailed("", deleted, "Deleted:")+ //hc.printPostDetailed("", deleted, "Deleted:")}
return
--- a/notifications.go
+++ b/notifications.go
@@ -103,7 +103,7 @@
continue
}
- _, plaintext := hc.RenderPostPlaintext(Notification.Status, hc.ctxref.ref, "")
+ _, plaintext := hc.RenderPostPlaintext(Notification.Status, *hc.ctxref)
printstatus(Notification, plaintext)
saveRef(hc.ctxref.postmap, Notification.Status, hc.ctxref.ref)
--- a/pages.go
+++ b/pages.go
@@ -157,7 +157,7 @@
fmt.Printf("Couldn't load status page: %s\n", err)}
for i := range statuses {- item := makePageItem(statusData.hc.renderAndIncrement(statusData.hc.ctxref, statuses[i]) + "\n")
+ item := makePageItem(statusData.hc.renderAndIncrement(statusData.hc.ctxref, statuses[i]))
itemArray = append(itemArray, item)
}
return &itemArray
--- a/references.go
+++ b/references.go
@@ -32,7 +32,8 @@
// print and increment a status given a postref struct
func (hc *Hellclient) printAndIncrement(ref *postref, post *mastodon.Status) (returnPost *mastodon.Status) {- returnPost = hc.printPostS(*ref, post)
+ returnPost, plaintext := hc.RenderPostPlaintext(post, *ref)
+ fmt.Print(plaintext)
IncrementRef(ref, post)
ref.ref = IncrementString(ref.ref)
return
@@ -39,10 +40,10 @@
}
func (hc *Hellclient) renderAndIncrement(ref *postref, post *mastodon.Status) string {- plaintext := hc.renderPostS(*ref, post)
+ post, plaintext := hc.RenderPostPlaintext(post, *ref)
IncrementRef(ref, post)
ref.ref = IncrementString(ref.ref)
- return plaintext
+ return hyphenate(plaintext)
}
func printAndIncrementDetailed(ref *postref, post *mastodon.Status, format func(*mastodon.Status, string) string) {--- a/renderer.go
+++ b/renderer.go
@@ -17,6 +17,14 @@
postContext *postref
}
+// Returns the rendered content of a status's rt
+func (st *StatusFormatter) reblogContent() string {+ currentpost := st.status
+ defer func(){st.status = currentpost}()+ st.status = st.status.Reblog
+ return st.statusContent()
+}
+
// Returns the rendered content of a status
func (st *StatusFormatter) statusContent() string {renderedPost, plaintexts := st.hc.renderStatus(st.status.Content, st.postContext.ref)
@@ -27,9 +35,13 @@
return renderedPost
}
-func (st *StatusFormatter) mediaDescriptions() string {+func (st *StatusFormatter) mediaDescriptions(reblog bool) string {var sb strings.Builder
- for _, item := range st.status.MediaAttachments {+ status := st.status
+ if reblog {+ status = status.Reblog
+ }
+ for _, item := range status.MediaAttachments { if item.Description != "" { sb.WriteString(fmt.Sprintf("\n🖼️[%s]", item.Description))continue
@@ -36,6 +48,9 @@
}
sb.WriteString("🖼️")}
+ if len(status.MediaAttachments) > 0 {+ sb.WriteString("\n")+ }
return sb.String()
}
@@ -79,6 +94,24 @@
return cf.statusContent()
}
+// Status boost content stringer
+type boostContent struct {+ *StatusFormatter
+}
+
+func (bc *boostContent) String() string {+ return bc.reblogContent()
+}
+
+// Media descriptions for boosted posts stringer
+type boostMediaDescriptions struct {+ *StatusFormatter
+}
+
+func (bm *boostMediaDescriptions) String() string {+ return bm.mediaDescriptions(true)
+}
+
// Media description stringer
type mediaDescriptions struct {*StatusFormatter
@@ -85,7 +118,7 @@
}
func (md *mediaDescriptions) String() string {- return md.mediaDescriptions()
+ return md.mediaDescriptions(false)
}
// Post detail line (likes rts replies) stringer
@@ -95,6 +128,19 @@
func (dl *detailLine) String() string {return dl.detailLine()
+}
+
+//Boosted username stringer
+type boostedusername struct {+ *StatusFormatter
+}
+
+func (usr *boostedusername) String() string {+ var sb strings.Builder
+ sb.WriteString("<")+ sb.WriteString(usr.status.Reblog.Account.Username)
+ sb.WriteString(">")+ return sb.String()
}
// Formatted <username> stringer
--- a/templater.go
+++ b/templater.go
@@ -22,6 +22,9 @@
{key: "media_descriptions", stringer: &mediaDescriptions{sf}}, {key: "detail_line", stringer: &detailLine{sf}}, {key: "username", stringer: &username{sf}},+ {key: "boostuser", stringer: &boostedusername{sf}},+ {key: "boostcontent", stringer: &boostContent{sf}},+ {key: "boosted_media_descriptions", stringer: &boostMediaDescriptions{sf}},},
}
}
@@ -33,7 +36,7 @@
return tr.tempdefs[item].stringer.String()
}
}
- return fmt.Sprintf("%KEY_NOT_FOUND:%s", key)+ return fmt.Sprintf("%%KEY_NOT_FOUND:%s", key)}
return hyphenate(os.Expand(template, expandMap)), nil
--
⑨