Skip to content

Commit 640fa66

Browse files
authored
feat: add ability use local info.json file (#9)
1 parent 1d6d977 commit 640fa66

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

internal/lib/generate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ func drawLayout(ctx *gg.Context, transparent bool, layout keyboard.Layout) error
6464
for _, key := range layout.Layout {
6565
x := key.X*keySize + spacer*key.X + spacer
6666
y := key.Y*keySize + spacer*key.Y + (fontSize + spacer*2)
67-
67+
w := keySize
68+
h := keySize
6869
if key.H != nil {
69-
ctx.DrawRoundedRectangle(x, y, key.W*keySize, *key.H*keySize, spacer)
70-
} else {
71-
ctx.DrawRoundedRectangle(x, y, key.W*keySize, keySize, spacer)
70+
h = *key.H * keySize
7271
}
73-
72+
if key.W != nil {
73+
w = *key.W * keySize
74+
}
75+
ctx.DrawRoundedRectangle(x, y, w, h, spacer)
7476
ctx.SetRGB(0., 0., 0.)
7577
ctx.StrokePreserve()
7678
ctx.SetRGB(1.0, 1.0, 1.0)

internal/lib/root.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ type GenerateCmd struct {
1515
KeyboardName string `arg:"" help:"Keyboard name to fetch layout."`
1616

1717
File string `optional:"" short:"f" type:"existingfile" help:"ZMK .keymap file"`
18+
LayoutFile string `optional:"" short:"l" type:"layoutfile" help:"info.json file"`
1819
Transparent bool `optional:"" short:"t" help:"Use a transparent background."`
1920
Output string `optional:"" short:"o" type:"existingdir" default:"." help:"Output directory."`
2021
}
2122

2223
func (g *GenerateCmd) Run() error {
2324
images := make(map[string]image.Image)
2425

25-
keyboardInfo, err := keyboard.Fetch(g.KeyboardName)
26+
var keyboardInfo keyboard.Layouts
27+
var err error
28+
if g.LayoutFile != "" {
29+
keyboardInfo, err = keyboard.LoadFile(g.KeyboardName, g.LayoutFile)
30+
} else {
31+
keyboardInfo, err = keyboard.Fetch(g.KeyboardName)
32+
}
33+
2634
if err != nil {
2735
return err
2836
}
37+
2938
g.KeyboardName = strings.ReplaceAll(g.KeyboardName, "/", "_")
3039

3140
for layoutName, layout := range keyboardInfo {

pkg/keyboard/keyboard.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
"os"
89
"time"
910

1011
"github.com/rs/zerolog/log"
@@ -13,7 +14,7 @@ import (
1314
type Key struct {
1415
X float64 `json:"x"`
1516
Y float64 `json:"y"`
16-
W float64 `json:"w"`
17+
W *float64 `json:"w"`
1718
H *float64 `json:"h"`
1819
Label string `json:"label"`
1920
}
@@ -72,6 +73,19 @@ func fetch(url string) (*file, error) {
7273
return &f, nil
7374
}
7475

76+
func loadFile(path string) (*Keyboard, error) {
77+
data, err := os.ReadFile(path)
78+
if err != nil {
79+
return nil, err
80+
}
81+
f := Keyboard{}
82+
err = json.Unmarshal(data, &f)
83+
if err != nil {
84+
return nil, err
85+
}
86+
return &f, nil
87+
}
88+
7589
func Fetch(name string) (Layouts, error) {
7690
log.Debug().Str("name", name).Send()
7791
url := "https://keyboards.qmk.fm/v1/keyboards/%v/info.json"
@@ -84,3 +98,15 @@ func Fetch(name string) (Layouts, error) {
8498
l := f.Keyboards[name].Layouts
8599
return l, nil
86100
}
101+
102+
func LoadFile(name, path string) (Layouts, error) {
103+
log.Debug().Str("name", name).Send()
104+
log.Debug().Str("path", path).Send()
105+
f, err := loadFile(path)
106+
if err != nil {
107+
return nil, err
108+
}
109+
l := f.Layouts
110+
return l, nil
111+
112+
}

0 commit comments

Comments
 (0)