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

Add the MultiSourceWidget concept

This commit is contained in:
Chris Cummer 2018-09-01 12:57:03 -07:00
parent 2af0fe51c6
commit 0cdaf5c2d3
4 changed files with 80 additions and 46 deletions

View File

@ -28,21 +28,18 @@ const HelpText = `
type Widget struct { type Widget struct {
wtf.HelpfulWidget wtf.HelpfulWidget
wtf.MultiSourceWidget
wtf.TextWidget wtf.TextWidget
filePaths []string
idx int
} }
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{ widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
MultiSourceWidget: wtf.NewMultiSourceWidget(),
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true), TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
idx: 0,
} }
widget.loadFilePaths() widget.LoadSources("textfile", "fileName", "fileNames")
widget.HelpfulWidget.SetView(widget.View) widget.HelpfulWidget.SetView(widget.View)
@ -56,18 +53,18 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Next() { func (widget *Widget) Next() {
widget.idx = widget.idx + 1 widget.MultiSourceWidget.Idx = widget.MultiSourceWidget.Idx + 1
if widget.idx == len(widget.filePaths) { if widget.Idx == len(widget.Sources) {
widget.idx = 0 widget.Idx = 0
} }
widget.display() widget.display()
} }
func (widget *Widget) Prev() { func (widget *Widget) Prev() {
widget.idx = widget.idx - 1 widget.Idx = widget.Idx - 1
if widget.idx < 0 { if widget.Idx < 0 {
widget.idx = len(widget.filePaths) - 1 widget.Idx = len(widget.Sources) - 1
} }
widget.display() widget.display()
@ -81,14 +78,10 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) currentFilePath() string {
return widget.filePaths[widget.idx]
}
func (widget *Widget) display() { func (widget *Widget) display() {
widget.View.SetTitle(widget.ContextualTitle(widget.fileName())) widget.View.SetTitle(widget.ContextualTitle(widget.fileName()))
text := wtf.SigilStr(len(widget.filePaths), widget.idx, widget.View) + "\n" text := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n"
if wtf.Config.UBool("wtf.mods.textfile.format", false) { if wtf.Config.UBool("wtf.mods.textfile.format", false) {
text = text + widget.formattedText() text = text + widget.formattedText()
@ -100,11 +93,11 @@ func (widget *Widget) display() {
} }
func (widget *Widget) fileName() string { func (widget *Widget) fileName() string {
return filepath.Base(widget.currentFilePath()) return filepath.Base(widget.CurrentSource())
} }
func (widget *Widget) formattedText() string { func (widget *Widget) formattedText() string {
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath()) filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource())
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
@ -134,24 +127,8 @@ func (widget *Widget) formattedText() string {
return tview.TranslateANSI(buf.String()) return tview.TranslateANSI(buf.String())
} }
// loadFilePaths parses file paths from the config and stores them in an array
// It is backwards-compatible, supporting both the original, singular filePath and
// the current plural filePaths
func (widget *Widget) loadFilePaths() {
var emptyArray []interface{}
filePath := wtf.Config.UString("wtf.mods.textfile.filePath", "")
filePaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.textfile.filePaths", emptyArray))
if filePath != "" {
filePaths = append(filePaths, filePath)
}
widget.filePaths = filePaths
}
func (widget *Widget) plainText() string { func (widget *Widget) plainText() string {
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath()) filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource())
text, err := ioutil.ReadFile(filePath) // just pass the file name text, err := ioutil.ReadFile(filePath) // just pass the file name
if err != nil { if err != nil {
@ -172,7 +149,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
widget.Next() widget.Next()
return nil return nil
case "o": case "o":
wtf.OpenFile(widget.currentFilePath()) wtf.OpenFile(widget.CurrentSource())
return nil return nil
} }

View File

@ -12,18 +12,22 @@ import (
const apiURL = "https://api.twitter.com/1.1/" const apiURL = "https://api.twitter.com/1.1/"
type Widget struct { type Widget struct {
wtf.MultiSourceWidget
wtf.TextWidget wtf.TextWidget
screenName string idx int
sources []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"), idx: 0,
} }
widget.loadSources()
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)
@ -34,7 +38,7 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
client := NewClient(widget.screenName, apiURL) client := NewClient(widget.Sources, apiURL)
userTweets := client.Tweets() userTweets := client.Tweets()
widget.UpdateRefreshedAt() widget.UpdateRefreshedAt()
@ -58,10 +62,14 @@ func (widget *Widget) contentFrom(tweets []Tweet) string {
return str return 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.screenName == tweet.User.ScreenName { if widget.currentSource() == tweet.User.ScreenName {
return "" return ""
} }
return tweet.User.ScreenName return tweet.User.ScreenName
@ -109,3 +117,16 @@ 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() {
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)
}
widget.sources = multiple
}

View File

@ -12,13 +12,11 @@ type HelpfulWidget struct {
} }
func NewHelpfulWidget(app *tview.Application, pages *tview.Pages, helpText string) HelpfulWidget { func NewHelpfulWidget(app *tview.Application, pages *tview.Pages, helpText string) HelpfulWidget {
widget := HelpfulWidget{ return HelpfulWidget{
app: app, app: app,
helpText: helpText, helpText: helpText,
pages: pages, pages: pages,
} }
return widget
} }
func (widget *HelpfulWidget) SetView(view *tview.TextView) { func (widget *HelpfulWidget) SetView(view *tview.TextView) {

38
wtf/multisource_widget.go Normal file
View File

@ -0,0 +1,38 @@
package wtf
import (
"fmt"
)
type MultiSourceWidget struct {
Idx int
Sources []string
}
func NewMultiSourceWidget() MultiSourceWidget {
return MultiSourceWidget{}
}
/* -------------------- Exported Functions -------------------- */
func (widget *MultiSourceWidget) CurrentSource() string {
return widget.Sources[widget.Idx]
}
func (widget *MultiSourceWidget) LoadSources(module, singular, plural string) {
var empty []interface{}
s := fmt.Sprintf("wtf.mods.%s.%s", module, singular)
p := fmt.Sprintf("wtf.mods.%s.%s", module, plural)
single := Config.UString(s, "")
multiple := Config.UList(p, empty)
asStrs := ToStrs(multiple)
if single != "" {
asStrs = append(asStrs, single)
}
widget.Sources = asStrs
}