From 9012868b2e0b6abf14a5c5abea84013e523ae010 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 3 Jan 2021 20:07:56 -0800 Subject: [PATCH] Don't fail to start up if there are no widgets defined Signed-off-by: Chris Cummer --- _sample_configs/multi_config.yml | 3 +++ app/display.go | 25 +++++++++++++++---------- app/wtf_app.go | 31 +++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 _sample_configs/multi_config.yml diff --git a/_sample_configs/multi_config.yml b/_sample_configs/multi_config.yml new file mode 100644 index 00000000..f914a9bd --- /dev/null +++ b/_sample_configs/multi_config.yml @@ -0,0 +1,3 @@ +configs: + - small_config.yml + - sample_config.yml \ No newline at end of file diff --git a/app/display.go b/app/display.go index a5dfd2cf..63d9c87a 100644 --- a/app/display.go +++ b/app/display.go @@ -1,6 +1,7 @@ package app import ( + "github.com/gdamore/tcell" "github.com/olebedev/config" "github.com/rivo/tview" "github.com/wtfutil/wtf/utils" @@ -9,24 +10,28 @@ import ( // Display is the container for the onscreen representation of a WtfApp type Display struct { - Grid *tview.Grid - config *config.Config + Grid *tview.Grid + + bgColor tcell.Color + config *config.Config } // NewDisplay creates and returns a Display func NewDisplay(widgets []wtf.Wtfable, config *config.Config) *Display { display := Display{ - Grid: tview.NewGrid(), - config: config, + Grid: tview.NewGrid(), + + bgColor: wtf.ColorFor("black"), + config: config, } - firstWidget := widgets[0] - display.Grid.SetBackgroundColor( - wtf.ColorFor( - firstWidget.CommonSettings().Colors.WidgetTheme.Background, - ), - ) + // Make sure we have some widgets before trying to get the first one + if len(widgets) > 0 { + firstWidget := widgets[0] + display.bgColor = wtf.ColorFor(firstWidget.CommonSettings().Colors.WidgetTheme.Background) + } + display.Grid.SetBackgroundColor(display.bgColor) display.build(widgets) return &display diff --git a/app/wtf_app.go b/app/wtf_app.go index 67bb3ff9..7ea32e1c 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -1,6 +1,7 @@ package app import ( + "errors" "fmt" "log" "os" @@ -60,12 +61,7 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat wtfApp.validator.Validate(wtfApp.widgets) - firstWidget := wtfApp.widgets[0] - wtfApp.pages.Box.SetBackgroundColor( - wtf.ColorFor( - firstWidget.CommonSettings().Colors.WidgetTheme.Background, - ), - ) + wtfApp.setBackgroundColor() wtfApp.TViewApp.SetInputCapture(wtfApp.keyboardIntercept) wtfApp.TViewApp.SetRoot(wtfApp.pages, true) @@ -75,6 +71,16 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat /* -------------------- Exported Functions -------------------- */ +// FirstWidget returns the first wiget in the set of widgets, or +// an error if there are none +func (wtfApp *WtfApp) FirstWidget() (wtf.Wtfable, error) { + if len(wtfApp.widgets) < 1 { + return nil, errors.New("cannot get first widget. no widgets defined") + } + + return wtfApp.widgets[0], nil +} + // Run starts the underlying tview app func (wtfApp *WtfApp) Run() { if err := wtfApp.TViewApp.Run(); err != nil { @@ -99,6 +105,19 @@ func (wtfApp *WtfApp) Stop() { /* -------------------- Unexported Functions -------------------- */ +func (wtfApp *WtfApp) setBackgroundColor() { + var bgColor tcell.Color + + firstWidget, err := wtfApp.FirstWidget() + if err != nil { + bgColor = wtf.ColorFor("black") + } else { + bgColor = wtf.ColorFor(firstWidget.CommonSettings().Colors.WidgetTheme.Background) + } + + wtfApp.pages.Box.SetBackgroundColor(bgColor) +} + func (wtfApp *WtfApp) stopAllWidgets() { for _, widget := range wtfApp.widgets { widget.Stop()