Skip to content

Commit

Permalink
Continued implementation of timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 3, 2021
1 parent 848779b commit 3350ec8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
27 changes: 25 additions & 2 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ const (

type Timeline struct {
client *mastodon.Client

LastRenderedIndex int

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

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

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

return timeline
Expand All @@ -33,6 +41,13 @@ func (timeline *Timeline) Load(timelineType TimelineType) (error) {
var statuses []*mastodon.Status
var err error

account, err := timeline.client.GetAccountCurrentUser(context.Background())
if err != nil {
return err
}

timeline.Account = *account

switch timelineType {
case TimelineHome:
statuses, err = timeline.client.GetTimelineHome(context.Background(), nil)
Expand All @@ -55,6 +70,14 @@ func (timeline *Timeline) Load(timelineType TimelineType) (error) {
return err
}

timeline.Toots = statuses
for _, status := range statuses {
id := string(status.ID)
_, exists := timeline.TootIDs[id]
if exists == false {
timeline.Toots = append(timeline.Toots, *status)
timeline.TootIDs[id] = (len(timeline.Toots) - 1)
}
}

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

var tootOutput string = ""
for i := len(timeline.Toots) - 1; i >= 0; i-- {
tootOutput, err = RenderToot(timeline.Toots[i], width)
newRenderedIndex := len(timeline.Toots) - 1
for i := newRenderedIndex; i > timeline.LastRenderedIndex; i-- {
tootOutput, err = RenderToot(&timeline.Toots[i], width)
output = fmt.Sprintf("%s%s\n", output, tootOutput)
}

timeline.LastRenderedIndex = newRenderedIndex
return output, err
}
33 changes: 24 additions & 9 deletions tui/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package tui

import (
"fmt"
// "time"

"github.com/grokify/html-strip-tags-go"
"html"

"image/color"
"github.com/eliukblau/pixterm/pkg/ansimage"
// "image/color"
// "github.com/eliukblau/pixterm/pkg/ansimage"
// "context"

"github.com/mattn/go-mastodon"
Expand All @@ -18,16 +19,30 @@ func RenderToot(toot *mastodon.Status, width int) (string, error) {
var output string = ""
var err error = nil

output = fmt.Sprintf("%s%s [%s]\n", output, toot.Account.DisplayName, toot.Account.Acct)
output = fmt.Sprintf("%s%s\n", output, html.UnescapeString(strip.StripTags(toot.Content)))
createdAt := toot.CreatedAt

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

for _, attachment := range toot.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%s\n", output, pix.RenderExt(false, false))
}
inReplyTo := ""
if toot.InReplyToID != nil {
inReplyTo = " [magenta]\xe2\x87\x9f[-]"
}

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)))

// for _, attachment := range toot.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\n", output)
return output, err
}
29 changes: 27 additions & 2 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func TUI(mastodonClient *mastodon.Client) {
app := tview.NewApplication()

input := tview.NewInputField().
SetLabel("@user.instance.org: ").
SetLabelColor(tcell.ColorDefault).
SetFieldBackgroundColor(tcell.ColorDefault).
SetDoneFunc(func(key tcell.Key) {
Expand All @@ -35,17 +34,43 @@ func TUI(mastodonClient *mastodon.Client) {
AddItem(input, 1, 0, 1, 1, 0, 0, true)

app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlN {
switch event.Key() {
case tcell.KeyCtrlR:
_, _, w, _ := stream.Box.GetInnerRect()
timeline.Load(mast.TimelineHome)
output, err := RenderTimeline(&timeline, w)
if err != nil {
panic(err)
}

input.
SetLabel(timeline.Account.Username + ": ").
SetLabelColor(tcell.ColorTeal)
app.SetFocus(input)

fmt.Fprint(stream, tview.TranslateANSI(output))

stream.ScrollToEnd()
return nil
case tcell.KeyRune:
switch event.Rune() {
case 'i':
if input.Box.HasFocus() == false {
app.SetFocus(input)
input.SetLabelColor(tcell.ColorTeal)
return nil
}
}
case tcell.KeyEscape:
if input.Box.HasFocus() == true {
app.SetFocus(stream)
input.SetLabelColor(tcell.ColorDefault)
return nil
}
// case tcell.KeyPgDn:
// app.SetFocus(stream)
}

return event
})

Expand Down

0 comments on commit 3350ec8

Please sign in to comment.