From d76f6807ccaaa8af63dcdc9f322555dd1312eda3 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 20:36:03 -0700 Subject: [PATCH 1/6] Redraw when a Transmission torrent is removed --- modules/transmission/widget.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/transmission/widget.go b/modules/transmission/widget.go index d53297e1..c31be5e8 100644 --- a/modules/transmission/widget.go +++ b/modules/transmission/widget.go @@ -125,6 +125,8 @@ func (widget *Widget) deleteSelectedTorrent() { } widget.client.TorrentRemove(removePayload) + + widget.display() } // pauseUnpauseTorrent either pauses or unpauses the downloading and seeding of the selected torrent From 08c7e768c0935b877ed59dfcf8e273869d464177 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 21:17:39 -0700 Subject: [PATCH 2/6] WTF-482 Sanity-check position configuration data for modules If a module is missing any of the positional data it now informs the user and exits gracefully with an error. --- cfg/common_settings.go | 21 +++++------- cfg/config_files.go | 1 + cfg/position.go | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 cfg/position.go diff --git a/cfg/common_settings.go b/cfg/common_settings.go index 12a58026..ce1be396 100644 --- a/cfg/common_settings.go +++ b/cfg/common_settings.go @@ -30,12 +30,12 @@ type Module struct { Type string } -type Position struct { - Height int - Left int - Top int - Width int -} +// type Position struct { +// Height int +// Left int +// Top int +// Width int +// } type Sigils struct { Checkbox struct { @@ -64,7 +64,7 @@ type Common struct { func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config.Config, globalSettings *config.Config) *Common { colorsConfig, _ := globalSettings.Get("wtf.colors") - positionPath := "position" + // positionPath := "position" sigilsPath := "wtf.sigils" common := Common{ @@ -86,12 +86,7 @@ func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config Type: moduleConfig.UString("type", name), }, - Position: Position{ - Height: moduleConfig.UInt(positionPath + ".height"), - Left: moduleConfig.UInt(positionPath + ".left"), - Top: moduleConfig.UInt(positionPath + ".top"), - Width: moduleConfig.UInt(positionPath + ".width"), - }, + Position: NewPositionFromYAML(name, moduleConfig), Enabled: moduleConfig.UBool("enabled", false), RefreshInterval: moduleConfig.UInt("refreshInterval", 300), diff --git a/cfg/config_files.go b/cfg/config_files.go index 19f46ecf..fb5a0b6d 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -132,6 +132,7 @@ func LoadConfigFile(filePath string) *config.Config { fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a syntax error. Try running it through http://www.yamllint.com to check for errors.") fmt.Println() fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) + os.Exit(1) } diff --git a/cfg/position.go b/cfg/position.go new file mode 100644 index 00000000..7bf65fad --- /dev/null +++ b/cfg/position.go @@ -0,0 +1,73 @@ +package cfg + +import ( + "fmt" + "os" + "strings" + + "github.com/olebedev/config" +) + +const ( + positionPath = "position" +) + +// Position represents the onscreen location of a widget +type Position struct { + Height int + Left int + Top int + Width int +} + +// NewPositionFromYAML creates and returns a new instance of Position +func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { + errs := make(map[string]error) + + // Parse the positional data from the config data + top, err := moduleConfig.Int(positionPath + ".top") + errs["top"] = err + + left, err := moduleConfig.Int(positionPath + ".left") + errs["left"] = err + + width, err := moduleConfig.Int(positionPath + ".width") + errs["width"] = err + + height, err := moduleConfig.Int(positionPath + ".height") + errs["height"] = err + + validatePositions(name, errs) + + pos := Position{ + Top: top, + Left: left, + Width: width, + Height: height, + } + + return pos +} + +/* -------------------- Unexported Functions -------------------- */ + +// If any of the position values have an error then we inform the user and exit the app +func validatePositions(name string, errs map[string]error) { + var errStr string + + for pos, err := range errs { + if err != nil { + errStr += fmt.Sprintf(" - Invalid value for %s\n", pos) + } + } + + if errStr != "" { + fmt.Println() + fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(name)) + fmt.Println(errStr) + fmt.Println("Please check your config.yml file.") + fmt.Println() + + os.Exit(1) + } +} From ca81aa51342365ecdf07f9d74d1b8d64d453d762 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 21:19:23 -0700 Subject: [PATCH 3/6] WTF-482 Fix redundant newline warning --- cfg/config_files.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index fb5a0b6d..50a73f04 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -126,8 +126,10 @@ func LoadConfigFile(filePath string) *config.Config { cfg, err := config.ParseYamlFile(absPath) if err != nil { - fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.\n") - fmt.Println("This could mean one of two things:\n") + fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.") + fmt.Println() + fmt.Println("This could mean one of two things:") + fmt.Println() fmt.Println(" 1. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.") fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a syntax error. Try running it through http://www.yamllint.com to check for errors.") fmt.Println() From 5a39bc00ce7519117ef9913a1037ca8de4021369 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 21:22:08 -0700 Subject: [PATCH 4/6] WTF-482 Remove commented-out code --- cfg/common_settings.go | 8 -------- cfg/position.go | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cfg/common_settings.go b/cfg/common_settings.go index ce1be396..a78a3394 100644 --- a/cfg/common_settings.go +++ b/cfg/common_settings.go @@ -30,13 +30,6 @@ type Module struct { Type string } -// type Position struct { -// Height int -// Left int -// Top int -// Width int -// } - type Sigils struct { Checkbox struct { Checked string @@ -64,7 +57,6 @@ type Common struct { func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config.Config, globalSettings *config.Config) *Common { colorsConfig, _ := globalSettings.Get("wtf.colors") - // positionPath := "position" sigilsPath := "wtf.sigils" common := Common{ diff --git a/cfg/position.go b/cfg/position.go index 7bf65fad..619af555 100644 --- a/cfg/position.go +++ b/cfg/position.go @@ -52,6 +52,25 @@ func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { /* -------------------- Unexported Functions -------------------- */ // If any of the position values have an error then we inform the user and exit the app +// Common examples of invalid position configuration are: +// +// 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 +// func validatePositions(name string, errs map[string]error) { var errStr string From 774c465f57589293408dbf3625bd42ebb535812a Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 21:28:09 -0700 Subject: [PATCH 5/6] WTF-482 More explicit param name in position.go --- cfg/position.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cfg/position.go b/cfg/position.go index 619af555..92c90cf6 100644 --- a/cfg/position.go +++ b/cfg/position.go @@ -21,7 +21,7 @@ type Position struct { } // NewPositionFromYAML creates and returns a new instance of Position -func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { +func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Position { errs := make(map[string]error) // Parse the positional data from the config data @@ -37,7 +37,7 @@ func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { height, err := moduleConfig.Int(positionPath + ".height") errs["height"] = err - validatePositions(name, errs) + validatePositions(moduleName, errs) pos := Position{ Top: top, @@ -71,7 +71,7 @@ func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { // width: 2 // height: 1 // -func validatePositions(name string, errs map[string]error) { +func validatePositions(moduleName string, errs map[string]error) { var errStr string for pos, err := range errs { @@ -82,7 +82,7 @@ func validatePositions(name string, errs map[string]error) { if errStr != "" { fmt.Println() - fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(name)) + fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(moduleName)) fmt.Println(errStr) fmt.Println("Please check your config.yml file.") fmt.Println() From 0104f97805a10a7ad3add35bff8aa88dfdb1bdd7 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 5 Jul 2019 22:21:50 -0700 Subject: [PATCH 6/6] Use a map to return the failed position value and error message --- cfg/position.go | 69 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/cfg/position.go b/cfg/position.go index 92c90cf6..fc59c23b 100644 --- a/cfg/position.go +++ b/cfg/position.go @@ -12,6 +12,35 @@ const ( positionPath = "position" ) +/* -------------------- Position Validation -------------------- */ + +type positionValidation struct { + err error + name string + val int +} + +func (posVal *positionValidation) hasError() bool { + return posVal.err != nil +} + +// String returns the Stringer representation of the positionValidation +func (posVal *positionValidation) String() string { + return fmt.Sprintf("Invalid value for %s:\t%d", posVal.name, posVal.val) +} + +func newPositionValidation(name string, val int, err error) *positionValidation { + posVal := &positionValidation{ + err: err, + name: name, + val: val, + } + + return posVal +} + +/* -------------------- Position -------------------- */ + // Position represents the onscreen location of a widget type Position struct { Height int @@ -22,28 +51,30 @@ type Position struct { // NewPositionFromYAML creates and returns a new instance of Position func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Position { - errs := make(map[string]error) + var val int + var err error + validations := make(map[string]*positionValidation) // Parse the positional data from the config data - top, err := moduleConfig.Int(positionPath + ".top") - errs["top"] = err + val, err = moduleConfig.Int(positionPath + ".top") + validations["top"] = newPositionValidation("top", val, err) - left, err := moduleConfig.Int(positionPath + ".left") - errs["left"] = err + val, err = moduleConfig.Int(positionPath + ".left") + validations["left"] = newPositionValidation("left", val, err) - width, err := moduleConfig.Int(positionPath + ".width") - errs["width"] = err + val, err = moduleConfig.Int(positionPath + ".width") + validations["width"] = newPositionValidation("width", val, err) - height, err := moduleConfig.Int(positionPath + ".height") - errs["height"] = err + val, err = moduleConfig.Int(positionPath + ".height") + validations["height"] = newPositionValidation("height", val, err) - validatePositions(moduleName, errs) + validatePositions(moduleName, validations) pos := Position{ - Top: top, - Left: left, - Width: width, - Height: height, + Top: validations["top"].val, + Left: validations["left"].val, + Width: validations["width"].val, + Height: validations["height"].val, } return pos @@ -71,18 +102,18 @@ func NewPositionFromYAML(moduleName string, moduleConfig *config.Config) Positio // width: 2 // height: 1 // -func validatePositions(moduleName string, errs map[string]error) { +func validatePositions(moduleName string, validations map[string]*positionValidation) { var errStr string - for pos, err := range errs { - if err != nil { - errStr += fmt.Sprintf(" - Invalid value for %s\n", pos) + for _, posVal := range validations { + if posVal.hasError() { + errStr += fmt.Sprintf(" - %s.\t\033[0;31mError\033[0m %v\n", posVal, posVal.err) } } if errStr != "" { fmt.Println() - fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(moduleName)) + fmt.Printf("\033[0;1mErrors in %s position configuration\033[0m\n", strings.Title(moduleName)) fmt.Println(errStr) fmt.Println("Please check your config.yml file.") fmt.Println()