From e760561027a97d50010204b5c0ea87a0491ad498 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 1 Sep 2018 17:05:52 -0700 Subject: [PATCH] Twitter module supports subscribing to multiple screen names --- main.go | 2 +- textfile/widget.go | 13 +++-- twitter/client.go | 6 +-- twitter/widget.go | 106 ++++++++++++++++++++++++++++---------- wtf/multisource_widget.go | 4 ++ 5 files changed, 94 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index bc8e8522..5da8d00e 100644 --- a/main.go +++ b/main.go @@ -249,7 +249,7 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { case "trello": widgets = append(widgets, trello.NewWidget()) case "twitter": - widgets = append(widgets, twitter.NewWidget()) + widgets = append(widgets, twitter.NewWidget(app, pages)) case "weather": widgets = append(widgets, weather.NewWidget(app, pages)) case "zendesk": diff --git a/textfile/widget.go b/textfile/widget.go index 607c7bb7..e035b365 100644 --- a/textfile/widget.go +++ b/textfile/widget.go @@ -2,6 +2,7 @@ package textfile import ( "bytes" + "fmt" "io/ioutil" "os" "path/filepath" @@ -39,7 +40,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { TextWidget: wtf.NewTextWidget("TextFile", "textfile", true), } - widget.LoadSources("textfile", "fileName", "fileNames") + widget.LoadSources("textfile", "filePath", "filePaths") widget.HelpfulWidget.SetView(widget.View) @@ -53,7 +54,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Next() { - widget.MultiSourceWidget.Idx = widget.MultiSourceWidget.Idx + 1 + widget.Idx = widget.Idx + 1 if widget.Idx == len(widget.Sources) { widget.Idx = 0 } @@ -72,14 +73,14 @@ func (widget *Widget) Prev() { func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() - widget.display() } /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) display() { - widget.View.SetTitle(widget.ContextualTitle(widget.fileName())) + title := fmt.Sprintf("[green]%s[white]", widget.CurrentSource()) + widget.View.SetTitle(widget.ContextualTitle(title)) text := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n" @@ -130,7 +131,9 @@ func (widget *Widget) formattedText() string { func (widget *Widget) plainText() string { filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) - text, err := ioutil.ReadFile(filePath) // just pass the file name + fmt.Println(filePath) + + text, err := ioutil.ReadFile(filePath) if err != nil { return err.Error() } diff --git a/twitter/client.go b/twitter/client.go index fa92688e..0d44da04 100644 --- a/twitter/client.go +++ b/twitter/client.go @@ -22,11 +22,11 @@ type Client struct { } // NewClient creates and returns a new Twitter client -func NewClient(screenName, url string) *Client { +func NewClient() *Client { client := Client{ - apiBase: url, - screenName: screenName, + apiBase: "https://api.twitter.com/1.1/", count: wtf.Config.UInt("wtf.mods.twitter.count", 5), + screenName: "", } client.loadAPICredentials() diff --git a/twitter/widget.go b/twitter/widget.go index 9b5d9ef2..890e886c 100644 --- a/twitter/widget.go +++ b/twitter/widget.go @@ -6,70 +6,103 @@ import ( "regexp" "github.com/dustin/go-humanize" + "github.com/gdamore/tcell" + "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" ) -const apiURL = "https://api.twitter.com/1.1/" +const HelpText = ` + Keyboard commands for Textfile: + + /: Show/hide this help window + h: Previous Twitter name + l: Next Twitter name + + arrow left: Previous Twitter name + arrow right: Next Twitter name +` type Widget struct { + wtf.HelpfulWidget wtf.MultiSourceWidget wtf.TextWidget + client *Client idx int sources []string } -func NewWidget() *Widget { +func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { widget := Widget{ - TextWidget: wtf.NewTextWidget("Twitter", "twitter", false), + HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), + MultiSourceWidget: wtf.NewMultiSourceWidget(), + TextWidget: wtf.NewTextWidget("Twitter", "twitter", true), idx: 0, } - widget.loadSources() + widget.LoadSources("twitter", "screenName", "screenNames") + widget.client = NewClient() widget.View.SetBorderPadding(1, 1, 1, 1) widget.View.SetWrap(true) widget.View.SetWordWrap(true) + widget.View.SetInputCapture(widget.keyboardIntercept) return &widget } /* -------------------- Exported Functions -------------------- */ +func (widget *Widget) Next() { + widget.Idx = widget.Idx + 1 + if widget.Idx == len(widget.Sources) { + widget.Idx = 0 + } + + widget.display() +} + +func (widget *Widget) Prev() { + widget.Idx = widget.Idx - 1 + if widget.Idx < 0 { + widget.Idx = len(widget.Sources) - 1 + } + + widget.display() +} + func (widget *Widget) Refresh() { - client := NewClient(widget.Sources, apiURL) - userTweets := client.Tweets() - widget.UpdateRefreshedAt() - widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("Twitter - [green]@%s[white]", client.screenName))) - - widget.View.SetText(widget.contentFrom(userTweets)) + widget.display() } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) contentFrom(tweets []Tweet) string { +func (widget *Widget) display() { + widget.client.screenName = widget.CurrentSource() + tweets := widget.client.Tweets() + + widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource()))) + if len(tweets) == 0 { - return fmt.Sprintf("\n\n\n%s", wtf.CenterText("[blue]No Tweets[white]", 50)) + str := fmt.Sprintf("\n\n\n%s", wtf.CenterText("[blue]No Tweets[white]", 50)) + widget.View.SetText(str) + return } - str := "" + str := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n" for _, tweet := range tweets { str = str + widget.format(tweet) } - return str -} - -func (widget *Widget) currentSource() string { - return widget.sources[widget.idx] + widget.View.SetText(str) } // 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.currentSource() == tweet.User.ScreenName { + if widget.CurrentSource() == tweet.User.ScreenName { return "" } return tweet.User.ScreenName @@ -118,15 +151,32 @@ func (widget *Widget) format(tweet Tweet) string { return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution) } -func (widget *Widget) loadSources() { - var empty []interface{} - - single := wtf.Config.UString("wtf.mods.twitter.screenName", "") - multiple := wtf.ToStrs(wtf.Config.UList("wtf.mods.twitter.screenNames", empty)) - - if single != "" { - multiple = append(multiple, single) +func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { + switch string(event.Rune()) { + case "/": + widget.ShowHelp() + return nil + case "h": + widget.Prev() + return nil + case "l": + widget.Next() + return nil + case "o": + wtf.OpenFile(widget.CurrentSource()) + return nil } - widget.sources = multiple + switch event.Key() { + case tcell.KeyLeft: + widget.Prev() + return nil + case tcell.KeyRight: + widget.Next() + return nil + default: + return event + } + + return event } diff --git a/wtf/multisource_widget.go b/wtf/multisource_widget.go index 87a359bb..a7650b0e 100644 --- a/wtf/multisource_widget.go +++ b/wtf/multisource_widget.go @@ -16,6 +16,10 @@ func NewMultiSourceWidget() MultiSourceWidget { /* -------------------- Exported Functions -------------------- */ func (widget *MultiSourceWidget) CurrentSource() string { + if widget.Idx >= len(widget.Sources) { + return "" + } + return widget.Sources[widget.Idx] }