Skip to content

Commit

Permalink
refactor: use JSON for configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 16, 2024
1 parent 7f8241e commit 3d1d339
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 53 deletions.
51 changes: 7 additions & 44 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package main

import (
"embed"
_ "embed"
)

type Config struct {
Input string `arg:"" help:"Code to screenshot." optional:""`

Expand Down Expand Up @@ -39,50 +44,8 @@ type Font struct {
Size float64 `help:"Font size to use for code." placeholder:"14"`
}

var configs = map[string]string{
"base": `{
"window": false,
"border": {
"radius": 0,
"width": 0,
"color": "#515151"
},
"shadow": {
"blur": 0,
"x": 0,
"y": 0
},
"padding": [20, 40, 20, 20],
"margin": "0",
"background": "#FFFFFF",
"font": {
"family": "JetBrains Mono",
"size": 14
},
"line_height": 1.2
}`,
"full": `{
"window": true,
"border": {
"radius": 8,
"width": 1,
"color": "#515151"
},
"shadow": {
"blur": 24,
"x": 0,
"y": 12
},
"padding": [20, 40, 20, 20],
"margin": [50, 60, 100, 60],
"background": "#FFFFFF",
"font": {
"family": "JetBrains Mono",
"size": 14
},
"line_height": 1.2
}`,
}
//go:embed configurations/*
var configs embed.FS

func expandPadding(p []int) []int {
switch len(p) {
Expand Down
20 changes: 17 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package main

import (
"strings"
"testing"

"github.com/alecthomas/kong"
)

func TestConfig(t *testing.T) {
for _, c := range configs {
_, err := kong.JSON(strings.NewReader(c))
dir := "configurations"

entries, err := configs.ReadDir(dir)
if err != nil {
t.Fatal(err)
}

if len(entries) != 2 {
t.Fatal(entries)
}

for _, entry := range entries {
f, err := configs.Open(dir + "/" + entry.Name())
if err != nil {
t.Fatal(err)
}
_, err = kong.JSON(f)
if err != nil {
t.Fatal(err)
}
Expand Down
26 changes: 26 additions & 0 deletions configurations/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"window": false,
"border": {
"radius": 0,
"width": 0,
"color": "#515151"
},
"shadow": {
"blur": 0,
"x": 0,
"y": 0
},
"padding": [
20,
40,
20,
20
],
"margin": "0",
"background": "#FFFFFF",
"font": {
"family": "JetBrains Mono",
"size": 14
},
"line_height": 1.2
}
31 changes: 31 additions & 0 deletions configurations/full.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"window": true,
"border": {
"radius": 8,
"width": 1,
"color": "#515151"
},
"shadow": {
"blur": 24,
"x": 0,
"y": 12
},
"padding": [
20,
40,
20,
20
],
"margin": [
50,
60,
100,
60
],
"background": "#FFFFFF",
"font": {
"family": "JetBrains Mono",
"size": 14
},
"line_height": 1.2
}
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ func main() {
_ = kong.Parse(&config,
kong.Help(helpPrinter))

c, ok := configs[config.Config]
if !ok {
b, err := os.ReadFile(config.Config)
c = string(b)
c, err := configs.Open("configurations/" + config.Config + ".json")
if err != nil {
c, err = os.Open(config.Config)
if err != nil {
c = configs["base"]
c, _ = configs.Open("configurations/base.json")
}
}

r, err := kong.JSON(strings.NewReader(c))
r, err := kong.JSON(c)
if err != nil {
log.Fatal("invalid json configuration", "error", err)
}
Expand Down
3 changes: 3 additions & 0 deletions out.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3d1d339

Please sign in to comment.