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:
parent
7c12523139
commit
9012868b2e
3
_sample_configs/multi_config.yml
Normal file
3
_sample_configs/multi_config.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
configs:
|
||||||
|
- small_config.yml
|
||||||
|
- sample_config.yml
|
@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
@ -9,24 +10,28 @@ import (
|
|||||||
|
|
||||||
// Display is the container for the onscreen representation of a WtfApp
|
// Display is the container for the onscreen representation of a WtfApp
|
||||||
type Display struct {
|
type Display struct {
|
||||||
Grid *tview.Grid
|
Grid *tview.Grid
|
||||||
config *config.Config
|
|
||||||
|
bgColor tcell.Color
|
||||||
|
config *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDisplay creates and returns a Display
|
// NewDisplay creates and returns a Display
|
||||||
func NewDisplay(widgets []wtf.Wtfable, config *config.Config) *Display {
|
func NewDisplay(widgets []wtf.Wtfable, config *config.Config) *Display {
|
||||||
display := Display{
|
display := Display{
|
||||||
Grid: tview.NewGrid(),
|
Grid: tview.NewGrid(),
|
||||||
config: config,
|
|
||||||
|
bgColor: wtf.ColorFor("black"),
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
firstWidget := widgets[0]
|
// Make sure we have some widgets before trying to get the first one
|
||||||
display.Grid.SetBackgroundColor(
|
if len(widgets) > 0 {
|
||||||
wtf.ColorFor(
|
firstWidget := widgets[0]
|
||||||
firstWidget.CommonSettings().Colors.WidgetTheme.Background,
|
display.bgColor = wtf.ColorFor(firstWidget.CommonSettings().Colors.WidgetTheme.Background)
|
||||||
),
|
}
|
||||||
)
|
|
||||||
|
|
||||||
|
display.Grid.SetBackgroundColor(display.bgColor)
|
||||||
display.build(widgets)
|
display.build(widgets)
|
||||||
|
|
||||||
return &display
|
return &display
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -60,12 +61,7 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat
|
|||||||
|
|
||||||
wtfApp.validator.Validate(wtfApp.widgets)
|
wtfApp.validator.Validate(wtfApp.widgets)
|
||||||
|
|
||||||
firstWidget := wtfApp.widgets[0]
|
wtfApp.setBackgroundColor()
|
||||||
wtfApp.pages.Box.SetBackgroundColor(
|
|
||||||
wtf.ColorFor(
|
|
||||||
firstWidget.CommonSettings().Colors.WidgetTheme.Background,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
wtfApp.TViewApp.SetInputCapture(wtfApp.keyboardIntercept)
|
wtfApp.TViewApp.SetInputCapture(wtfApp.keyboardIntercept)
|
||||||
wtfApp.TViewApp.SetRoot(wtfApp.pages, true)
|
wtfApp.TViewApp.SetRoot(wtfApp.pages, true)
|
||||||
@ -75,6 +71,16 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat
|
|||||||
|
|
||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- 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
|
// Run starts the underlying tview app
|
||||||
func (wtfApp *WtfApp) Run() {
|
func (wtfApp *WtfApp) Run() {
|
||||||
if err := wtfApp.TViewApp.Run(); err != nil {
|
if err := wtfApp.TViewApp.Run(); err != nil {
|
||||||
@ -99,6 +105,19 @@ func (wtfApp *WtfApp) Stop() {
|
|||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- 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() {
|
func (wtfApp *WtfApp) stopAllWidgets() {
|
||||||
for _, widget := range wtfApp.widgets {
|
for _, widget := range wtfApp.widgets {
|
||||||
widget.Stop()
|
widget.Stop()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user