-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.go
87 lines (72 loc) · 1.84 KB
/
print.go
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
package goobj
import (
"fmt"
"io"
"os"
"strings"
)
var symbolHeaderRows = []string{"Offset", "Size", "Type", "DupOK", "Local", "MakeTypeLink", "Name", "Version", "GoType"}
// PrintSymbols prints the symbols in the table format.
func PrintSymbols(file *File) {
fmt.Println("The list of defined symbols:")
table := newTable(symbolHeaderRows)
for _, symbol := range file.Symbols {
ref := file.SymbolReferences[symbol.IDIndex]
goType := file.SymbolReferences[symbol.GoTypeIndex]
row := []string{
fmt.Sprintf("%#x", file.DataBlockPosition+symbol.DataAddr.Offset),
fmt.Sprintf("%#x", symbol.Size),
fmt.Sprintf("%s", symbol.Kind),
fmt.Sprintf("%v", symbol.DupOK),
fmt.Sprintf("%v", symbol.Local),
fmt.Sprintf("%v", symbol.Typelink),
fmt.Sprintf("%s", ref.Name),
fmt.Sprintf("%d", ref.Version),
fmt.Sprintf("%s", goType.Name),
}
table.addRow(row)
}
table.print()
}
type table struct {
headers []string
rows [][]string
}
func newTable(headers []string) *table {
return &table{headers: headers}
}
func (t *table) addRow(values []string) {
t.rows = append(t.rows, values)
}
func (t *table) print() {
t.writeTo(os.Stdout)
}
func (t *table) writeTo(w io.Writer) {
maxWidths := t.calcMaxWidths()
t.writeRowTo(w, t.headers, maxWidths)
for _, row := range t.rows {
t.writeRowTo(w, row, maxWidths)
}
}
func (t *table) calcMaxWidths() []int {
maxWidths := make([]int, len(t.headers))
for i, header := range t.headers {
maxWidths[i] = len(header)
}
for _, row := range t.rows {
for j, val := range row {
if maxWidths[j] < len(val) {
maxWidths[j] = len(val)
}
}
}
return maxWidths
}
func (t *table) writeRowTo(w io.Writer, row []string, maxWidths []int) {
fmt.Fprint(w, " ")
for i, val := range row {
fmt.Fprint(w, val)
fmt.Fprint(w, strings.Repeat(" ", maxWidths[i]-len(val)+1))
}
fmt.Fprint(w, "\n")
}