-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
216 lines (188 loc) · 5.06 KB
/
render.go
File metadata and controls
216 lines (188 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"strings"
)
// ANSI escape sequences.
const (
ansiReset = "\033[0m"
ansiBold = "\033[1m"
ansiFgWhite = "\033[97m"
ansiFgBlack = "\033[30m"
ansiBgRed = "\033[41m" // internal (avoidable) padding
ansiBgGray = "\033[100m" // trailing (unavoidable) padding
)
// Cycling background colors for fields.
var fieldBG = []string{
"\033[44m", // blue
"\033[42m", // green
"\033[43m", // yellow
"\033[46m", // cyan
"\033[45m", // magenta
}
// byteKind categorises each byte of the struct's memory.
type byteKind int
const (
bkField byteKind = iota
bkInternalPad
bkTrailingPad
)
type byteCell struct {
kind byteKind
fieldIdx int
label string // exactly cellW runes
}
const cellW = 5 // visible characters per cell
func buildByteMap(layout Layout) []byteCell {
cells := make([]byteCell, layout.StructSize)
for fi, field := range layout.Fields {
for b := 0; b < field.Size; b++ {
label := strings.Repeat(" ", cellW)
if b == 0 {
name := field.Name
if len(name) > cellW {
name = name[:cellW-1] + "…"
}
label = fmt.Sprintf("%-*s", cellW, name)
}
cells[field.Offset+b] = byteCell{
kind: bkField,
fieldIdx: fi,
label: label,
}
}
for p := 0; p < field.PadAfter; p++ {
cells[field.Offset+field.Size+p] = byteCell{
kind: bkInternalPad,
label: strings.Repeat("▒", cellW),
}
}
}
for i := layout.StructSize - layout.TrailingPad; i < layout.StructSize; i++ {
cells[i] = byteCell{
kind: bkTrailingPad,
label: strings.Repeat("░", cellW),
}
}
return cells
}
func coloredCell(c byteCell) string {
var color string
switch c.kind {
case bkInternalPad:
color = ansiBgRed + ansiFgWhite
case bkTrailingPad:
color = ansiBgGray + ansiFgBlack
default:
color = fieldBG[c.fieldIdx%len(fieldBG)] + ansiFgWhite
}
return color + c.label + ansiReset
}
// renderGrid produces the colored byte-grid for a layout.
func renderGrid(layout Layout) string {
if layout.StructSize == 0 {
return " (empty struct)\n"
}
cells := buildByteMap(layout)
rowWidth := max(layout.StructAlign, 1)
sep := strings.Repeat("─", cellW)
// Build the top, mid, and bottom border for rowWidth columns.
borderLine := func(left, cross, right string) string {
parts := make([]string, rowWidth)
for i := range parts {
parts[i] = sep
}
return left + strings.Join(parts, cross) + right
}
top := borderLine("┌", "┬", "┐")
mid := borderLine("├", "┼", "┤")
bot := borderLine("└", "┴", "┘")
// Column offset header.
var header strings.Builder
header.WriteString(" ") // indent matching row-label width
for col := 0; col < rowWidth; col++ {
label := fmt.Sprintf("%-*d", cellW, col)
header.WriteString(label)
if col < rowWidth-1 {
header.WriteString(" ")
}
}
header.WriteString("\n")
var sb strings.Builder
sb.WriteString(header.String())
rows := (layout.StructSize + rowWidth - 1) / rowWidth
for row := 0; row < rows; row++ {
startByte := row * rowWidth
if row == 0 {
sb.WriteString(" " + top + "\n")
} else {
sb.WriteString(" " + mid + "\n")
}
// Row label (absolute byte offset).
fmt.Fprintf(&sb, " %4d: │", startByte)
for col := 0; col < rowWidth; col++ {
idx := startByte + col
if idx < len(cells) {
sb.WriteString(coloredCell(cells[idx]))
} else {
sb.WriteString(strings.Repeat(" ", cellW))
}
sb.WriteString("│")
}
sb.WriteString("\n")
}
sb.WriteString(" " + bot + "\n")
return sb.String()
}
// renderLegend prints the color legend for field names in the layout.
func renderLegend(layout Layout) string {
seen := make(map[string]bool)
var sb strings.Builder
sb.WriteString(" Fields: ")
first := true
for _, f := range layout.Fields {
if seen[f.Name] {
continue
}
seen[f.Name] = true
idx := indexOf(layout.Fields, f.Name)
color := fieldBG[idx%len(fieldBG)] + ansiFgWhite
if !first {
sb.WriteString(" ")
}
label := f.Name
if len(label) > cellW {
label = label[:cellW-1] + "…"
}
sb.WriteString(color + fmt.Sprintf(" %-*s ", cellW, label) + ansiReset)
first = false
}
sb.WriteString(" ")
sb.WriteString(ansiBgRed + ansiFgWhite + " ▒▒▒ " + ansiReset + " internal pad (avoidable)")
sb.WriteString(" ")
sb.WriteString(ansiBgGray + ansiFgBlack + " ░░░ " + ansiReset + " trailing pad (unavoidable)")
sb.WriteString("\n")
return sb.String()
}
func indexOf(fields []LayoutField, name string) int {
for i, f := range fields {
if f.Name == name {
return i
}
}
return 0
}
// banner prints a titled section header.
func banner(title string) string {
line := strings.Repeat("─", 60)
return fmt.Sprintf("\n%s%s%s\n%s\n", ansiBold, title, ansiReset, line)
}
// statsLine prints size/alignment/waste info for a layout.
func statsLine(layout Layout) string {
totalWaste := layout.InternalPad + layout.TrailingPad
return fmt.Sprintf(
" size: %d bytes alignment: %d internal padding: %d bytes trailing padding: %d bytes total waste: %d bytes\n",
layout.StructSize, layout.StructAlign,
layout.InternalPad, layout.TrailingPad, totalWaste,
)
}