Skip to content

Commit

Permalink
Merge pull request #87 from owulveryck/wtg2svglive
Browse files Browse the repository at this point in the history
feat: add width and height as env variables
  • Loading branch information
owulveryck authored Mar 14, 2023
2 parents d9012da + 52ffd09 commit 8ca07fb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
2 changes: 2 additions & 0 deletions examples/wtg2svglive/assets/websocket.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var gvar = this;
let socket = new WebSocket("ws://localhost:8080/ws");

socket.onopen = function(e) {
Expand All @@ -9,6 +10,7 @@ socket.onmessage = function(event) {
console.log("received message");
//console.log(`received ${event.data}`);
document.getElementById("svgContainer").innerHTML = event.data;
gvar.eval(document.getElementById('SVGScript').textContent);

};

Expand Down
3 changes: 2 additions & 1 deletion examples/wtg2svglive/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ go 1.19

require (
github.com/fsnotify/fsnotify v1.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/owulveryck/wardleyToGo v0.0.0
nhooyr.io/websocket v1.8.7
)

replace github.com/owulveryck/wardleyToGo v0.0.0 => ../../

require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/klauspost/compress v1.10.3 // indirect
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 // indirect
golang.org/x/image v0.3.0 // indirect
golang.org/x/sys v0.1.0 // indirect
gonum.org/v1/gonum v0.12.0 // indirect
Expand Down
4 changes: 3 additions & 1 deletion examples/wtg2svglive/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
Expand All @@ -51,7 +54,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 h1:fJwx88sMf5RXwDwziL0/Mn9Wqs+efMSo/RYcL+37W9c=
golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down
35 changes: 20 additions & 15 deletions examples/wtg2svglive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"context"
"embed"
"fmt"
"image"
Expand All @@ -13,31 +12,40 @@ import (
"os/exec"
"path/filepath"
"runtime"
"time"

"github.com/fsnotify/fsnotify"
"github.com/kelseyhightower/envconfig"
svgmap "github.com/owulveryck/wardleyToGo/encoding/svg"
"github.com/owulveryck/wardleyToGo/parser/wtg"
"nhooyr.io/websocket"
)

type configuration struct {
Width int `default:"1500"`
Height int `default:"900"`
Port string `default:"8080"`
}

var config configuration

//go:embed assets/*
var assets embed.FS

var fullPath string

func main() {
port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
err := envconfig.Process("WTGLIVE", &config)
if err != nil {
envconfig.Usage("WTGLIVE", &config)
log.Fatal(err.Error())
}

if len(os.Args) != 2 {
envconfig.Usage("WTGLIVE", &config)
log.Fatalf("usage: %v [wtg file to watch]", os.Args[0])
}
fileToWatch := os.Args[1]
// Get the directory
var err error
fullPath, err = filepath.Abs(fileToWatch)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -101,9 +109,9 @@ func main() {
assetsFs := http.FileServer(http.FS(myFs))

mux.Handle("/", http.StripPrefix("/", assetsFs))
log.Println("listening on " + port + ". Use the PORT env var to change it")
openbrowser("http://localhost:8080")
err = http.ListenAndServe(":"+port, mux)
log.Println("listening on " + config.Port + ". Use the PORT env var to change it")
openbrowser("http://localhost:" + config.Port)
err = http.ListenAndServe(":"+config.Port, mux)
log.Fatal(err)
}

Expand All @@ -121,10 +129,7 @@ func (ws *wsWriter) handler(w http.ResponseWriter, r *http.Request) {
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")

ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)
defer cancel()

ctx = c.CloseRead(ctx)
ctx := c.CloseRead(r.Context())

for {
select {
Expand Down Expand Up @@ -168,8 +173,8 @@ func generateSVG(filePath string) ([]byte, error) {
imgArea := (p.ImageSize.Max.X - p.ImageSize.Min.X) * (p.ImageSize.Max.X - p.ImageSize.Min.Y)
canvasArea := (p.MapSize.Max.X - p.MapSize.Min.X) * (p.MapSize.Max.X - p.MapSize.Min.Y)
if imgArea == 0 || canvasArea == 0 {
p.ImageSize = image.Rect(0, 0, 1100, 900)
p.MapSize = image.Rect(30, 50, 1070, 850)
p.ImageSize = image.Rect(0, 0, config.Width, config.Height)
p.MapSize = image.Rect(30, 50, config.Width-30, config.Height-50)
}
var output bytes.Buffer
e, err := svgmap.NewEncoder(&output, p.ImageSize, p.MapSize)
Expand Down
29 changes: 26 additions & 3 deletions examples/wtg2svglive/sample.wtg
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
a
title: sample

a -- b
a:c
business - cup of tea
public - cup of tea
cup of tea - cup
cup of tea --- hot water
cup of tea -- tea

hot water - kettle
hot water - water


public: {

}
business: {
evolution: |..|..|.x.|..|
color: Green
}

cup of tea: |..|..|.x.|..|
bla: {
evolution: |..|..|.x.|..|
// label: N
color: Green
}
bla: |..|x|..|..|

0 comments on commit 8ca07fb

Please sign in to comment.