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

Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2020-12-18 20:20:31 -08:00
parent 23dc1c5058
commit cf6d2fc061
3 changed files with 29 additions and 17 deletions

View File

@ -1,6 +1,11 @@
package app
import "errors"
import (
"errors"
"github.com/olebedev/config"
"github.com/rivo/tview"
)
// WtfAppManager handles the instances of WtfApp, ensuring that they're displayed as requested
type WtfAppManager struct {
@ -13,13 +18,19 @@ type WtfAppManager struct {
func NewAppManager() WtfAppManager {
appMan := WtfAppManager{
WtfApps: []*WtfApp{},
selected: 0,
}
return appMan
}
// MakeNewWtfApp creates and starts a new instance of WtfApp from a set of configuration params
func (appMan *WtfAppManager) MakeNewWtfApp(config *config.Config, configFilePath string) {
wtfApp := NewWtfApp(tview.NewApplication(), config, configFilePath)
appMan.Add(wtfApp)
wtfApp.Start()
}
// Add adds a WtfApp to the collection of apps that the AppManager manages.
// This app is then available for display onscreen.
func (appMan *WtfAppManager) Add(wtfApp *WtfApp) {
@ -28,7 +39,7 @@ func (appMan *WtfAppManager) Add(wtfApp *WtfApp) {
// Current returns the currently-displaying instance of WtfApp
func (appMan *WtfAppManager) Current() (*WtfApp, error) {
if appMan.selected < 0 || appMan.selected > (len(appMan.WtfApps)-1) {
if appMan.selected < 0 || appMan.selected >= len(appMan.WtfApps) {
return nil, errors.New("invalid app index selected")
}
@ -39,6 +50,7 @@ func (appMan *WtfAppManager) Current() (*WtfApp, error) {
// the current one. If there are none after the current one, it wraps around.
func (appMan *WtfAppManager) Next() (*WtfApp, error) {
appMan.selected++
if appMan.selected >= len(appMan.WtfApps) {
appMan.selected = 0
}
@ -50,6 +62,7 @@ func (appMan *WtfAppManager) Next() (*WtfApp, error) {
// list the current one. If there are none before the current one, it wraps around.
func (appMan *WtfAppManager) Prev() (*WtfApp, error) {
appMan.selected--
if appMan.selected < 0 {
appMan.selected = len(appMan.WtfApps) - 1
}

View File

@ -34,8 +34,8 @@ type WtfApp struct {
}
// NewWtfApp creates and returns an instance of WtfApp
func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePath string) WtfApp {
wtfApp := WtfApp{
func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePath string) *WtfApp {
wtfApp := &WtfApp{
TViewApp: tviewApp,
config: config,
@ -86,9 +86,9 @@ func (wtfApp *WtfApp) Run() {
// Start initializes the app
func (wtfApp *WtfApp) Start() {
go wtfApp.scheduleWidgets()
go wtfApp.watchForConfigChanges()
// FIXME: This should be moved to the AppManager
go func() { _ = wtfApp.ghUser.Load() }()
}

19
main.go
View File

@ -40,13 +40,15 @@ func setTerm(config *config.Config) {
}
}
func makeWtfApp(config *config.Config, flagConfig string) app.WtfApp {
tviewApp = tview.NewApplication()
wtfApp := app.NewWtfApp(tviewApp, config, flagConfig)
wtfApp.Start()
// // FIXME: Move this into the AppManager
// panic("BOO")
// func makeWtfApp(config *config.Config, flagConfig string) app.WtfApp {
// tviewApp = tview.NewApplication()
// wtfApp := app.NewWtfApp(tviewApp, config, flagConfig)
// wtfApp.Start()
return wtfApp
}
// return wtfApp
// }
/* -------------------- Main -------------------- */
@ -73,11 +75,8 @@ func main() {
utils.Init(openFileUtil, openURLUtil)
/* Initialize the App Manager */
wtfApp := makeWtfApp(config, flags.Config)
appMan := app.NewAppManager()
appMan.Add(&wtfApp)
appMan.MakeNewWtfApp(config, flags.Config)
currentApp, err := appMan.Current()
if err != nil {