Skip to content

Commit 1442efb

Browse files
authored
Merge pull request #1 from unix-streamdeck/configStruct
Improve config struct
2 parents daf6c7c + 328e725 commit 1442efb

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

config.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@ package main
22

33
import "image"
44

5-
type Pages [][]struct {
6-
Icon string `json:"icon,omitempty"`
7-
SwitchPage *int `json:"switch_page,omitempty"`
8-
Text string `json:"text,omitempty"`
9-
Keybind string `json:"keybind,omitempty"`
10-
Command string `json:"command,omitempty"`
11-
Brightness *int `json:"brightness,omitempty"`
12-
Url string `json:"url,omitempty"`
13-
buff image.Image
14-
}
5+
type Page []Key
156

167
type Config struct {
17-
Pages Pages `json:"pages"`
8+
Pages []Page `json:"pages"`
189
}
1910

2011
type Key struct {

main.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ func main() {
3939
log.Fatal(err)
4040
}
4141
config, err = readConfig()
42-
if err != nil {
42+
if err != nil && !os.IsNotExist(err) {
4343
log.Fatal(err)
4444
}
45+
if len(config.Pages) == 0 {
46+
config.Pages = append(config.Pages, Page{})
47+
}
4548
cleanupHook()
4649
setPage()
4750
kch, err := dev.ReadKeys()
@@ -59,7 +62,9 @@ func main() {
5962
continue
6063
}
6164
if k.Pressed == true {
62-
handleInput(config.Pages[page][k.Index])
65+
if len(config.Pages)-1 >= page && len(config.Pages[page])-1 >= int(k.Index) {
66+
handleInput(config.Pages[page][k.Index])
67+
}
6368
}
6469
}
6570
}
@@ -86,8 +91,7 @@ func setImage(img image.Image, i int, p int) {
8691

8792
func setPage() {
8893
currentPage := config.Pages[page]
89-
for i := range currentPage {
90-
currentKey := currentPage[i]
94+
for i, currentKey := range currentPage {
9195
if currentKey.buff == nil {
9296
if currentKey.Icon == "" {
9397
img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels)))

0 commit comments

Comments
 (0)