mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Improve the display of tweets in the Twitter widget
This commit is contained in:
parent
816a3d4194
commit
d5017cd06c
@ -22,10 +22,10 @@ type Client struct {
|
||||
}
|
||||
|
||||
// NewClient creates and returns a new Twitter client
|
||||
func NewClient(url string) *Client {
|
||||
func NewClient(screenName, url string) *Client {
|
||||
client := Client{
|
||||
apiBase: url,
|
||||
screenName: wtf.Config.UString("wtf.mods.twitter.screenName", "wtfutil"),
|
||||
screenName: screenName,
|
||||
count: 5,
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,7 @@ package twitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Tweet struct {
|
||||
@ -19,8 +18,10 @@ func (tweet *Tweet) String() string {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (tweet *Tweet) Username() string {
|
||||
return fmt.Sprint(tweet.User.ScreenName)
|
||||
return tweet.User.ScreenName
|
||||
}
|
||||
func (tweet *Tweet) PrettyStart() string {
|
||||
return wtf.PrettyDate(tweet.CreatedAt)
|
||||
|
||||
func (tweet *Tweet) PrettyCreatedAt() string {
|
||||
newTime, _ := time.Parse(time.RubyDate, tweet.CreatedAt)
|
||||
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
|
||||
}
|
||||
|
@ -2,19 +2,27 @@ package twitter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
const apiURL = "https://api.twitter.com/1.1/"
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
screenName string
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget("Twitter", "twitter", false),
|
||||
|
||||
screenName: wtf.Config.UString("wtf.mods.twitter.screenName", "wtfutil"),
|
||||
}
|
||||
|
||||
widget.View.SetBorderPadding(1, 1, 1, 1)
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
|
||||
@ -24,11 +32,11 @@ func NewWidget() *Widget {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
client := NewClient("https://api.twitter.com/1.1/")
|
||||
client := NewClient(widget.screenName, apiURL)
|
||||
userTweets := client.Tweets()
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.SetTitle(widget.ContextualTitle(widget.Name))
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("Twitter - [green]@%s[white]", client.screenName)))
|
||||
|
||||
widget.View.SetText(widget.contentFrom(userTweets))
|
||||
}
|
||||
@ -48,6 +56,43 @@ func (widget *Widget) contentFrom(tweets []Tweet) string {
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) format(tweet Tweet) string {
|
||||
return fmt.Sprintf(" [white]%s[grey]\n [grey]%s - %s[white]\n\n", tweet.Text, tweet.Username(), tweet.CreatedAt)
|
||||
// If the tweet's Username is the same as the account we're watching, no
|
||||
// need to display the username
|
||||
func (widget *Widget) displayName(tweet Tweet) string {
|
||||
if widget.screenName == tweet.User.ScreenName {
|
||||
return ""
|
||||
}
|
||||
return tweet.User.ScreenName
|
||||
}
|
||||
|
||||
func (widget *Widget) highlightText(text string) string {
|
||||
result := text
|
||||
|
||||
// RT indicator
|
||||
rtRegExp := regexp.MustCompile(`^RT`)
|
||||
result = rtRegExp.ReplaceAllString(result, "[olive]${0}[white::-]")
|
||||
|
||||
// @name mentions
|
||||
atRegExp := regexp.MustCompile(`@[0-9A-Za-z_]*`)
|
||||
result = atRegExp.ReplaceAllString(result, "[blue]${0}[white]")
|
||||
|
||||
// HTTP(S) links
|
||||
linkRegExp := regexp.MustCompile(`http[s:\/.0-9A-Za-z]*`)
|
||||
result = linkRegExp.ReplaceAllString(result, "[lightblue::u]${0}[white::-]")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (widget *Widget) format(tweet Tweet) string {
|
||||
body := widget.highlightText(tweet.Text)
|
||||
name := widget.displayName(tweet)
|
||||
|
||||
var attribution string
|
||||
if name == "" {
|
||||
attribution = tweet.PrettyCreatedAt()
|
||||
} else {
|
||||
attribution = fmt.Sprintf("%s, %s", name, tweet.PrettyCreatedAt())
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution)
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user