From b723c15aecaeff80e69cb1726a0812d0e0188379 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Mon, 27 Jul 2020 16:14:53 +0100 Subject: [PATCH 1/2] Make a cleaner API for the same config format --- config.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index e8d45cc..e30a82e 100644 --- a/config.go +++ b/config.go @@ -2,22 +2,12 @@ package main import "image" -type Pages [][] struct { - Icon string `json:"icon,omitempty"` - SwitchPage *int `json:"switch_page,omitempty"` - Text string `json:"text,omitempty"` - Keybind string `json:"keybind,omitempty"` - Command string `json:"command,omitempty"` - Brightness *int `json:"brightness,omitempty"` - Url string `json:"url,omitempty"` - buff image.Image -} +type Page []Key type Config struct { - Pages Pages `json:"pages"` + Pages []Page `json:"pages"` } - type Key struct { Icon string `json:"icon,omitempty"` SwitchPage *int `json:"switch_page,omitempty"` From 47f06139f2dda1f53571a5684cba2022904a4ffb Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Mon, 27 Jul 2020 16:15:13 +0100 Subject: [PATCH 2/2] Let's not fail if the config file is missing --- main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 03c41d3..e2ae16b 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,12 @@ func main() { log.Fatal(err) } config, err = readConfig() - if err != nil { + if err != nil && !os.IsNotExist(err) { log.Fatal(err) } + if len(config.Pages) == 0 { + config.Pages = append(config.Pages, Page{}) + } cleanupHook() setPage() kch, err := dev.ReadKeys() @@ -57,7 +60,9 @@ func main() { continue } if k.Pressed == true { - handleInput(config.Pages[page][k.Index]) + if len(config.Pages)-1 >= page && len(config.Pages[page])-1 >= int(k.Index) { + handleInput(config.Pages[page][k.Index]) + } } } } @@ -84,8 +89,7 @@ func setImage(img image.Image, i int, p int) { func setPage() { currentPage := config.Pages[page] - for i := range currentPage { - currentKey := currentPage[i] + for i, currentKey := range currentPage { if currentKey.Icon == "" { img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels))) draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{0, 0, 0, 255}), image.ZP, draw.Src)