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:
parent
cfd3c731ba
commit
73391b06e4
@ -17,21 +17,22 @@ func Schedule(widget wtf.Wtfable) {
|
||||
return
|
||||
}
|
||||
|
||||
tick := time.NewTicker(interval)
|
||||
quit := make(chan struct{})
|
||||
timer := time.NewTicker(interval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
case <-timer.C:
|
||||
if widget.Enabled() {
|
||||
widget.Refresh()
|
||||
} else {
|
||||
tick.Stop()
|
||||
timer.Stop()
|
||||
return
|
||||
}
|
||||
case quit := <-widget.QuitChan():
|
||||
if quit == true {
|
||||
timer.Stop()
|
||||
return
|
||||
}
|
||||
case <-quit:
|
||||
tick.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,14 +60,14 @@ func (wtfApp *WtfApp) Start() {
|
||||
|
||||
// Stop kills all the currently-running widgets in this app
|
||||
func (wtfApp *WtfApp) Stop() {
|
||||
wtfApp.disableAllWidgets()
|
||||
wtfApp.stopAllWidgets()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (wtfApp *WtfApp) disableAllWidgets() {
|
||||
func (wtfApp *WtfApp) stopAllWidgets() {
|
||||
for _, widget := range wtfApp.Widgets {
|
||||
widget.Disable()
|
||||
widget.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,19 +18,19 @@ func (widget *Widget) initializeKeyboardControls() {
|
||||
}
|
||||
|
||||
func (widget *Widget) previous() {
|
||||
widget.SpotifyClient.Previous()
|
||||
widget.client.Previous()
|
||||
time.Sleep(time.Second * 1)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) next() {
|
||||
widget.SpotifyClient.Next()
|
||||
widget.client.Next()
|
||||
time.Sleep(time.Second * 1)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
||||
func (widget *Widget) playPause() {
|
||||
widget.SpotifyClient.PlayPause()
|
||||
widget.client.PlayPause()
|
||||
time.Sleep(time.Second * 1)
|
||||
widget.Refresh()
|
||||
}
|
||||
|
@ -13,20 +13,19 @@ type Widget struct {
|
||||
wtf.KeyboardWidget
|
||||
wtf.TextWidget
|
||||
|
||||
client spotigopher.SpotifyClient
|
||||
settings *Settings
|
||||
spotigopher.Info
|
||||
spotigopher.SpotifyClient
|
||||
}
|
||||
|
||||
// NewWidget creates a new instance of a widget
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
spotifyClient := spotigopher.NewClient()
|
||||
widget := Widget{
|
||||
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Info: spotigopher.Info{},
|
||||
SpotifyClient: spotifyClient,
|
||||
Info: spotigopher.Info{},
|
||||
client: spotigopher.NewClient(),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
@ -45,7 +44,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
}
|
||||
|
||||
func (w *Widget) refreshSpotifyInfos() error {
|
||||
info, err := w.SpotifyClient.GetInfo()
|
||||
info, err := w.client.GetInfo()
|
||||
w.Info = info
|
||||
return err
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ type BarGraph struct {
|
||||
key string
|
||||
maxStars int
|
||||
name string
|
||||
quitChan chan bool
|
||||
refreshing bool
|
||||
starChar string
|
||||
|
||||
@ -37,9 +38,11 @@ func NewBarGraph(app *tview.Application, name string, settings *cfg.Common, focu
|
||||
focusable: focusable,
|
||||
maxStars: settings.Config.UInt("graphStars", 20),
|
||||
name: settings.Title,
|
||||
quitChan: make(chan bool),
|
||||
starChar: settings.Config.UString("graphIcon", "|"),
|
||||
RefreshInt: settings.RefreshInterval,
|
||||
commonSettings: settings,
|
||||
|
||||
RefreshInt: settings.RefreshInterval,
|
||||
}
|
||||
|
||||
widget.View = widget.addView()
|
||||
@ -90,6 +93,10 @@ func (widget *BarGraph) Name() string {
|
||||
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
|
||||
func (widget *BarGraph) Refreshing() bool {
|
||||
return widget.refreshing
|
||||
@ -104,6 +111,11 @@ func (widget *BarGraph) SetFocusChar(char string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (widget *BarGraph) Stop() {
|
||||
widget.enabled = false
|
||||
widget.quitChan <- true
|
||||
}
|
||||
|
||||
func (widget *BarGraph) TextView() *tview.TextView {
|
||||
return widget.View
|
||||
}
|
||||
|
6
wtf/stoppable.go
Normal file
6
wtf/stoppable.go
Normal file
@ -0,0 +1,6 @@
|
||||
package wtf
|
||||
|
||||
// Stoppable is the interface that enforces a stoppable state
|
||||
type Stoppable interface {
|
||||
Stop()
|
||||
}
|
@ -15,6 +15,7 @@ type TextWidget struct {
|
||||
focusable bool
|
||||
focusChar string
|
||||
name string
|
||||
quitChan chan bool
|
||||
refreshing bool
|
||||
refreshInterval int
|
||||
app *tview.Application
|
||||
@ -32,6 +33,7 @@ func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable
|
||||
focusable: focusable,
|
||||
focusChar: commonSettings.FocusChar(),
|
||||
name: commonSettings.Name,
|
||||
quitChan: make(chan bool),
|
||||
refreshing: false,
|
||||
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)
|
||||
}
|
||||
|
||||
func (widget *TextWidget) QuitChan() chan bool {
|
||||
return widget.quitChan
|
||||
}
|
||||
|
||||
func (widget *TextWidget) Name() string {
|
||||
return widget.name
|
||||
}
|
||||
@ -115,6 +121,11 @@ func (widget *TextWidget) SetFocusChar(char string) {
|
||||
widget.focusChar = char
|
||||
}
|
||||
|
||||
func (widget *TextWidget) Stop() {
|
||||
widget.enabled = false
|
||||
widget.quitChan <- true
|
||||
}
|
||||
|
||||
func (widget *TextWidget) String() string {
|
||||
return widget.name
|
||||
}
|
||||
|
@ -10,12 +10,14 @@ import (
|
||||
type Wtfable interface {
|
||||
Enablable
|
||||
Schedulable
|
||||
Stoppable
|
||||
|
||||
BorderColor() string
|
||||
ConfigText() string
|
||||
FocusChar() string
|
||||
Focusable() bool
|
||||
HelpText() string
|
||||
QuitChan() chan bool
|
||||
Name() string
|
||||
SetFocusChar(string)
|
||||
TextView() *tview.TextView
|
||||
|
Loading…
x
Reference in New Issue
Block a user