diff --git a/app/app_manager.go b/app/app_manager.go index 097d3a01..50e89954 100644 --- a/app/app_manager.go +++ b/app/app_manager.go @@ -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 } diff --git a/app/wtf_app.go b/app/wtf_app.go index da99f507..67bb3ff9 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -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() }() } diff --git a/main.go b/main.go index e1978414..32d996c9 100644 --- a/main.go +++ b/main.go @@ -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 {