Skip to content

Commit

Permalink
Move common nav controls into MultiSourceWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
senorprogrammer committed Sep 2, 2018
1 parent e760561 commit ad431ee
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
24 changes: 4 additions & 20 deletions textfile/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand Down
25 changes: 5 additions & 20 deletions twitter/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
49 changes: 42 additions & 7 deletions wtf/multisource_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import (
)

type MultiSourceWidget struct {
Idx int
Sources []string
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 -------------------- */
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit ad431ee

Please sign in to comment.