Skip to content

Commit

Permalink
WTF-730 Fix missing color key config error (wtfutil#738)
Browse files Browse the repository at this point in the history
* WTF-730 Fix missing color key config error

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Add Subheading color formatting to modules

Users can now set a `subheading` color in their config to change the
color of subheadings in widget display.

Defaults to `red`.

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Fix oustanding color issues

Clean up missing color config changes not addressed in earlier commits.

Signed-off-by: Chris Cummer <[email protected]>

* Remove unused dependency

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Base cleanup

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Fix a few bugs related to color config changes

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Fix issues with PagerDuty subheading display

Signed-off-by: Chris Cummer <[email protected]>

* WTF-730 Fix bug with Todo list colour rendering

Signed-off-by: Chris Cummer <[email protected]>
  • Loading branch information
senorprogrammer authored Nov 9, 2019
1 parent 1bfca29 commit 200dbcc
Show file tree
Hide file tree
Showing 33 changed files with 366 additions and 137 deletions.
21 changes: 21 additions & 0 deletions _sample_configs/dynamic_sizing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
wtf:
mods:
battery:
type: power
title: "⚡️"
enabled: true
position:
top: 0
left: 0
height: 1
width: 1
refreshInterval: 15
security_info:
type: security
enabled: true
position:
top: 0
left: 1
height: 1
width: 1
refreshInterval: 3600
8 changes: 7 additions & 1 deletion app/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ func NewDisplay(widgets []wtf.Wtfable, config *config.Config) *Display {
config: config,
}

display.Grid.SetBackgroundColor(wtf.ColorFor(config.UString("wtf.colors.background", "transparent")))
firstWidget := widgets[0]
display.Grid.SetBackgroundColor(
wtf.ColorFor(
firstWidget.CommonSettings().Colors.WidgetTheme.Background,
),
)

display.build(widgets)

return &display
Expand Down
12 changes: 10 additions & 2 deletions app/focus_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ func (tracker *FocusTracker) blur(idx int) {
view := widget.TextView()
view.Blur()

view.SetBorderColor(wtf.ColorFor(widget.BorderColor()))
view.SetBorderColor(
wtf.ColorFor(
widget.BorderColor(),
),
)

tracker.IsFocused = false
}
Expand All @@ -178,7 +182,11 @@ func (tracker *FocusTracker) focus(idx int) {
}

view := widget.TextView()
view.SetBorderColor(wtf.ColorFor(tracker.config.UString("wtf.colors.border.focused", "gray")))
view.SetBorderColor(
wtf.ColorFor(
widget.CommonSettings().Colors.BorderTheme.Focused,
),
)
tracker.App.SetFocus(view)
}

Expand Down
9 changes: 7 additions & 2 deletions app/wtf_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func NewWtfApp(app *tview.Application, config *config.Config, configFilePath str
return false
})

wtfApp.pages.Box.SetBackgroundColor(wtf.ColorFor(config.UString("wtf.colors.background", "transparent")))

wtfApp.app.SetInputCapture(wtfApp.keyboardIntercept)
wtfApp.widgets = MakeWidgets(wtfApp.app, wtfApp.pages, wtfApp.config)
wtfApp.display = NewDisplay(wtfApp.widgets, wtfApp.config)
Expand All @@ -54,6 +52,13 @@ func NewWtfApp(app *tview.Application, config *config.Config, configFilePath str

wtfApp.validator.Validate(wtfApp.widgets)

firstWidget := wtfApp.widgets[0]
wtfApp.pages.Box.SetBackgroundColor(
wtf.ColorFor(
firstWidget.CommonSettings().Colors.WidgetTheme.Background,
),
)

return &wtfApp
}

Expand Down
89 changes: 49 additions & 40 deletions cfg/common_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ import (
"github.com/olebedev/config"
)

type Colors struct {
Background string
BorderFocusable string
BorderFocused string
BorderNormal string
Checked string
Foreground string
HighlightBack string
HighlightFore string
Text string
Title string

Rows struct {
Even string
Odd string
}
}

type Module struct {
Name string
Type string
Expand All @@ -42,11 +24,11 @@ type Sigils struct {
}

type Common struct {
Colors
Module
PositionSettings `help:"Defines where in the grid this module’s widget will be displayed."`
Sigils

Colors ColorTheme
Bordered bool `help:"Whether or not the module should be displayed with a border." values:"true, false" optional:"true" default:"true"`
Enabled bool `help:"Whether or not this module is executed and if its data displayed onscreen." values:"true, false" optional:"true" default:"false"`
Focusable bool `help:"Whether or not this module is focusable." values:"true, false" optional:"true" default:"false"`
Expand All @@ -57,23 +39,44 @@ type Common struct {
focusChar int `help:"Define one of the number keys as a short cut key to access the widget." optional:"true"`
}

// NewCommonSettingsFromModule returns a common settings configuration tailed to the given module
func NewCommonSettingsFromModule(name, defaultTitle string, defaultFocusable bool, moduleConfig *config.Config, globalSettings *config.Config) *Common {
colorsConfig, _ := globalSettings.Get("wtf.colors")
sigilsPath := "wtf.sigils"
baseColors := NewDefaultColorTheme()

colorsConfig, err := globalSettings.Get("wtf.colors")
if err != nil && strings.Contains(err.Error(), "Nonexistent map") {
// Create a default colors config to fill in for the missing one
// This comes into play when the configuration file does not contain a `colors:` key, i.e:
//
// wtf:
// # colors: <- missing
// refreshInterval: 1
// openFileUtil: "open"
//
colorsConfig, _ = NewDefaultColorConfig()
}

// And finally create a third instance to be the final default fallback in case there are empty or nil values in
// the colors extracted from the config file (aka colorsConfig)
defaultColorTheme := NewDefaultColorTheme()

baseColors.BorderTheme.Focusable = moduleConfig.UString("colors.border.focusable", colorsConfig.UString("border.focusable", defaultColorTheme.BorderTheme.Focusable))
baseColors.BorderTheme.Focused = moduleConfig.UString("colors.border.focused", colorsConfig.UString("border.focused", defaultColorTheme.BorderTheme.Focused))
baseColors.BorderTheme.Unfocusable = moduleConfig.UString("colors.border.normal", colorsConfig.UString("border.normal", defaultColorTheme.BorderTheme.Unfocusable))

baseColors.CheckboxTheme.Checked = moduleConfig.UString("colors.checked", colorsConfig.UString("checked", defaultColorTheme.CheckboxTheme.Checked))

baseColors.RowTheme.EvenForeground = moduleConfig.UString("colors.rows.even", colorsConfig.UString("rows.even", defaultColorTheme.RowTheme.EvenForeground))
baseColors.RowTheme.OddForeground = moduleConfig.UString("colors.rows.odd", colorsConfig.UString("rows.odd", defaultColorTheme.RowTheme.OddForeground))

baseColors.TextTheme.Subheading = moduleConfig.UString("colors.subheading", colorsConfig.UString("subheading", defaultColorTheme.TextTheme.Subheading))
baseColors.TextTheme.Text = moduleConfig.UString("colors.text", colorsConfig.UString("text", defaultColorTheme.TextTheme.Text))
baseColors.TextTheme.Title = moduleConfig.UString("colors.title", colorsConfig.UString("title", defaultColorTheme.TextTheme.Title))

baseColors.WidgetTheme.Background = moduleConfig.UString("colors.background", colorsConfig.UString("background", defaultColorTheme.WidgetTheme.Background))

common := Common{
Colors: Colors{
Background: moduleConfig.UString("colors.background", colorsConfig.UString("background", "transparent")),
BorderFocusable: moduleConfig.UString("colors.border.focusable", colorsConfig.UString("border.focusable", "red")),
BorderFocused: moduleConfig.UString("colors.border.focused", colorsConfig.UString("border.focused", "orange")),
BorderNormal: moduleConfig.UString("colors.border.normal", colorsConfig.UString("border.normal", "gray")),
Checked: moduleConfig.UString("colors.checked", colorsConfig.UString("checked", "white")),
Foreground: moduleConfig.UString("colors.foreground", colorsConfig.UString("foreground", "white")),
HighlightFore: moduleConfig.UString("colors.highlight.fore", colorsConfig.UString("highlight.fore", "black")),
HighlightBack: moduleConfig.UString("colors.highlight.back", colorsConfig.UString("highlight.back", "green")),
Text: moduleConfig.UString("colors.text", colorsConfig.UString("text", "white")),
Title: moduleConfig.UString("colors.title", colorsConfig.UString("title", "white")),
},
Colors: baseColors,

Module: Module{
Name: name,
Expand All @@ -92,12 +95,10 @@ func NewCommonSettingsFromModule(name, defaultTitle string, defaultFocusable boo
focusChar: moduleConfig.UInt("focusChar", -1),
}

common.Colors.Rows.Even = moduleConfig.UString("colors.rows.even", colorsConfig.UString("rows.even", "white"))
common.Colors.Rows.Odd = moduleConfig.UString("colors.rows.odd", colorsConfig.UString("rows.odd", "lightblue"))
sigilsPath := "wtf.sigils"

common.Sigils.Checkbox.Checked = globalSettings.UString(sigilsPath+".checkbox.checked", "x")
common.Sigils.Checkbox.Unchecked = globalSettings.UString(sigilsPath+".checkbox.unchecked", " ")

common.Sigils.Paging.Normal = globalSettings.UString(sigilsPath+".paging.normal", globalSettings.UString("wtf.paging.pageSigil", "*"))
common.Sigils.Paging.Selected = globalSettings.UString(sigilsPath+".paging.select", globalSettings.UString("wtf.paging.selectedSigil", "_"))

Expand All @@ -107,11 +108,19 @@ func NewCommonSettingsFromModule(name, defaultTitle string, defaultFocusable boo
/* -------------------- Exported Functions -------------------- */

func (common *Common) DefaultFocusedRowColor() string {
return fmt.Sprintf("%s:%s", common.Colors.HighlightFore, common.Colors.HighlightBack)
return fmt.Sprintf(
"%s:%s",
common.Colors.RowTheme.HighlightedForeground,
common.Colors.RowTheme.HighlightedBackground,
)
}

func (common *Common) DefaultRowColor() string {
return fmt.Sprintf("%s:%s", common.Colors.Foreground, common.Colors.Background)
return fmt.Sprintf(
"%s:%s",
common.Colors.RowTheme.EvenForeground,
common.Colors.RowTheme.EvenBackground,
)
}

func (common *Common) FocusChar() string {
Expand All @@ -124,10 +133,10 @@ func (common *Common) FocusChar() string {

func (common *Common) RowColor(idx int) string {
if idx%2 == 0 {
return common.Colors.Rows.Even
return common.Colors.RowTheme.EvenForeground
}

return common.Colors.Rows.Odd
return common.Colors.RowTheme.OddForeground
}

func (common *Common) RightAlignFormat(width int) string {
Expand Down
106 changes: 106 additions & 0 deletions cfg/default_color_theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cfg

import (
"github.com/olebedev/config"
"gopkg.in/yaml.v2"
)

// BorderTheme defines the default color scheme for drawing widget borders
type BorderTheme struct {
Focusable string
Focused string
Unfocusable string
}

// CheckboxTheme defines the default color scheme for drawing checkable rows in widgets
type CheckboxTheme struct {
Checked string
}

// RowTheme defines the default color scheme for row text
type RowTheme struct {
EvenBackground string
EvenForeground string

OddBackground string
OddForeground string

HighlightedBackground string
HighlightedForeground string
}

// TextTheme defines the default color scheme for text rendering
type TextTheme struct {
Subheading string
Text string
Title string
}

type WidgetTheme struct {
Background string
}

// ColorTheme is an alamgam of all the default color settings
type ColorTheme struct {
BorderTheme
CheckboxTheme
RowTheme
TextTheme
WidgetTheme
}

// NewDefaultColorTheme creates and returns an instance of DefaultColorTheme
func NewDefaultColorTheme() ColorTheme {
defaultTheme := ColorTheme{
BorderTheme: BorderTheme{
Focusable: "blue",
Focused: "orange",
Unfocusable: "gray",
},

CheckboxTheme: CheckboxTheme{
Checked: "gray",
},

RowTheme: RowTheme{
EvenBackground: "transparent",
EvenForeground: "white",

OddBackground: "transparent",
OddForeground: "lightblue",

HighlightedForeground: "black",
HighlightedBackground: "green",
},

TextTheme: TextTheme{
Subheading: "red",
Text: "white",
Title: "green",
},

WidgetTheme: WidgetTheme{
Background: "transparent",
},
}

return defaultTheme
}

// NewDefaultColorConfig creates and returns a config.Config-compatible configuration struct
// using a DefaultColorTheme to pre-populate all the relevant values
func NewDefaultColorConfig() (*config.Config, error) {
colorTheme := NewDefaultColorTheme()

yamlBytes, err := yaml.Marshal(colorTheme)
if err != nil {
return nil, err
}

cfg, err := config.ParseYamlBytes(yamlBytes)
if err != nil {
return nil, err
}

return cfg, nil
}
26 changes: 26 additions & 0 deletions cfg/default_color_theme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cfg

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_NewDefaultColorTheme(t *testing.T) {
theme := NewDefaultColorTheme()

assert.Equal(t, "orange", theme.BorderTheme.Focused)
assert.Equal(t, "red", theme.TextTheme.Subheading)
assert.Equal(t, "transparent", theme.WidgetTheme.Background)
}

func Test_NewDefaultColorConfig(t *testing.T) {
cfg, err := NewDefaultColorConfig()

assert.Nil(t, err)

assert.Equal(t, "orange", cfg.UString("bordertheme.focused"))
assert.Equal(t, "red", cfg.UString("texttheme.subheading"))
assert.Equal(t, "transparent", cfg.UString("widgettheme.background"))
assert.Equal(t, "", cfg.UString("widgettheme.missing"))
}
2 changes: 1 addition & 1 deletion cfg/position_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/alecthomas/assert"
"github.com/stretchr/testify/assert"
)

var (
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/VictorAvelar/devto-api-go v1.0.0
github.com/adlio/trello v1.4.0
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
github.com/alecthomas/chroma v0.6.8
github.com/andygrunwald/go-gerrit v0.0.0-20190825170856-5959a9bf9ff8
github.com/briandowns/openweathermap v0.0.0-20180804155945-5f41b7c9d92d
Expand Down
5 changes: 4 additions & 1 deletion modules/datadog/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ func (widget *Widget) content() (string, string, bool) {
if len(triggeredMonitors) > 0 {
str += fmt.Sprintf(
" %s\n",
"[red]Triggered Monitors[white]",
fmt.Sprintf(
"[%s]Triggered Monitors[white]",
widget.settings.common.Colors.Subheading,
),
)
for idx, triggeredMonitor := range triggeredMonitors {
row := fmt.Sprintf(`[%s][red] %s[%s]`,
Expand Down
Loading

0 comments on commit 200dbcc

Please sign in to comment.