Skip to content

Commit

Permalink
Implemented Toot struct & internal IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 3, 2021
1 parent 3350ec8 commit 62bebaa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
18 changes: 11 additions & 7 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ type Timeline struct {

Type TimelineType
Account mastodon.Account
Toots []mastodon.Status
TootIDs map[string]int
Toots []Toot
TootIndexStatusIDMappings map[string]int
}

func NewTimeline(mastodonClient *mastodon.Client) Timeline {
timeline := Timeline{
client: mastodonClient,

LastRenderedIndex: -1,
TootIDs: make(map[string]int),
TootIndexStatusIDMappings: make(map[string]int),
}

return timeline
Expand Down Expand Up @@ -70,12 +70,16 @@ func (timeline *Timeline) Load(timelineType TimelineType) (error) {
return err
}

for _, status := range statuses {
oldestStatusIndex := len(statuses) - 1
for i := oldestStatusIndex; i > 0; i-- {
status := statuses[i]

id := string(status.ID)
_, exists := timeline.TootIDs[id]
_, exists := timeline.TootIndexStatusIDMappings[id]
if exists == false {
timeline.Toots = append(timeline.Toots, *status)
timeline.TootIDs[id] = (len(timeline.Toots) - 1)
tootIndex := len(timeline.Toots)
timeline.Toots = append(timeline.Toots, NewToot(timeline.client, status, tootIndex))
timeline.TootIndexStatusIDMappings[id] = tootIndex
}
}

Expand Down
25 changes: 25 additions & 0 deletions mast/toot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mast

import (
// "context"

"github.com/mattn/go-mastodon"
)

type Toot struct {
client *mastodon.Client

ID int
Status mastodon.Status
}

func NewToot(mastodonClient *mastodon.Client, mastodonStatus *mastodon.Status , id int) Toot {
toot := Toot{
client: mastodonClient,

ID: id,
Status: *mastodonStatus,
}

return toot
}
4 changes: 2 additions & 2 deletions tui/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func RenderTimeline(timeline *mast.Timeline, width int) (string, error) {
var err error = nil

var tootOutput string = ""
newRenderedIndex := len(timeline.Toots) - 1
for i := newRenderedIndex; i > timeline.LastRenderedIndex; i-- {
newRenderedIndex := len(timeline.Toots)
for i := (timeline.LastRenderedIndex + 1); i < newRenderedIndex; i++ {
tootOutput, err = RenderToot(&timeline.Toots[i], width)
output = fmt.Sprintf("%s%s\n", output, tootOutput)
}
Expand Down
28 changes: 16 additions & 12 deletions tui/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,41 @@ import (
// "github.com/eliukblau/pixterm/pkg/ansimage"
// "context"

"github.com/mattn/go-mastodon"
// "github.com/mrusme/gomphotherium/mast"
// "github.com/mattn/go-mastodon"
"github.com/mrusme/gomphotherium/mast"
)

func RenderToot(toot *mastodon.Status, width int) (string, error) {
func RenderToot(toot *mast.Toot, width int) (string, error) {
var output string = ""
var err error = nil

createdAt := toot.CreatedAt
status := &toot.Status

account := toot.Account.Acct
createdAt := status.CreatedAt

account := status.Account.Acct
if account == "" {
account = toot.Account.Username
account = status.Account.Username
}

inReplyTo := ""
if toot.InReplyToID != nil {
inReplyTo = " [magenta]\xe2\x87\x9f[-]"
inReplyToLen := 0
if status.InReplyToID != nil {
inReplyTo = " \xe2\x87\x9f"
inReplyToLen = 1
}

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s%s[-]\n", output, toot.Account.DisplayName, account, inReplyTo)
output = fmt.Sprintf("%s%s\n", output, html.UnescapeString(strip.StripTags(toot.Content)))
output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][magenta]%s[-][grey]%*d[-]\n", output, status.Account.DisplayName, account, inReplyTo, (width - len(string(toot.ID)) - len(status.Account.DisplayName) - len(account) - inReplyToLen), toot.ID)
output = fmt.Sprintf("%s%s\n", output, html.UnescapeString(strip.StripTags(status.Content)))

// for _, attachment := range toot.MediaAttachments {
// for _, attachment := range status.MediaAttachments {
// pix, err := ansimage.NewScaledFromURL(attachment.PreviewURL, int((float64(width) * 0.75)), width, color.Transparent, ansimage.ScaleModeResize, ansimage.NoDithering)
// if err == nil {
// output = fmt.Sprintf("%s\n%s\n", output, pix.RenderExt(false, false))
// }
// }

output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] [green]\xe2\x86\xbb %d[-] [yellow]\xe2\x98\x85 %d[-] [grey]#%d on %s at %s[-]\n", output, toot.RepliesCount, toot.ReblogsCount, toot.FavouritesCount, createdAt.Format("Jan 2"), createdAt.Format("15:04"))
output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] [green]\xe2\x86\xbb %d[-] [yellow]\xe2\x98\x85 %d[-] [grey]on %s at %s[-]\n", output, status.RepliesCount, status.ReblogsCount, status.FavouritesCount, createdAt.Format("Jan 2"), createdAt.Format("15:04"))

output = fmt.Sprintf("%s\n", output)
return output, err
Expand Down

0 comments on commit 62bebaa

Please sign in to comment.