Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to templates #4

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions internal/lib/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"

"github.com/mrmarble/zmk-viewer/internal/template"
"github.com/mrmarble/zmk-viewer/pkg/keyboard"
"github.com/rs/zerolog/log"
)
Expand All @@ -17,14 +18,22 @@ type GenerateCmd struct {
File string `optional:"" short:"f" type:"existingfile" help:"ZMK .keymap file"`
Transparent bool `optional:"" short:"t" help:"Use a transparent background."`
Output string `optional:"" short:"o" type:"existingdir" default:"." help:"Output directory."`
Template string `optional:"" type:"existingfile" help:"Template to generate Layout"`
}

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

keyboardInfo, err := keyboard.Fetch(g.KeyboardName)
if err != nil {
return err
var keyboardInfo keyboard.Layouts
var err error

if g.Template != "" {
keyboardInfo, err = fromTemplate(g.Template)
} else {
keyboardInfo, err = fromRemote(g.KeyboardName)
if err != nil {
return err
}
}

for _, layout := range keyboardInfo {
Expand Down Expand Up @@ -64,3 +73,21 @@ func (g *GenerateCmd) Run() error {

return nil
}

func fromTemplate(name string) (keyboard.Layouts, error) {
tpl, err := template.FromFile(name)
if err != nil {
return nil, err
}
l := keyboard.Layouts{}
l[tpl.Keyboard.Name] = keyboard.FromTemplate(*tpl)
return l, nil
}

func fromRemote(name string) (keyboard.Layouts, error) {
keyboardInfo, err := keyboard.Fetch(name)
if err != nil {
return nil, err
}
return keyboardInfo, nil
}
49 changes: 49 additions & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package template

import (
"os"

"gopkg.in/yaml.v3"
)

type Template struct {
Keyboard Keyboard
}

type Keyboard struct {
Name string
Columns []Column
Keys []Key
Mirror bool
}

type Column struct {
Keys int
Step float64
}

type Key struct {
Column int
Row int
Step float64
H float64
W float64
}

func FromFile(name string) (*Template, error) {
data, err := os.ReadFile(name)
if err != nil {
return nil, err
}
return Parse(data)
}

func Parse(data []byte) (*Template, error) {
tpl := Template{}

err := yaml.Unmarshal(data, &tpl)
if err != nil {
return nil, err
}
return &tpl, nil
}
27 changes: 27 additions & 0 deletions internal/template/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package template_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/mrmarble/zmk-viewer/internal/template"
)

func Diff(t *testing.T, x interface{}, y interface{}) {
t.Helper()

diff := cmp.Diff(x, y)
if diff != "" {
t.Fatalf(diff)
}
}

func TestParse(t *testing.T) {
tpl, err := template.FromFile("testdata/template.yaml.golden")
if err != nil {
t.Fatal(err)
}
Diff(t, tpl.Keyboard.Mirror, false)
Diff(t, len(tpl.Keyboard.Columns), 2)
Diff(t, tpl.Keyboard.Columns[0].Step, 0)
}
4 changes: 4 additions & 0 deletions internal/template/testdata/template.yaml.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
keyboard:
columns:
- keys: 2
- keys: 2
46 changes: 46 additions & 0 deletions pkg/keyboard/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"time"

"github.com/mrmarble/zmk-viewer/internal/template"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -84,3 +85,48 @@ func Fetch(name string) (Layouts, error) {
l := f.Keyboards[name].Layouts
return l, nil
}

func FromTemplate(tpl template.Template) Layout {
layout := Layout{}
lastColumn := 0

generate := func(offset, last int) {
for x, column := range tpl.Keyboard.Columns {
for y := 0; y < column.Keys; y++ {
var h float64 = 1
layout.Layout = append(layout.Layout, Key{X: float64(x + offset), Y: float64(y) + column.Step, W: 1, H: &h})
}
lastColumn++
}
for _, key := range tpl.Keyboard.Keys {
w := key.W
h := key.H

if w == 0 { // TODO: Implement custom Unmarshaller to handle this
w = 1
}
if h == 0 {
h = 1
}
layout.Layout = append(layout.Layout, Key{X: float64(key.Column + offset - last), Y: float64(key.Row) + key.Step, H: &h, W: w})
if key.Column > lastColumn {
lastColumn = key.Column
}
}
}
generate(0, 0)
if tpl.Keyboard.Mirror {
tpl.Keyboard.Columns = reverse(tpl.Keyboard.Columns)
generate(lastColumn+3, lastColumn+1)
}
return layout
}

func reverse(columns []template.Column) []template.Column {
rev := []template.Column{}
for i := range columns {
// reverse the order
rev = append(rev, columns[len(columns)-1-i])
}
return rev
}