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:
parent
2af0fe51c6
commit
0cdaf5c2d3
@ -28,21 +28,18 @@ const HelpText = `
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.MultiSourceWidget
|
||||
wtf.TextWidget
|
||||
|
||||
filePaths []string
|
||||
idx int
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
|
||||
|
||||
idx: 0,
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget(),
|
||||
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
|
||||
}
|
||||
|
||||
widget.loadFilePaths()
|
||||
widget.LoadSources("textfile", "fileName", "fileNames")
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
@ -56,18 +53,18 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Next() {
|
||||
widget.idx = widget.idx + 1
|
||||
if widget.idx == len(widget.filePaths) {
|
||||
widget.idx = 0
|
||||
widget.MultiSourceWidget.Idx = widget.MultiSourceWidget.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.filePaths) - 1
|
||||
widget.Idx = widget.Idx - 1
|
||||
if widget.Idx < 0 {
|
||||
widget.Idx = len(widget.Sources) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
@ -81,14 +78,10 @@ func (widget *Widget) Refresh() {
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) currentFilePath() string {
|
||||
return widget.filePaths[widget.idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) display() {
|
||||
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) {
|
||||
text = text + widget.formattedText()
|
||||
@ -100,11 +93,11 @@ func (widget *Widget) display() {
|
||||
}
|
||||
|
||||
func (widget *Widget) fileName() string {
|
||||
return filepath.Base(widget.currentFilePath())
|
||||
return filepath.Base(widget.CurrentSource())
|
||||
}
|
||||
|
||||
func (widget *Widget) formattedText() string {
|
||||
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
|
||||
filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource())
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
@ -134,24 +127,8 @@ func (widget *Widget) formattedText() 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 {
|
||||
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
|
||||
filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource())
|
||||
|
||||
text, err := ioutil.ReadFile(filePath) // just pass the file name
|
||||
if err != nil {
|
||||
@ -172,7 +149,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
widget.Next()
|
||||
return nil
|
||||
case "o":
|
||||
wtf.OpenFile(widget.currentFilePath())
|
||||
wtf.OpenFile(widget.CurrentSource())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -12,18 +12,22 @@ import (
|
||||
const apiURL = "https://api.twitter.com/1.1/"
|
||||
|
||||
type Widget struct {
|
||||
wtf.MultiSourceWidget
|
||||
wtf.TextWidget
|
||||
|
||||
screenName string
|
||||
idx int
|
||||
sources []string
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
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.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
@ -34,7 +38,7 @@ func NewWidget() *Widget {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
client := NewClient(widget.screenName, apiURL)
|
||||
client := NewClient(widget.Sources, apiURL)
|
||||
userTweets := client.Tweets()
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
@ -58,10 +62,14 @@ func (widget *Widget) contentFrom(tweets []Tweet) string {
|
||||
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
|
||||
// need to display the username
|
||||
func (widget *Widget) displayName(tweet Tweet) string {
|
||||
if widget.screenName == tweet.User.ScreenName {
|
||||
if widget.currentSource() == tweet.User.ScreenName {
|
||||
return ""
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -12,13 +12,11 @@ type HelpfulWidget struct {
|
||||
}
|
||||
|
||||
func NewHelpfulWidget(app *tview.Application, pages *tview.Pages, helpText string) HelpfulWidget {
|
||||
widget := HelpfulWidget{
|
||||
return HelpfulWidget{
|
||||
app: app,
|
||||
helpText: helpText,
|
||||
pages: pages,
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
func (widget *HelpfulWidget) SetView(view *tview.TextView) {
|
||||
|
38
wtf/multisource_widget.go
Normal file
38
wtf/multisource_widget.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user