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

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.
This commit is contained in:
Chris Cummer 2019-04-28 20:43:39 -07:00
parent 9d7b91a024
commit ed8d357881
5 changed files with 52 additions and 32 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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
}