1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Casey Primozic d82eda1933
Improvements to twitterstats code from review
* Unmarshal directly into a `TwitterStats` struct by using json struct annotations
 * Pull stats fetching for a single user out into its own function so that closing the request body is done after each request and the HTTP request can be re-used for multiple usernames' stats
 * Improve type casting code used in settings parsing logic
2019-10-23 11:49:33 -07:00

50 lines
1.0 KiB
Go

package twitterstats
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.TextWidget
client *Client
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(app, settings.common),
client: NewClient(settings),
}
widget.View.SetBorderPadding(1, 1, 1, 1)
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
return &widget
}
func (widget *Widget) Refresh() {
widget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
usernames := widget.client.screenNames
stats := widget.client.GetStats()
// Add header row
str := fmt.Sprintf("%-16s %8s %8s\n", "Username", "Followers", "Tweets")
// Add rows for each of the followed usernames
for i, username := range usernames {
followerCount := stats[i].FollowerCount
tweetCount := stats[i].TweetCount
str += fmt.Sprintf("%-16s %8d %8d\n", username, followerCount, tweetCount)
}
return "Twitter Stats", str, true
}