1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Don't fail to start up if there are no widgets defined

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2021-01-03 20:07:56 -08:00
parent 7c12523139
commit 9012868b2e
3 changed files with 43 additions and 16 deletions

View File

@ -0,0 +1,3 @@
configs:
- small_config.yml
- sample_config.yml

View File

@ -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

View File

@ -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()