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
|
// NewClient creates and returns a new Twitter client
|
||||||
func NewClient(url string) *Client {
|
func NewClient(screenName, url string) *Client {
|
||||||
client := Client{
|
client := Client{
|
||||||
apiBase: url,
|
apiBase: url,
|
||||||
screenName: wtf.Config.UString("wtf.mods.twitter.screenName", "wtfutil"),
|
screenName: screenName,
|
||||||
count: 5,
|
count: 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,7 @@ package twitter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
"github.com/senorprogrammer/wtf/wtf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tweet struct {
|
type Tweet struct {
|
||||||
@ -19,8 +18,10 @@ func (tweet *Tweet) String() string {
|
|||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
func (tweet *Tweet) Username() string {
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/senorprogrammer/wtf/wtf"
|
"github.com/senorprogrammer/wtf/wtf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const apiURL = "https://api.twitter.com/1.1/"
|
||||||
|
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
wtf.TextWidget
|
wtf.TextWidget
|
||||||
|
|
||||||
|
screenName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget() *Widget {
|
func NewWidget() *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: wtf.NewTextWidget("Twitter", "twitter", false),
|
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.SetWrap(true)
|
||||||
widget.View.SetWordWrap(true)
|
widget.View.SetWordWrap(true)
|
||||||
|
|
||||||
@ -24,11 +32,11 @@ func NewWidget() *Widget {
|
|||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
client := NewClient("https://api.twitter.com/1.1/")
|
client := NewClient(widget.screenName, apiURL)
|
||||||
userTweets := client.Tweets()
|
userTweets := client.Tweets()
|
||||||
|
|
||||||
widget.UpdateRefreshedAt()
|
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))
|
widget.View.SetText(widget.contentFrom(userTweets))
|
||||||
}
|
}
|
||||||
@ -48,6 +56,43 @@ func (widget *Widget) contentFrom(tweets []Tweet) string {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) format(tweet Tweet) string {
|
// If the tweet's Username is the same as the account we're watching, no
|
||||||
return fmt.Sprintf(" [white]%s[grey]\n [grey]%s - %s[white]\n\n", tweet.Text, tweet.Username(), tweet.CreatedAt)
|
// 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
|
package wtf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user