1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/app/app_manager.go
Chris Cummer 5eeb553e2a Move the exit message into the AppManager
Signed-off-by: Chris Cummer <chriscummer@me.com>
2021-02-14 13:19:46 -08:00

82 lines
2.1 KiB
Go

package app
import (
"errors"
"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(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(configFilePath string) {
wtfApp := NewWtfApp(tview.NewApplication(), appMan.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) {
appMan.WtfApps = append(appMan.WtfApps, wtfApp)
}
// Current returns the currently-displaying instance of WtfApp
func (appMan *WtfAppManager) Current() (*WtfApp, error) {
if appMan.selected < 0 || appMan.selected >= len(appMan.WtfApps) {
return nil, errors.New("invalid app index selected")
}
return appMan.WtfApps[appMan.selected], nil
}
// Next cycles the WtfApps forward by one, making the next one in the list
// 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
}
return appMan.Current()
}
// Prev cycles the WtfApps backwards by one, making the previous one in the
// 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
}
return appMan.Current()
}