Skip to content

Commit

Permalink
structlayout-svg: add background, struct and padding colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ajstarks committed Mar 25, 2016
1 parent fc95091 commit 672fe54
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions structlayout-svg/structlayout-svg.go
Expand Up @@ -12,10 +12,13 @@ import (
"honnef.co/go/structlayout"
)

var title string
var title, bgcolor, scolor, pcolor string

func init() {
flag.StringVar(&title, "t", "Structure Layout", "title")
flag.StringVar(&bgcolor, "bgcolor", "rgb(240,240,240)", "background color")
flag.StringVar(&scolor, "scolor", "steelblue", "structure color")
flag.StringVar(&pcolor, "pcolor", "maroon", "padding color")
}

func main() {
Expand All @@ -30,7 +33,7 @@ func main() {
if len(fields) == 0 {
return
}

// layout variables
top := 50
structheight := 50
Expand All @@ -41,37 +44,37 @@ func main() {
width := 600

// Determine the height of the canvas
height := top
height := top + gutter
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.Rect(0, 0, width, height, "fill:"+bgcolor)
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)
var fillcolor string
nudge := (fontsize*2)/3
nudge := (fontsize * 2) / 3
for _, f := range fields {
name := f.Name + " " + f.Type
if f.IsPadding {
name = "padding"
fillcolor = "fill:maroon"
fillcolor = pcolor
} else {
fillcolor = "fill:steelblue"
fillcolor = scolor
}
structheight = byteheight * int(f.Size)
canvas.Rect(x, y, structwidth, structheight, fillcolor)
canvas.Rect(x, y, structwidth, structheight, "fill:"+fillcolor)
canvas.Text(x+structwidth+10, y+nudge, fmt.Sprintf("%d", pos))
canvas.Text(x+structwidth+fontsize*4, y+nudge, fmt.Sprintf("%s (size %d, align %d)", name, f.Size, f.Align),)
canvas.Text(x+structwidth+fontsize*4, y+nudge, 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")
Expand Down

0 comments on commit 672fe54

Please sign in to comment.