diff --git a/app/app_manager.go b/app/app_manager.go index 50e89954..00f209c3 100644 --- a/app/app_manager.go +++ b/app/app_manager.go @@ -5,27 +5,37 @@ import ( "github.com/olebedev/config" "github.com/rivo/tview" + "github.com/wtfutil/wtf/support" ) // WtfAppManager handles the instances of WtfApp, ensuring that they're displayed as requested type WtfAppManager struct { WtfApps []*WtfApp + config *config.Config + ghUser *support.GitHubUser selected int } // NewAppManager creates and returns an instance of AppManager -func NewAppManager() WtfAppManager { +func NewAppManager(config *config.Config) WtfAppManager { appMan := WtfAppManager{ WtfApps: []*WtfApp{}, + + config: config, } + githubAPIKey := readGitHubAPIKey(config) + appMan.ghUser = support.NewGitHubUser(githubAPIKey) + + go func() { _ = appMan.ghUser.Load() }() + 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) +func (appMan *WtfAppManager) MakeNewWtfApp(configFilePath string) { + wtfApp := NewWtfApp(tview.NewApplication(), appMan.config, configFilePath) appMan.Add(wtfApp) wtfApp.Start() diff --git a/app/exit_message.go b/app/exit_message.go index f68779ff..1f621347 100644 --- a/app/exit_message.go +++ b/app/exit_message.go @@ -21,17 +21,17 @@ const exitMessageHeader = ` ` // DisplayExitMessage displays the onscreen exit message when the app quits -func (wtfApp *WtfApp) DisplayExitMessage() { - exitMessageIsDisplayable := readDisplayableConfig(wtfApp.config) +func (appMan *WtfAppManager) DisplayExitMessage() { + exitMessageIsDisplayable := readDisplayableConfig(appMan.config) - wtfApp.displayExitMsg(exitMessageIsDisplayable) + appMan.displayExitMsg(exitMessageIsDisplayable) } /* -------------------- Unexported Functions -------------------- */ -func (wtfApp *WtfApp) displayExitMsg(exitMessageIsDisplayable bool) string { +func (appMan *WtfAppManager) displayExitMsg(exitMessageIsDisplayable bool) string { // If a sponsor or contributor and opt out of seeing the exit message, do not display it - if (wtfApp.ghUser.IsContributor || wtfApp.ghUser.IsSponsor) && !exitMessageIsDisplayable { + if (appMan.ghUser.IsContributor || appMan.ghUser.IsSponsor) && !exitMessageIsDisplayable { return "" } @@ -39,16 +39,16 @@ func (wtfApp *WtfApp) displayExitMsg(exitMessageIsDisplayable bool) string { msgs = append(msgs, aurora.Magenta(exitMessageHeader).String()) - if wtfApp.ghUser.IsContributor { - msgs = append(msgs, wtfApp.contributorThankYouMessage()) + if appMan.ghUser.IsContributor { + msgs = append(msgs, appMan.contributorThankYouMessage()) } - if wtfApp.ghUser.IsSponsor { - msgs = append(msgs, wtfApp.sponsorThankYouMessage()) + if appMan.ghUser.IsSponsor { + msgs = append(msgs, appMan.sponsorThankYouMessage()) } - if !wtfApp.ghUser.IsContributor && !wtfApp.ghUser.IsSponsor { - msgs = append(msgs, wtfApp.supportRequestMessage()) + if !appMan.ghUser.IsContributor && !appMan.ghUser.IsSponsor { + msgs = append(msgs, appMan.supportRequestMessage()) } displayMsg := strings.Join(msgs, "\n") @@ -82,21 +82,21 @@ func readGitHubAPIKey(cfg *config.Config) string { /* -------------------- Messaging -------------------- */ -func (wtfApp *WtfApp) contributorThankYouMessage() string { +func (appMan *WtfAppManager) contributorThankYouMessage() string { str := " On behalf of all the users of WTF, thank you for contributing to the source code." str += fmt.Sprintf(" %s", aurora.Green("\n\n You rock.")) return str } -func (wtfApp *WtfApp) sponsorThankYouMessage() string { +func (appMan *WtfAppManager) sponsorThankYouMessage() string { str := " Your sponsorship of WTF makes a difference. Thank you for sponsoring and supporting WTF." str += fmt.Sprintf(" %s", aurora.Green("\n\n You're awesome.")) return str } -func (wtfApp *WtfApp) supportRequestMessage() string { +func (appMan *WtfAppManager) supportRequestMessage() string { str := " The development and maintenance of WTF is supported by sponsorships.\n" str += fmt.Sprintf(" Please consider sponsoring WTF at %s\n", aurora.Green("https://github.com/sponsors/senorprogrammer")) diff --git a/app/exit_message_test.go b/app/exit_message_test.go index b236d4e3..5022afa6 100644 --- a/app/exit_message_test.go +++ b/app/exit_message_test.go @@ -51,13 +51,13 @@ func Test_displayExitMessage(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - wtfApp := WtfApp{} - wtfApp.ghUser = &support.GitHubUser{ + appMan := NewAppManager() + appMan.ghUser = &support.GitHubUser{ IsContributor: tt.isContributor, IsSponsor: tt.isSponsor, } - actual := wtfApp.displayExitMsg(tt.isDisplayable) + actual := appMan.displayExitMsg(tt.isDisplayable) if tt.compareWith == "equals" { assert.Equal(t, actual, tt.expected) diff --git a/app/wtf_app.go b/app/wtf_app.go index 7ea32e1c..500b7b55 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -8,13 +8,15 @@ import ( "time" "github.com/gdamore/tcell" + + // Bring in the extended set of terminfo definitions _ "github.com/gdamore/tcell/terminfo/extended" + "github.com/logrusorgru/aurora" "github.com/olebedev/config" "github.com/radovskyb/watcher" "github.com/rivo/tview" "github.com/wtfutil/wtf/cfg" - "github.com/wtfutil/wtf/support" "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/wtf" ) @@ -28,7 +30,6 @@ type WtfApp struct { configFilePath string display *Display focusTracker FocusTracker - ghUser *support.GitHubUser pages *tview.Pages validator *ModuleValidator widgets []wtf.Wtfable @@ -54,9 +55,6 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat wtfApp.focusTracker = NewFocusTracker(wtfApp.TViewApp, wtfApp.widgets, wtfApp.config) wtfApp.validator = NewModuleValidator() - githubAPIKey := readGitHubAPIKey(wtfApp.config) - wtfApp.ghUser = support.NewGitHubUser(githubAPIKey) - wtfApp.pages.AddPage("grid", wtfApp.display.Grid, true, true) wtfApp.validator.Validate(wtfApp.widgets) @@ -93,9 +91,6 @@ func (wtfApp *WtfApp) Run() { func (wtfApp *WtfApp) Start() { go wtfApp.scheduleWidgets() go wtfApp.watchForConfigChanges() - - // FIXME: This should be moved to the AppManager - go func() { _ = wtfApp.ghUser.Load() }() } // Stop kills all the currently-running widgets in this app @@ -128,6 +123,8 @@ func (wtfApp *WtfApp) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { // These keys are global keys used by the app. Widgets should not implement these keys switch event.Key() { case tcell.KeyCtrlC: + // FIXME: This can't reside in the app, the app shouldn't know + // about termination. The AppManager needs to catch this wtfApp.Stop() wtfApp.TViewApp.Stop() wtfApp.DisplayExitMessage() diff --git a/main.go b/main.go index b95cd667..2027b579 100644 --- a/main.go +++ b/main.go @@ -52,8 +52,8 @@ func main() { utils.Init(openFileUtil, openURLUtil) /* Initialize the App Manager */ - appMan := app.NewAppManager() - appMan.MakeNewWtfApp(config, flags.Config) + appMan := app.NewAppManager(config) + appMan.MakeNewWtfApp(flags.Config) currentApp, err := appMan.Current() if err != nil {