Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Last active March 24, 2016 04:47
Show Gist options
  • Save ajstarks/808c6533235fd84b869b to your computer and use it in GitHub Desktop.
Save ajstarks/808c6533235fd84b869b to your computer and use it in GitHub Desktop.
// structlayout-svg: generate SVG struct layouts
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/ajstarks/svgo"
"honnef.co/go/structlayout"
)
var title string
func init() {
flag.StringVar(&title, "t", "Structure Layout", "title")
}
func main() {
log.SetFlags(0)
flag.Parse()
// Decode the JSON
var fields []structlayout.Field
if err := json.NewDecoder(os.Stdin).Decode(&fields); err != nil {
log.Fatal(err)
}
if len(fields) == 0 {
return
}
// layout variables
top := 50
structheight := 50
structwidth := 100
gutter := 8
byteheight := 10
fontsize := byteheight + byteheight/2
width := 600
// Determine the height of the canvas
height := top
for _, f := range fields {
height += byteheight * int(f.Size)
height += gutter
}
x := width / 10
y := top
// Set up the canvas
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Rect(0, 0, width, height, "fill:rgb(240,240,240)")
canvas.Gstyle(fmt.Sprintf("font-size:%dpx;font-family:sans-serif", fontsize))
canvas.Text(x+structwidth/2, top/2, title, "text-anchor:middle;font-size:120%")
// For every field, draw a labled box
pos := int64(0)
for _, f := range fields {
name := f.Name + " " + f.Type
if f.IsPadding {
name = "padding"
}
structheight = byteheight * int(f.Size)
canvas.Rect(x, y, structwidth, structheight, "fill:steelblue")
canvas.Text(x+structwidth+10, y+fontsize, fmt.Sprintf("%d", pos))
canvas.Text(x+structwidth+fontsize*4, y+fontsize, fmt.Sprintf("%s (size %d, align %d)", name, f.Size, f.Align))
if f.Size > 2 {
canvas.Text(x-10, y+structheight, fmt.Sprintf("%d", pos+f.Size-1), "text-anchor:end")
}
pos += f.Size
y += structheight + gutter
}
canvas.Gend()
canvas.End()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment