diff --git a/app/app_manager.go b/app/app_manager.go new file mode 100644 index 00000000..29d3528f --- /dev/null +++ b/app/app_manager.go @@ -0,0 +1,57 @@ +package app + +import "errors" + +// WtfAppManager handles the instances of WtfApp, ensuring that they're displayed as requested +type WtfAppManager struct { + WtfApps []*WtfApp + + selected int +} + +// NewAppManager creates and returns an instance of AppManager +func NewAppManager() WtfAppManager { + appMan := WtfAppManager{ + selected: 0, + } + + return appMan +} + +// AddApp adds a WtfApp to the collection of apps that the AppManager manages. +// This app is then available for display onscreen. +func (appMan *WtfAppManager) AddApp(wtfApp *WtfApp) error { + appMan.WtfApps = append(appMan.WtfApps, wtfApp) + return nil +} + +// Current returns the currently-displaying instance of WtfApp +func (appMan *WtfAppManager) Current() (*WtfApp, error) { + if appMan.selected < 0 || appMan.selected > (len(appMan.WtfApps)-1) { + 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() +} diff --git a/app/wtf_app.go b/app/wtf_app.go index 46c17d0d..da99f507 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -51,12 +51,11 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat wtfApp.widgets = MakeWidgets(wtfApp.TViewApp, wtfApp.pages, wtfApp.config) wtfApp.display = NewDisplay(wtfApp.widgets, wtfApp.config) wtfApp.focusTracker = NewFocusTracker(wtfApp.TViewApp, wtfApp.widgets, wtfApp.config) + wtfApp.validator = NewModuleValidator() githubAPIKey := readGitHubAPIKey(wtfApp.config) wtfApp.ghUser = support.NewGitHubUser(githubAPIKey) - wtfApp.validator = NewModuleValidator() - wtfApp.pages.AddPage("grid", wtfApp.display.Grid, true, true) wtfApp.validator.Validate(wtfApp.widgets) @@ -116,6 +115,11 @@ func (wtfApp *WtfApp) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { case tcell.KeyCtrlR: wtfApp.refreshAllWidgets() return nil + case tcell.KeyCtrlSpace: + // FIXME: This can't reside in the app, the app doesn't know about + // the AppManager. The AppManager needs to catch this one + fmt.Println("Next app") + return nil case tcell.KeyTab: wtfApp.focusTracker.Next() case tcell.KeyBacktab: diff --git a/main.go b/main.go index 6a0cf661..3287b45c 100644 --- a/main.go +++ b/main.go @@ -72,10 +72,18 @@ func main() { openURLUtil := utils.ToStrs(config.UList("wtf.openUrlUtil", []interface{}{})) utils.Init(openFileUtil, openURLUtil) - apps := []app.WtfApp{} - app := makeWtfApp(config, flags.Config) - apps = append(apps, app) + /* Initialize the App Manager */ + + wtfApp := makeWtfApp(config, flags.Config) + + appMan := app.NewAppManager() + appMan.AddApp(&wtfApp) + + currentApp, err := appMan.Current() + if err != nil { + fmt.Printf("\n%s %v\n", aurora.Red("ERROR"), err) + os.Exit(1) + } - currentApp := apps[0] currentApp.Run() } diff --git a/modules/gitlabtodo/keyboard.go b/modules/gitlabtodo/keyboard.go index e6507d92..91c336b8 100644 --- a/modules/gitlabtodo/keyboard.go +++ b/modules/gitlabtodo/keyboard.go @@ -3,8 +3,8 @@ package gitlabtodo import "github.com/gdamore/tcell" func (widget *Widget) initializeKeyboardControls() { - widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help widget") - widget.SetKeyboardChar("r", widget.Refresh, "Refresh widget") + widget.InitializeRefreshKeyboardControl(widget.Refresh) + widget.SetKeyboardChar("j", widget.Next, "Select next item") widget.SetKeyboardChar("k", widget.Prev, "Select previous item") widget.SetKeyboardChar("o", widget.openTodo, "Open todo in browser") diff --git a/modules/uptimerobot/keyboard.go b/modules/uptimerobot/keyboard.go index 483a4ef2..b06df96c 100644 --- a/modules/uptimerobot/keyboard.go +++ b/modules/uptimerobot/keyboard.go @@ -1,6 +1,5 @@ package uptimerobot func (widget *Widget) initializeKeyboardControls() { - widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help widget") - widget.SetKeyboardChar("r", widget.Refresh, "Refresh widget") + widget.InitializeRefreshKeyboardControl(widget.Refresh) } diff --git a/view/text_widget.go b/view/text_widget.go index 7753f43a..8e345496 100644 --- a/view/text_widget.go +++ b/view/text_widget.go @@ -14,6 +14,8 @@ type TextWidget struct { *KeyboardWidget View *tview.TextView + + tviewApp *tview.Application } // NewTextWidget creates and returns an instance of TextWidget @@ -21,6 +23,8 @@ func NewTextWidget(tviewApp *tview.Application, pages *tview.Pages, commonSettin widget := TextWidget{ Base: NewBase(commonSettings), KeyboardWidget: NewKeyboardWidget(tviewApp, pages, commonSettings), + + tviewApp: tviewApp, } widget.View = widget.createView(widget.bordered) @@ -39,6 +43,7 @@ func (widget *TextWidget) TextView() *tview.TextView { } func (widget *TextWidget) Redraw(data func() (string, string, bool)) { + // FIXME: This is coming from KeyboardWidget, which seems wrong widget.tviewApp.QueueUpdateDraw(func() { title, content, wrap := data()