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

Move common nav controls into MultiSourceWidget

This commit is contained in:
Chris Cummer 2018-09-01 17:37:09 -07:00
parent e760561027
commit ad431ee3a7
3 changed files with 51 additions and 47 deletions

View File

@ -36,11 +36,12 @@ type Widget struct {
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
MultiSourceWidget: wtf.NewMultiSourceWidget(),
MultiSourceWidget: wtf.NewMultiSourceWidget("textfile", "filePath", "filePaths"),
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
}
widget.LoadSources("textfile", "filePath", "filePaths")
widget.LoadSources()
widget.SetDisplayFunction(widget.display)
widget.HelpfulWidget.SetView(widget.View)
@ -53,24 +54,7 @@ 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.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()
}
// Refresh is called on the interval and refreshes the data
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.display()

View File

@ -35,13 +35,15 @@ type Widget struct {
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
MultiSourceWidget: wtf.NewMultiSourceWidget(),
MultiSourceWidget: wtf.NewMultiSourceWidget("twitter", "screenName", "screenNames"),
TextWidget: wtf.NewTextWidget("Twitter", "twitter", true),
idx: 0,
}
widget.LoadSources("twitter", "screenName", "screenNames")
widget.LoadSources()
widget.SetDisplayFunction(widget.display)
widget.client = NewClient()
widget.View.SetBorderPadding(1, 1, 1, 1)
@ -54,24 +56,7 @@ 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.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()
}
// Refresh is called on the interval and refreshes the data
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.display()

View File

@ -5,12 +5,21 @@ import (
)
type MultiSourceWidget struct {
module string
singular string
plural string
DisplayFunction func()
Idx int
Sources []string
}
func NewMultiSourceWidget() MultiSourceWidget {
return MultiSourceWidget{}
func NewMultiSourceWidget(module, singular, plural string) MultiSourceWidget {
return MultiSourceWidget{
module: module,
singular: singular,
plural: plural,
}
}
/* -------------------- Exported Functions -------------------- */
@ -23,11 +32,11 @@ func (widget *MultiSourceWidget) CurrentSource() string {
return widget.Sources[widget.Idx]
}
func (widget *MultiSourceWidget) LoadSources(module, singular, plural string) {
func (widget *MultiSourceWidget) LoadSources() {
var empty []interface{}
s := fmt.Sprintf("wtf.mods.%s.%s", module, singular)
p := fmt.Sprintf("wtf.mods.%s.%s", module, plural)
s := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.singular)
p := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.plural)
single := Config.UString(s, "")
multiple := Config.UList(p, empty)
@ -40,3 +49,29 @@ func (widget *MultiSourceWidget) LoadSources(module, singular, plural string) {
widget.Sources = asStrs
}
func (widget *MultiSourceWidget) Next() {
widget.Idx = widget.Idx + 1
if widget.Idx == len(widget.Sources) {
widget.Idx = 0
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
func (widget *MultiSourceWidget) Prev() {
widget.Idx = widget.Idx - 1
if widget.Idx < 0 {
widget.Idx = len(widget.Sources) - 1
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
func (widget *MultiSourceWidget) SetDisplayFunction(displayFunc func()) {
widget.DisplayFunction = displayFunc
}