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

Add ability to explicitly stop modules via a QuitChan

This commit is contained in:
Chris Cummer 2019-07-27 07:25:55 -07:00
parent cfd3c731ba
commit 73391b06e4
8 changed files with 50 additions and 19 deletions

View File

@ -17,21 +17,22 @@ func Schedule(widget wtf.Wtfable) {
return return
} }
tick := time.NewTicker(interval) timer := time.NewTicker(interval)
quit := make(chan struct{})
for { for {
select { select {
case <-tick.C: case <-timer.C:
if widget.Enabled() { if widget.Enabled() {
widget.Refresh() widget.Refresh()
} else { } else {
tick.Stop() timer.Stop()
return
}
case quit := <-widget.QuitChan():
if quit == true {
timer.Stop()
return return
} }
case <-quit:
tick.Stop()
return
} }
} }
} }

View File

@ -60,14 +60,14 @@ func (wtfApp *WtfApp) Start() {
// Stop kills all the currently-running widgets in this app // Stop kills all the currently-running widgets in this app
func (wtfApp *WtfApp) Stop() { func (wtfApp *WtfApp) Stop() {
wtfApp.disableAllWidgets() wtfApp.stopAllWidgets()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (wtfApp *WtfApp) disableAllWidgets() { func (wtfApp *WtfApp) stopAllWidgets() {
for _, widget := range wtfApp.Widgets { for _, widget := range wtfApp.Widgets {
widget.Disable() widget.Stop()
} }
} }

View File

@ -18,19 +18,19 @@ func (widget *Widget) initializeKeyboardControls() {
} }
func (widget *Widget) previous() { func (widget *Widget) previous() {
widget.SpotifyClient.Previous() widget.client.Previous()
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
widget.Refresh() widget.Refresh()
} }
func (widget *Widget) next() { func (widget *Widget) next() {
widget.SpotifyClient.Next() widget.client.Next()
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
widget.Refresh() widget.Refresh()
} }
func (widget *Widget) playPause() { func (widget *Widget) playPause() {
widget.SpotifyClient.PlayPause() widget.client.PlayPause()
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
widget.Refresh() widget.Refresh()
} }

View File

@ -13,20 +13,19 @@ type Widget struct {
wtf.KeyboardWidget wtf.KeyboardWidget
wtf.TextWidget wtf.TextWidget
client spotigopher.SpotifyClient
settings *Settings settings *Settings
spotigopher.Info spotigopher.Info
spotigopher.SpotifyClient
} }
// NewWidget creates a new instance of a widget // NewWidget creates a new instance of a widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
spotifyClient := spotigopher.NewClient()
widget := Widget{ widget := Widget{
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common), KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
TextWidget: wtf.NewTextWidget(app, settings.common, true), TextWidget: wtf.NewTextWidget(app, settings.common, true),
Info: spotigopher.Info{}, Info: spotigopher.Info{},
SpotifyClient: spotifyClient, client: spotigopher.NewClient(),
settings: settings, settings: settings,
} }
@ -45,7 +44,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
} }
func (w *Widget) refreshSpotifyInfos() error { func (w *Widget) refreshSpotifyInfos() error {
info, err := w.SpotifyClient.GetInfo() info, err := w.client.GetInfo()
w.Info = info w.Info = info
return err return err
} }

View File

@ -17,6 +17,7 @@ type BarGraph struct {
key string key string
maxStars int maxStars int
name string name string
quitChan chan bool
refreshing bool refreshing bool
starChar string starChar string
@ -37,9 +38,11 @@ func NewBarGraph(app *tview.Application, name string, settings *cfg.Common, focu
focusable: focusable, focusable: focusable,
maxStars: settings.Config.UInt("graphStars", 20), maxStars: settings.Config.UInt("graphStars", 20),
name: settings.Title, name: settings.Title,
quitChan: make(chan bool),
starChar: settings.Config.UString("graphIcon", "|"), starChar: settings.Config.UString("graphIcon", "|"),
RefreshInt: settings.RefreshInterval,
commonSettings: settings, commonSettings: settings,
RefreshInt: settings.RefreshInterval,
} }
widget.View = widget.addView() widget.View = widget.addView()
@ -90,6 +93,10 @@ func (widget *BarGraph) Name() string {
return widget.name return widget.name
} }
func (widget *BarGraph) QuitChan() chan bool {
return widget.quitChan
}
// Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not // Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not
func (widget *BarGraph) Refreshing() bool { func (widget *BarGraph) Refreshing() bool {
return widget.refreshing return widget.refreshing
@ -104,6 +111,11 @@ func (widget *BarGraph) SetFocusChar(char string) {
return return
} }
func (widget *BarGraph) Stop() {
widget.enabled = false
widget.quitChan <- true
}
func (widget *BarGraph) TextView() *tview.TextView { func (widget *BarGraph) TextView() *tview.TextView {
return widget.View return widget.View
} }

6
wtf/stoppable.go Normal file
View File

@ -0,0 +1,6 @@
package wtf
// Stoppable is the interface that enforces a stoppable state
type Stoppable interface {
Stop()
}

View File

@ -15,6 +15,7 @@ type TextWidget struct {
focusable bool focusable bool
focusChar string focusChar string
name string name string
quitChan chan bool
refreshing bool refreshing bool
refreshInterval int refreshInterval int
app *tview.Application app *tview.Application
@ -32,6 +33,7 @@ func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable
focusable: focusable, focusable: focusable,
focusChar: commonSettings.FocusChar(), focusChar: commonSettings.FocusChar(),
name: commonSettings.Name, name: commonSettings.Name,
quitChan: make(chan bool),
refreshing: false, refreshing: false,
refreshInterval: commonSettings.RefreshInterval, refreshInterval: commonSettings.RefreshInterval,
} }
@ -97,6 +99,10 @@ func (widget *TextWidget) HelpText() string {
return fmt.Sprintf("\n There is no help available for widget %s", widget.commonSettings.Module.Type) return fmt.Sprintf("\n There is no help available for widget %s", widget.commonSettings.Module.Type)
} }
func (widget *TextWidget) QuitChan() chan bool {
return widget.quitChan
}
func (widget *TextWidget) Name() string { func (widget *TextWidget) Name() string {
return widget.name return widget.name
} }
@ -115,6 +121,11 @@ func (widget *TextWidget) SetFocusChar(char string) {
widget.focusChar = char widget.focusChar = char
} }
func (widget *TextWidget) Stop() {
widget.enabled = false
widget.quitChan <- true
}
func (widget *TextWidget) String() string { func (widget *TextWidget) String() string {
return widget.name return widget.name
} }

View File

@ -10,12 +10,14 @@ import (
type Wtfable interface { type Wtfable interface {
Enablable Enablable
Schedulable Schedulable
Stoppable
BorderColor() string BorderColor() string
ConfigText() string ConfigText() string
FocusChar() string FocusChar() string
Focusable() bool Focusable() bool
HelpText() string HelpText() string
QuitChan() chan bool
Name() string Name() string
SetFocusChar(string) SetFocusChar(string)
TextView() *tview.TextView TextView() *tview.TextView