1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Twitter module supports subscribing to multiple screen names

This commit is contained in:
Chris Cummer 2018-09-01 17:05:52 -07:00
parent 0cdaf5c2d3
commit e760561027
5 changed files with 94 additions and 37 deletions

View File

@ -249,7 +249,7 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) {
case "trello": case "trello":
widgets = append(widgets, trello.NewWidget()) widgets = append(widgets, trello.NewWidget())
case "twitter": case "twitter":
widgets = append(widgets, twitter.NewWidget()) widgets = append(widgets, twitter.NewWidget(app, pages))
case "weather": case "weather":
widgets = append(widgets, weather.NewWidget(app, pages)) widgets = append(widgets, weather.NewWidget(app, pages))
case "zendesk": case "zendesk":

View File

@ -2,6 +2,7 @@ package textfile
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -39,7 +40,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true), TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
} }
widget.LoadSources("textfile", "fileName", "fileNames") widget.LoadSources("textfile", "filePath", "filePaths")
widget.HelpfulWidget.SetView(widget.View) widget.HelpfulWidget.SetView(widget.View)
@ -53,7 +54,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Next() { func (widget *Widget) Next() {
widget.MultiSourceWidget.Idx = widget.MultiSourceWidget.Idx + 1 widget.Idx = widget.Idx + 1
if widget.Idx == len(widget.Sources) { if widget.Idx == len(widget.Sources) {
widget.Idx = 0 widget.Idx = 0
} }
@ -72,14 +73,14 @@ func (widget *Widget) Prev() {
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt() widget.UpdateRefreshedAt()
widget.display() widget.display()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() { 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" 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 { func (widget *Widget) plainText() string {
filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) 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 { if err != nil {
return err.Error() return err.Error()
} }

View File

@ -22,11 +22,11 @@ type Client struct {
} }
// NewClient creates and returns a new Twitter client // NewClient creates and returns a new Twitter client
func NewClient(screenName, url string) *Client { func NewClient() *Client {
client := Client{ client := Client{
apiBase: url, apiBase: "https://api.twitter.com/1.1/",
screenName: screenName,
count: wtf.Config.UInt("wtf.mods.twitter.count", 5), count: wtf.Config.UInt("wtf.mods.twitter.count", 5),
screenName: "",
} }
client.loadAPICredentials() client.loadAPICredentials()

View File

@ -6,70 +6,103 @@ import (
"regexp" "regexp"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf" "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 { type Widget struct {
wtf.HelpfulWidget
wtf.MultiSourceWidget wtf.MultiSourceWidget
wtf.TextWidget wtf.TextWidget
client *Client
idx int idx int
sources []string sources []string
} }
func NewWidget() *Widget { func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := 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, idx: 0,
} }
widget.loadSources() widget.LoadSources("twitter", "screenName", "screenNames")
widget.client = NewClient()
widget.View.SetBorderPadding(1, 1, 1, 1) widget.View.SetBorderPadding(1, 1, 1, 1)
widget.View.SetWrap(true) widget.View.SetWrap(true)
widget.View.SetWordWrap(true) widget.View.SetWordWrap(true)
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget return &widget
} }
/* -------------------- Exported Functions -------------------- */ /* -------------------- 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() { func (widget *Widget) Refresh() {
client := NewClient(widget.Sources, apiURL)
userTweets := client.Tweets()
widget.UpdateRefreshedAt() widget.UpdateRefreshedAt()
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("Twitter - [green]@%s[white]", client.screenName))) widget.display()
widget.View.SetText(widget.contentFrom(userTweets))
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- 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 { 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 { for _, tweet := range tweets {
str = str + widget.format(tweet) str = str + widget.format(tweet)
} }
return str widget.View.SetText(str)
}
func (widget *Widget) currentSource() string {
return widget.sources[widget.idx]
} }
// If the tweet's Username is the same as the account we're watching, no // If the tweet's Username is the same as the account we're watching, no
// need to display the username // need to display the username
func (widget *Widget) displayName(tweet Tweet) string { func (widget *Widget) displayName(tweet Tweet) string {
if widget.currentSource() == tweet.User.ScreenName { if widget.CurrentSource() == tweet.User.ScreenName {
return "" return ""
} }
return tweet.User.ScreenName 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) return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution)
} }
func (widget *Widget) loadSources() { func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
var empty []interface{} switch string(event.Rune()) {
case "/":
single := wtf.Config.UString("wtf.mods.twitter.screenName", "") widget.ShowHelp()
multiple := wtf.ToStrs(wtf.Config.UList("wtf.mods.twitter.screenNames", empty)) return nil
case "h":
if single != "" { widget.Prev()
multiple = append(multiple, single) 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
} }

View File

@ -16,6 +16,10 @@ func NewMultiSourceWidget() MultiSourceWidget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *MultiSourceWidget) CurrentSource() string { func (widget *MultiSourceWidget) CurrentSource() string {
if widget.Idx >= len(widget.Sources) {
return ""
}
return widget.Sources[widget.Idx] return widget.Sources[widget.Idx]
} }