From 8f4c53d58ad2450edfc2be18bc0e9571a91339b4 Mon Sep 17 00:00:00 2001 From: Anand Sudhir Prayaga Date: Wed, 22 Aug 2018 13:47:44 +0200 Subject: [PATCH 1/2] Make google calendar focussable When there are lot of events, the content in teh widget is hidden and there is no way to view the upcoming events. Making the widget focussable allows for one to scroll through the list of calendar events using the arrow keys --- gcal/display.go | 1 + gcal/widget.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gcal/display.go b/gcal/display.go index 2290a5d2..572eb67f 100644 --- a/gcal/display.go +++ b/gcal/display.go @@ -33,6 +33,7 @@ func (widget *Widget) display() { defer widget.mutex.Unlock() _, timedEvents := widget.sortedEvents() + widget.View.SetTitle(widget.ContextualTitle(widget.Name)) widget.View.SetText(widget.contentFrom(timedEvents)) } diff --git a/gcal/widget.go b/gcal/widget.go index 011215dc..101cf042 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -17,7 +17,7 @@ type Widget struct { func NewWidget() *Widget { widget := Widget{ - TextWidget: wtf.NewTextWidget("Calendar", "gcal", false), + TextWidget: wtf.NewTextWidget("Calendar", "gcal", true), ch: make(chan struct{}), } From d5017cd06c1b5e80a4573f61ac8e5d506673f40f Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Wed, 22 Aug 2018 13:44:12 -0700 Subject: [PATCH 2/2] Improve the display of tweets in the Twitter widget --- twitter/client.go | 4 ++-- twitter/tweet.go | 11 +++++----- twitter/widget.go | 53 +++++++++++++++++++++++++++++++++++++++++++---- wtf/colors.go | 2 +- 4 files changed, 58 insertions(+), 12 deletions(-) diff --git a/twitter/client.go b/twitter/client.go index bb8b4c63..e39fa1ff 100644 --- a/twitter/client.go +++ b/twitter/client.go @@ -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, } diff --git a/twitter/tweet.go b/twitter/tweet.go index 18dc1ea9..60d231ba 100644 --- a/twitter/tweet.go +++ b/twitter/tweet.go @@ -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")) } diff --git a/twitter/widget.go b/twitter/widget.go index 6b6ff437..d878cca2 100644 --- a/twitter/widget.go +++ b/twitter/widget.go @@ -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) } diff --git a/wtf/colors.go b/wtf/colors.go index 47befef7..7eb1219c 100644 --- a/wtf/colors.go +++ b/wtf/colors.go @@ -1,9 +1,9 @@ package wtf import ( - "strings" "regexp" "strconv" + "strings" "github.com/gdamore/tcell" )