mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Move the exit message into the AppManager
Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
parent
9012868b2e
commit
5eeb553e2a
@ -5,27 +5,37 @@ import (
|
|||||||
|
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/support"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WtfAppManager handles the instances of WtfApp, ensuring that they're displayed as requested
|
// WtfAppManager handles the instances of WtfApp, ensuring that they're displayed as requested
|
||||||
type WtfAppManager struct {
|
type WtfAppManager struct {
|
||||||
WtfApps []*WtfApp
|
WtfApps []*WtfApp
|
||||||
|
|
||||||
|
config *config.Config
|
||||||
|
ghUser *support.GitHubUser
|
||||||
selected int
|
selected int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAppManager creates and returns an instance of AppManager
|
// NewAppManager creates and returns an instance of AppManager
|
||||||
func NewAppManager() WtfAppManager {
|
func NewAppManager(config *config.Config) WtfAppManager {
|
||||||
appMan := WtfAppManager{
|
appMan := WtfAppManager{
|
||||||
WtfApps: []*WtfApp{},
|
WtfApps: []*WtfApp{},
|
||||||
|
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
githubAPIKey := readGitHubAPIKey(config)
|
||||||
|
appMan.ghUser = support.NewGitHubUser(githubAPIKey)
|
||||||
|
|
||||||
|
go func() { _ = appMan.ghUser.Load() }()
|
||||||
|
|
||||||
return appMan
|
return appMan
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeNewWtfApp creates and starts a new instance of WtfApp from a set of configuration params
|
// MakeNewWtfApp creates and starts a new instance of WtfApp from a set of configuration params
|
||||||
func (appMan *WtfAppManager) MakeNewWtfApp(config *config.Config, configFilePath string) {
|
func (appMan *WtfAppManager) MakeNewWtfApp(configFilePath string) {
|
||||||
wtfApp := NewWtfApp(tview.NewApplication(), config, configFilePath)
|
wtfApp := NewWtfApp(tview.NewApplication(), appMan.config, configFilePath)
|
||||||
appMan.Add(wtfApp)
|
appMan.Add(wtfApp)
|
||||||
|
|
||||||
wtfApp.Start()
|
wtfApp.Start()
|
||||||
|
@ -21,17 +21,17 @@ const exitMessageHeader = `
|
|||||||
`
|
`
|
||||||
|
|
||||||
// DisplayExitMessage displays the onscreen exit message when the app quits
|
// DisplayExitMessage displays the onscreen exit message when the app quits
|
||||||
func (wtfApp *WtfApp) DisplayExitMessage() {
|
func (appMan *WtfAppManager) DisplayExitMessage() {
|
||||||
exitMessageIsDisplayable := readDisplayableConfig(wtfApp.config)
|
exitMessageIsDisplayable := readDisplayableConfig(appMan.config)
|
||||||
|
|
||||||
wtfApp.displayExitMsg(exitMessageIsDisplayable)
|
appMan.displayExitMsg(exitMessageIsDisplayable)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- 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 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 ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,16 +39,16 @@ func (wtfApp *WtfApp) displayExitMsg(exitMessageIsDisplayable bool) string {
|
|||||||
|
|
||||||
msgs = append(msgs, aurora.Magenta(exitMessageHeader).String())
|
msgs = append(msgs, aurora.Magenta(exitMessageHeader).String())
|
||||||
|
|
||||||
if wtfApp.ghUser.IsContributor {
|
if appMan.ghUser.IsContributor {
|
||||||
msgs = append(msgs, wtfApp.contributorThankYouMessage())
|
msgs = append(msgs, appMan.contributorThankYouMessage())
|
||||||
}
|
}
|
||||||
|
|
||||||
if wtfApp.ghUser.IsSponsor {
|
if appMan.ghUser.IsSponsor {
|
||||||
msgs = append(msgs, wtfApp.sponsorThankYouMessage())
|
msgs = append(msgs, appMan.sponsorThankYouMessage())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !wtfApp.ghUser.IsContributor && !wtfApp.ghUser.IsSponsor {
|
if !appMan.ghUser.IsContributor && !appMan.ghUser.IsSponsor {
|
||||||
msgs = append(msgs, wtfApp.supportRequestMessage())
|
msgs = append(msgs, appMan.supportRequestMessage())
|
||||||
}
|
}
|
||||||
|
|
||||||
displayMsg := strings.Join(msgs, "\n")
|
displayMsg := strings.Join(msgs, "\n")
|
||||||
@ -82,21 +82,21 @@ func readGitHubAPIKey(cfg *config.Config) string {
|
|||||||
|
|
||||||
/* -------------------- Messaging -------------------- */
|
/* -------------------- 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 := " 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."))
|
str += fmt.Sprintf(" %s", aurora.Green("\n\n You rock."))
|
||||||
|
|
||||||
return str
|
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 := " 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."))
|
str += fmt.Sprintf(" %s", aurora.Green("\n\n You're awesome."))
|
||||||
|
|
||||||
return str
|
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 := " 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"))
|
str += fmt.Sprintf(" Please consider sponsoring WTF at %s\n", aurora.Green("https://github.com/sponsors/senorprogrammer"))
|
||||||
|
|
||||||
|
@ -51,13 +51,13 @@ func Test_displayExitMessage(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
wtfApp := WtfApp{}
|
appMan := NewAppManager()
|
||||||
wtfApp.ghUser = &support.GitHubUser{
|
appMan.ghUser = &support.GitHubUser{
|
||||||
IsContributor: tt.isContributor,
|
IsContributor: tt.isContributor,
|
||||||
IsSponsor: tt.isSponsor,
|
IsSponsor: tt.isSponsor,
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := wtfApp.displayExitMsg(tt.isDisplayable)
|
actual := appMan.displayExitMsg(tt.isDisplayable)
|
||||||
|
|
||||||
if tt.compareWith == "equals" {
|
if tt.compareWith == "equals" {
|
||||||
assert.Equal(t, actual, tt.expected)
|
assert.Equal(t, actual, tt.expected)
|
||||||
|
@ -8,13 +8,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
|
|
||||||
|
// Bring in the extended set of terminfo definitions
|
||||||
_ "github.com/gdamore/tcell/terminfo/extended"
|
_ "github.com/gdamore/tcell/terminfo/extended"
|
||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/radovskyb/watcher"
|
"github.com/radovskyb/watcher"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/cfg"
|
"github.com/wtfutil/wtf/cfg"
|
||||||
"github.com/wtfutil/wtf/support"
|
|
||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
)
|
)
|
||||||
@ -28,7 +30,6 @@ type WtfApp struct {
|
|||||||
configFilePath string
|
configFilePath string
|
||||||
display *Display
|
display *Display
|
||||||
focusTracker FocusTracker
|
focusTracker FocusTracker
|
||||||
ghUser *support.GitHubUser
|
|
||||||
pages *tview.Pages
|
pages *tview.Pages
|
||||||
validator *ModuleValidator
|
validator *ModuleValidator
|
||||||
widgets []wtf.Wtfable
|
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.focusTracker = NewFocusTracker(wtfApp.TViewApp, wtfApp.widgets, wtfApp.config)
|
||||||
wtfApp.validator = NewModuleValidator()
|
wtfApp.validator = NewModuleValidator()
|
||||||
|
|
||||||
githubAPIKey := readGitHubAPIKey(wtfApp.config)
|
|
||||||
wtfApp.ghUser = support.NewGitHubUser(githubAPIKey)
|
|
||||||
|
|
||||||
wtfApp.pages.AddPage("grid", wtfApp.display.Grid, true, true)
|
wtfApp.pages.AddPage("grid", wtfApp.display.Grid, true, true)
|
||||||
|
|
||||||
wtfApp.validator.Validate(wtfApp.widgets)
|
wtfApp.validator.Validate(wtfApp.widgets)
|
||||||
@ -93,9 +91,6 @@ func (wtfApp *WtfApp) Run() {
|
|||||||
func (wtfApp *WtfApp) Start() {
|
func (wtfApp *WtfApp) Start() {
|
||||||
go wtfApp.scheduleWidgets()
|
go wtfApp.scheduleWidgets()
|
||||||
go wtfApp.watchForConfigChanges()
|
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
|
// 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
|
// These keys are global keys used by the app. Widgets should not implement these keys
|
||||||
switch event.Key() {
|
switch event.Key() {
|
||||||
case tcell.KeyCtrlC:
|
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.Stop()
|
||||||
wtfApp.TViewApp.Stop()
|
wtfApp.TViewApp.Stop()
|
||||||
wtfApp.DisplayExitMessage()
|
wtfApp.DisplayExitMessage()
|
||||||
|
4
main.go
4
main.go
@ -52,8 +52,8 @@ func main() {
|
|||||||
utils.Init(openFileUtil, openURLUtil)
|
utils.Init(openFileUtil, openURLUtil)
|
||||||
|
|
||||||
/* Initialize the App Manager */
|
/* Initialize the App Manager */
|
||||||
appMan := app.NewAppManager()
|
appMan := app.NewAppManager(config)
|
||||||
appMan.MakeNewWtfApp(config, flags.Config)
|
appMan.MakeNewWtfApp(flags.Config)
|
||||||
|
|
||||||
currentApp, err := appMan.Current()
|
currentApp, err := appMan.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user