From 52ffd09b10a55c733c473aade4f07874044e9d74 Mon Sep 17 00:00:00 2001 From: Olivier Wulveryck Date: Tue, 14 Mar 2023 15:04:24 +0100 Subject: [PATCH] feat: add width and height as env variables --- examples/wtg2svglive/assets/websocket.js | 2 ++ examples/wtg2svglive/go.mod | 3 +- examples/wtg2svglive/go.sum | 4 ++- examples/wtg2svglive/main.go | 35 ++++++++++++++---------- examples/wtg2svglive/sample.wtg | 29 ++++++++++++++++++-- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/examples/wtg2svglive/assets/websocket.js b/examples/wtg2svglive/assets/websocket.js index f85bfd7..96cd9a6 100644 --- a/examples/wtg2svglive/assets/websocket.js +++ b/examples/wtg2svglive/assets/websocket.js @@ -1,3 +1,4 @@ +var gvar = this; let socket = new WebSocket("ws://localhost:8080/ws"); socket.onopen = function(e) { @@ -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); }; diff --git a/examples/wtg2svglive/go.mod b/examples/wtg2svglive/go.mod index 21147c2..5ba7f57 100644 --- a/examples/wtg2svglive/go.mod +++ b/examples/wtg2svglive/go.mod @@ -4,6 +4,7 @@ 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 ) @@ -11,8 +12,8 @@ require ( 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 diff --git a/examples/wtg2svglive/go.sum b/examples/wtg2svglive/go.sum index 4649f1b..fe4505e 100644 --- a/examples/wtg2svglive/go.sum +++ b/examples/wtg2svglive/go.sum @@ -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= @@ -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= diff --git a/examples/wtg2svglive/main.go b/examples/wtg2svglive/main.go index 5ab595b..7cd68f1 100644 --- a/examples/wtg2svglive/main.go +++ b/examples/wtg2svglive/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "context" "embed" "fmt" "image" @@ -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) @@ -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) } @@ -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 { @@ -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) diff --git a/examples/wtg2svglive/sample.wtg b/examples/wtg2svglive/sample.wtg index 3acfe07..1549559 100644 --- a/examples/wtg2svglive/sample.wtg +++ b/examples/wtg2svglive/sample.wtg @@ -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|..|..|