-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
266 lines (228 loc) · 5.37 KB
/
printer.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package printer
import (
"fmt"
"github.com/gookit/color"
"io"
"strings"
"github.com/victorgama/tlvp/meta"
"github.com/victorgama/tlvp/tlv"
)
type itemLine struct {
text string
color color.Color
}
type printItem struct {
data []itemLine
title itemLine
parent *printItem
children []*printItem
}
func (item *printItem) addLine(line string, color color.Color) {
item.data = append(item.data, itemLine{line, color})
}
func (item *printItem) setTitle(line string) {
item.title = itemLine{line, color.Normal}
}
func (item *printItem) isLastChild() bool {
if item.parent == nil {
return true
}
return item.parent.children[len(item.parent.children)-1] == item
}
func Print(entries []*tlv.GenericEntry, describe bool) {
var result []*printItem
for _, e := range entries {
func(entry *tlv.GenericEntry) {
processEntry(entry, describe, nil, &result)
}(e)
}
printItems(result, describe)
}
func printItems(entries []*printItem, describe bool) {
for _, e := range entries {
printEntry(e, describe)
}
}
func autoWrap(text string, wrapAt int) []string {
var result []string
var elements = strings.Split(text, " ")
var currentLine []string
for {
if len(elements) == 0 {
if len(currentLine) > 0 {
result = append(result, strings.Join(currentLine, " "))
}
return result
}
// Check whether we can add a new item to the current line
newArr := append(currentLine, elements[0])
if len(strings.Join(newArr, " ")) > wrapAt {
// Too long. Flush current line and start a new one.
result = append(result, strings.Join(currentLine, " "))
currentLine = []string{elements[0]}
} else {
currentLine = newArr
}
elements = elements[1:]
}
}
func printEntry(e *printItem, describe bool) {
margin := printMargin(e, true)
fmt.Printf("%s%s\n", margin, e.title.text)
margin = printMargin(e, false)
marginLen := len(margin)
for _, s := range e.data {
for _, txt := range autoWrap(s.text, 80-marginLen) {
fmt.Printf("%s%s\n", margin, s.color.Sprint(txt))
}
}
if describe {
fmt.Printf("%s\n", margin)
}
printItems(e.children, describe)
}
func printMargin(entry *printItem, isTitle bool) string {
var tree []*printItem
e := entry
for {
if e == nil {
break
}
tree = append(tree, e)
e = e.parent
}
tree = reverseTree(tree)
return printTree(tree, isTitle, "")
}
func printTree(tree []*printItem, isTitle bool, margin string) string {
if len(tree) == 0 {
return margin
}
item := tree[0]
if isTitle && item.parent != nil {
if len(tree) == 1 {
margin += " "
} else if len(tree) > 1 {
margin += " "
}
if item.parent != nil && item.isLastChild() {
if len(tree) == 1 {
margin += "└"
}
} else if item.parent != nil && !item.isLastChild() {
margin += "├"
}
}
if !isTitle {
if len(tree) == 1 {
if item.isLastChild() && item.parent != nil {
margin += " "
}
if !item.isLastChild() || item.parent == nil || !item.parent.isLastChild() || len(item.children) > 0 {
margin += " │ "
}
} else if item.parent != nil {
if item.isLastChild() {
margin += " "
} else {
margin += " "
}
}
}
return printTree(tree[1:], isTitle, margin)
}
func reverseTree(input []*printItem) []*printItem {
if len(input) == 0 {
return input
}
return append(reverseTree(input[1:]), input[0])
}
func processEntry(entry *tlv.GenericEntry, describe bool, parent *printItem, list *[]*printItem) {
item := printItem{
parent: parent,
}
item.setTitle(printTitle(entry, &item))
if describe {
if metadata := entry.AsMeta(); metadata != nil {
if info := metadata.TagInfo; info != nil {
item.addLine(info.Description, color.Gray)
}
}
}
item.addLine(printSize(entry))
if entry.Kind == tlv.KindConstructed {
constructed := entry.AsConstructed()
for {
e, _, err := constructed.Next()
if err == io.EOF {
break
}
processEntry(e, describe, &item, &item.children)
}
} else {
item.addLine(printValue(entry.AsPrimitive().Value))
}
newList := append(*list, &item)
*list = newList
}
func printSize(ent *tlv.GenericEntry) (string, color.Color) {
size := ent.AsMeta().ValueLength
plural := ""
if size > 1 {
plural = "s"
}
return fmt.Sprintf("Size: %02d byte%s", size, plural), color.Normal
}
func printTitle(ent *tlv.GenericEntry, item *printItem) string {
var (
bytes string
title string
)
if ent.Kind == tlv.KindConstructed {
e := ent.AsConstructed()
bytes = printBytes(e.TagBytes, false)
if tagMeta := e.TagInfo; tagMeta != nil {
title = tagMeta.Name
} else {
title = "Unknown"
}
} else {
e := ent.AsPrimitive()
bytes = printBytes(e.TagBytes, false)
if tagMeta := e.TagInfo; tagMeta != nil {
title = tagMeta.Name
} else {
title = "Unknown"
}
}
rail := ""
if item.parent != nil {
rail = "── "
}
return fmt.Sprintf("%s[%s] %s", rail, color.Magenta.Text(bytes), color.Light.Sprint(title))
}
func printBytes(buf []byte, pretty bool) string {
var format string
separator := ""
if pretty {
format = "0x%02X"
separator = " "
} else {
format = "%02X"
}
data := make([]string, len(buf))
for i, b := range buf {
data[i] = fmt.Sprintf(format, b)
}
return strings.Join(data, separator)
}
func printValue(buf []byte) (string, color.Color) {
isPrintable := meta.IsAsciiPrintable(buf)
var result string
if isPrintable {
result = string(buf)
} else {
result = printBytes(buf, true)
}
return result, color.Cyan
}