From ed8d357881ea1ef00ad28cd186481e97386249a0 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 28 Apr 2019 20:43:39 -0700 Subject: [PATCH] Simplifies the use of MultiSourceWidget Widgets that embed MultiSourceWidget no longer have to explicitly call loadSources(). This is done as part of the set-up for MultiSourceWidget. --- modules/git/widget.go | 1 - modules/mercurial/widget.go | 1 - modules/textfile/widget.go | 1 - modules/twitter/widget.go | 1 - wtf/multisource_widget.go | 80 ++++++++++++++++++++++++------------- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/modules/git/widget.go b/modules/git/widget.go index 0b6ad89d..6373a7d0 100644 --- a/modules/git/widget.go +++ b/modules/git/widget.go @@ -50,7 +50,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * settings: settings, } - widget.LoadSources() widget.SetDisplayFunction(widget.display) widget.HelpfulWidget.SetView(widget.View) diff --git a/modules/mercurial/widget.go b/modules/mercurial/widget.go index 656047e8..ea95c989 100644 --- a/modules/mercurial/widget.go +++ b/modules/mercurial/widget.go @@ -45,7 +45,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * settings: settings, } - widget.LoadSources() widget.SetDisplayFunction(widget.display) widget.HelpfulWidget.SetView(widget.View) diff --git a/modules/textfile/widget.go b/modules/textfile/widget.go index 17bf277f..9acb3075 100644 --- a/modules/textfile/widget.go +++ b/modules/textfile/widget.go @@ -53,7 +53,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * widget.settings.common.RefreshInterval = 0 widget.HelpfulWidget.SetView(widget.View) - widget.LoadSources() widget.SetDisplayFunction(widget.display) widget.View.SetWrap(true) widget.View.SetWordWrap(true) diff --git a/modules/twitter/widget.go b/modules/twitter/widget.go index 2682e151..33dfb5c0 100644 --- a/modules/twitter/widget.go +++ b/modules/twitter/widget.go @@ -48,7 +48,6 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * widget.HelpfulWidget.SetView(widget.View) - widget.LoadSources() widget.SetDisplayFunction(widget.display) widget.client = NewClient(settings) diff --git a/wtf/multisource_widget.go b/wtf/multisource_widget.go index 3123e6cf..6ddffdf1 100644 --- a/wtf/multisource_widget.go +++ b/wtf/multisource_widget.go @@ -14,16 +14,22 @@ type MultiSourceWidget struct { Sources []string } +// NewMultiSourceWidget creates and returns an instance of MultiSourceWidget func NewMultiSourceWidget(moduleConfig *cfg.Common, singular, plural string) MultiSourceWidget { - return MultiSourceWidget{ + widget := MultiSourceWidget{ moduleConfig: moduleConfig, singular: singular, plural: plural, } + + widget.loadSources() + + return widget } /* -------------------- Exported Functions -------------------- */ +// CurrentSource returns the string representations of the currently-displayed source func (widget *MultiSourceWidget) CurrentSource() string { if widget.Idx >= len(widget.Sources) { return "" @@ -32,7 +38,51 @@ func (widget *MultiSourceWidget) CurrentSource() string { return widget.Sources[widget.Idx] } -func (widget *MultiSourceWidget) LoadSources() { +// Next displays the next source in the source list. If the current source is the last +// source it wraps around to the first source +func (widget *MultiSourceWidget) Next() { + widget.Idx = widget.Idx + 1 + if widget.Idx == len(widget.Sources) { + widget.Idx = 0 + } + + if widget.DisplayFunction != nil { + widget.DisplayFunction() + } +} + +// Prev displays the previous source in the source list. If the current source is the first +// source, it wraps around to the last source +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() + } +} + +// SetDisplayFunction stores the function that should be called when the source is +// changed. This is typically called from within the initializer for the struct that +// embeds MultiSourceWidget +// +// Example: +// +// widget := Widget{ +// MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "person", "people") +// } +// +// widget.SetDisplayFunction(widget.display) +// +func (widget *MultiSourceWidget) SetDisplayFunction(displayFunc func()) { + widget.DisplayFunction = displayFunc +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *MultiSourceWidget) loadSources() { var empty []interface{} single := widget.moduleConfig.Config.UString(widget.singular, "") @@ -46,29 +96,3 @@ func (widget *MultiSourceWidget) LoadSources() { 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 -}