Skip to content

Commit

Permalink
Reimpliments the module configuration validation
Browse files Browse the repository at this point in the history
Now supports displaying errors from multiple widgets.
  • Loading branch information
senorprogrammer committed Jul 7, 2019
1 parent c308d1b commit b07f3c5
Show file tree
Hide file tree
Showing 65 changed files with 371 additions and 363 deletions.
25 changes: 19 additions & 6 deletions cfg/common_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Sigils struct {
type Common struct {
Colors
Module
Position `help:"Defines where in the grid this module’s widget will be displayed."`
PositionSettings `help:"Defines where in the grid this module’s widget will be displayed."`
Sigils

Enabled bool `help:"Determines whether or not this module is executed and if its data displayed onscreen." values:"true, false"`
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config
Type: moduleConfig.UString("type", name),
},

Position: NewPositionFromYAML(name, moduleConfig),
PositionSettings: NewPositionSettingsFromYAML(name, moduleConfig),

Enabled: moduleConfig.UBool("enabled", false),
RefreshInterval: moduleConfig.UInt("refreshInterval", 300),
Expand All @@ -100,6 +100,8 @@ func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config
return &common
}

/* -------------------- Exported Functions -------------------- */

func (common *Common) DefaultFocusedRowColor() string {
return fmt.Sprintf("%s:%s", common.Colors.HighlightFore, common.Colors.HighlightBack)
}
Expand All @@ -109,12 +111,11 @@ func (common *Common) DefaultRowColor() string {
}

func (common *Common) FocusChar() string {
focusChar := string('0' + common.focusChar)
if common.focusChar == -1 {
focusChar = ""
if common.focusChar <= -1 {
return ""
}

return focusChar
return string('0' + common.focusChar)
}

func (common *Common) RowColor(idx int) string {
Expand Down Expand Up @@ -143,3 +144,15 @@ func (common *Common) SigilStr(len, pos int, width int) string {

return sigils
}

// Validations aggregates all the validations from all the sub-sections in Common into a
// single array of validations
func (common *Common) Validations() []Validatable {
validatables := []Validatable{}

for _, validation := range common.PositionSettings.Validations.validations {
validatables = append(validatables, validation)
}

return validatables
}
123 changes: 0 additions & 123 deletions cfg/position.go

This file was deleted.

51 changes: 51 additions & 0 deletions cfg/position_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cfg

import (
"github.com/olebedev/config"
)

const (
positionPath = "position"
)

// PositionSettings represents the onscreen location of a widget
type PositionSettings struct {
Validations *Validations

Height int
Left int
Top int
Width int
}

// NewPositionSettingsFromYAML creates and returns a new instance of cfg.Position
func NewPositionSettingsFromYAML(moduleName string, moduleConfig *config.Config) PositionSettings {
var currVal int
var err error

validations := NewValidations()

// Parse the positional data from the config data
currVal, err = moduleConfig.Int(positionPath + ".top")
validations.append("top", newPositionValidation("top", currVal, err))

currVal, err = moduleConfig.Int(positionPath + ".left")
validations.append("left", newPositionValidation("left", currVal, err))

currVal, err = moduleConfig.Int(positionPath + ".width")
validations.append("width", newPositionValidation("width", currVal, err))

currVal, err = moduleConfig.Int(positionPath + ".height")
validations.append("height", newPositionValidation("height", currVal, err))

pos := PositionSettings{
Validations: validations,

Top: validations.valueFor("top"),
Left: validations.valueFor("left"),
Width: validations.valueFor("width"),
Height: validations.valueFor("height"),
}

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

import (
"fmt"

"github.com/logrusorgru/aurora"
)

// Common examples of invalid position configuration are:
//
// position:
// top: -3
// left: 2
// width: 0
// height: 1
//
// position:
// top: 3
// width: 2
// height: 1
//
// position:
// top: 3
// # left: 2
// width: 2
// height: 1
//
// position:
// top: 3
// left: 2
// width: 2
// height: 1
//
type positionValidation struct {
err error
name string
intVal int
}

func (posVal *positionValidation) Error() error {
return posVal.err
}

func (posVal *positionValidation) HasError() bool {
return posVal.err != nil
}

func (posVal *positionValidation) IntValue() int {
return posVal.intVal
}

// String returns the Stringer representation of the positionValidation
func (posVal *positionValidation) String() string {
return fmt.Sprintf("Invalid value for %s:\t%d", aurora.Yellow(posVal.name), posVal.intVal)
}

func newPositionValidation(name string, intVal int, err error) *positionValidation {
posVal := &positionValidation{
err: err,
name: name,
intVal: intVal,
}

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

// Validatable is implemented by any value that validates a configuration setting
type Validatable interface {
Error() error
HasError() bool
String() string
IntValue() int
}
28 changes: 28 additions & 0 deletions cfg/validations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cfg

// Validations represent a collection of config setting validations
type Validations struct {
validations map[string]Validatable
}

// NewValidations creates and returns an instance of Validations
func NewValidations() *Validations {
vals := &Validations{
validations: make(map[string]Validatable),
}

return vals
}

func (vals *Validations) append(key string, posVal Validatable) {
vals.validations[key] = posVal
}

func (vals *Validations) valueFor(key string) int {
val := vals.validations[key]
if val != nil {
return val.IntValue()
}

return 0
}
2 changes: 1 addition & 1 deletion generator/textwidget.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) display() {
widget.Redraw(widget.CommonSettings.Title, "Some text", false)
widget.Redraw(widget.CommonSettings().Title, "Some text", false)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/hekmon/transmissionrpc v0.0.0-20190525133028-1d589625bacd
github.com/jessevdk/go-flags v1.4.0
github.com/kr/pretty v0.1.0 // indirect
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mmcdole/gofeed v1.0.0-beta2.0.20190420154928-0e68beaf6fdf
github.com/olebedev/config v0.0.0-20190528211619-364964f3a8e4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946 h1:z+WaKrgu3kCpcdnbK9YG+JThpOCd1nU5jO5ToVmSlR4=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid *
config := cfg.LoadConfigFile(absPath)

widgets := maker.MakeWidgets(app, pages, config)
wtf.ValidateWidgets(widgets)
runningWidgets = widgets

wtf.ValidateWidgets(widgets)

focusTracker = wtf.NewFocusTracker(app, widgets, config)

display := wtf.NewDisplay(widgets, config)
Expand Down Expand Up @@ -152,9 +153,10 @@ func main() {
pages := tview.NewPages()

widgets := maker.MakeWidgets(app, pages, config)
wtf.ValidateWidgets(widgets)
runningWidgets = widgets

wtf.ValidateWidgets(widgets)

focusTracker = wtf.NewFocusTracker(app, widgets, config)

display := wtf.NewDisplay(widgets, config)
Expand Down
2 changes: 1 addition & 1 deletion modules/bamboohr/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (widget *Widget) Refresh() {
wtf.Now().Format(wtf.DateFormat),
)

widget.Redraw(widget.CommonSettings.Title, widget.contentFrom(todayItems), false)
widget.Redraw(widget.CommonSettings().Title, widget.contentFrom(todayItems), false)
}

/* -------------------- Unexported Functions -------------------- */
Expand Down
Loading

0 comments on commit b07f3c5

Please sign in to comment.