forked from fanzhidongyzby/TableFormat
/
table.go
696 lines (579 loc) · 13.7 KB
/
table.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package table
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
//option config parameters
var (
//separate rows
RowSeparator string = "\n"
//separate columns, empty string means all the space characters
ColumnSeparator string = ""
//what means a empty table field
Placeholder string = "_"
//what to be filled in blank table field when row's too short
BlankFilling string = ""
//what to be filled in blank header field when row's too long
BlankFillingForHeader string = ""
//discard more columns or not when row's too long
ColOverflow bool = true
//use utf8 character to print board
UseBoard bool = true
//what to replace \n \b \t ...
SpaceAlt byte = ' '
//what to separator overflow columns
OverFlowSeparator string = " "
//what to fill into field in order to centralize
CenterFilling byte = ' '
//whether ignore empty header when all header fields are placeholder
IgnoreEmptyHeader bool = true
)
//reset all the configs to default, if change the config, go defer it makes good
func Reset() {
RowSeparator = "\n"
ColumnSeparator = ""
Placeholder = "_"
BlankFilling = ""
BlankFillingForHeader = ""
ColOverflow = true
UseBoard = true
SpaceAlt = ' '
OverFlowSeparator = " "
CenterFilling = ' '
IgnoreEmptyHeader = true
}
/*
Convert Interface for user
Description: Any type which implements this interface
has the ability of converting to string. Especially
to struct type, which can convert its field to
string independently. For example:
type Rect struct {
Length int `table:"a, meter"`
Width int `table:"b, meter"`
}
func (this Rect) Convert(field interface{}, typeStr string) (str string) {
switch typeStr {
case "meter":
if v, ok := field.(int); ok {
str = fmt.Sprintf("%dm", v)
}
}
return str
}
The common style of table tag defined in the struct is:
`table : [name] [,type] [,nolist]`
1. 'name' renames the field in the table, if 'name' is '-', it means the field is totoally ignored
2. 'type' allows user define the convertion behavior of the field
3. 'nolist' defines whether the field appears when format object list or map
Parameters:
field: Represents any field's value in struct
typeStr: Type defined in struct's table tag, represents
different type convertion.
*/
type Convertable interface {
Convert(field interface{}, typeStr string) string
}
//raw string type, do not tokenize string's content
type RawString string
//the format API
func Format(obj interface{}) string {
return format(encode(obj))
}
//quick print
func Print(obj interface{}) {
fmt.Print(Format(obj))
}
//encode object, ignore panics
func encode(obj interface{}) (str string) {
//ignore all the panic
defer func() {
if r := recover(); r != nil {
str = createEmptyHeader(1) + createRow(fmt.Sprint(r))
}
}()
v := reflect.ValueOf(obj)
return encodeAny(v)
}
//encode any type
func encodeAny(v reflect.Value) (str string) {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
str = encodeAny(v.Elem())
case reflect.String:
str = encodeString(v)
case reflect.Array, reflect.Slice:
str = encodeList(v)
case reflect.Struct:
str = encodeStruct(v)
case reflect.Map:
str = encodeMap(v)
case reflect.Func:
str = encodeFunc(v)
default:
_, str = encodePlain(v)
}
return str
}
//raw string
func encodeRawString(v reflect.Value) (str string) {
var buf bytes.Buffer
obj := v.Interface()
if o, ok := obj.(RawString); ok {
buf.WriteString(createEmptyHeader(1))
buf.WriteString(createRow(string(o)))
}
return buf.String()
}
//string type, classic format type
func encodeString(v reflect.Value) (str string) {
var buf bytes.Buffer
if v.Kind() != reflect.String {
return buf.String()
}
obj := v.Interface()
//raw string
if _, ok := obj.(RawString); ok {
return encodeRawString(v)
}
//normal string
if o, ok := obj.(string); ok {
buf.WriteString(createRow(o))
}
return buf.String()
}
//function type, get the function name
func encodePlainFunc(v reflect.Value) (str string) {
var buf bytes.Buffer
if v.Kind() != reflect.Func {
return buf.String()
}
buf.WriteString(createRow(runtime.FuncForPC(v.Pointer()).Name()))
return buf.String()
}
//function type, get the function name
func encodeFunc(v reflect.Value) (str string) {
var buf bytes.Buffer
if v.Kind() != reflect.Func {
return buf.String()
}
buf.WriteString(createEmptyHeader(1))
buf.WriteString(encodePlainFunc(v))
return buf.String()
}
//base types
func encodePlain(v reflect.Value) (key, str string) {
key = Placeholder
switch v.Kind() {
case reflect.Invalid:
case reflect.Ptr, reflect.Interface:
key, str = encodePlain(v.Elem())
case reflect.Struct:
key, str = encodePlainStruct(v)
case reflect.Func:
str = encodePlainFunc(v)
default:
str = fmt.Sprint(v.Interface())
}
return key, str
}
//map type
func encodeMap(v reflect.Value) (str string) {
var buf bytes.Buffer
if v.Kind() != reflect.Map {
return buf.String()
}
keys := v.MapKeys()
for i, key := range keys {
value := v.MapIndex(key)
k1, v1 := encodePlain(key)
k2, v2 := encodePlain(value)
if i == 0 {
buf.WriteString(createRow(k1, k2))
}
buf.WriteString(createRow(v1, v2))
}
return buf.String()
}
//array, slice type
func encodeList(v reflect.Value) (str string) {
var buf bytes.Buffer
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
return buf.String()
}
//format list
for i := 0; i < v.Len(); i++ {
key, val := encodePlain(v.Index(i))
if i == 0 {
buf.WriteString(createRow(Placeholder, key))
}
buf.WriteString(createRow(strconv.Itoa(i+1), val))
}
return buf.String()
}
//return key string and value string
func encodePlainStruct(v reflect.Value) (string, string) {
_, _, keys, vals := processStruct(v)
if len(keys) == 0 {
keys = []string{Placeholder}
vals = []string{fmt.Sprint(v.Interface())}
}
return createRow(keys...), createRow(vals...)
}
//struct type
func encodeStruct(v reflect.Value) (str string) {
var buf bytes.Buffer
keys, vals, _, _ := processStruct(v)
if len(keys) == 0 {
return fmt.Sprint(v.Interface())
}
buf.WriteString(createEmptyHeader(2))
for i := 0; i < len(keys); i++ {
buf.WriteString(createRow(keys[i], vals[i]))
}
return buf.String()
}
//process struct, return objfmt fields and listfmt fields
func processStruct(v reflect.Value) (detKeys, detVals, absKeys, absVals []string) {
detKeys = []string{}
detVals = []string{}
absKeys = []string{}
absVals = []string{}
obj := v.Interface()
if v.Kind() != reflect.Struct {
return detKeys, detVals, absKeys, absVals
}
//struct fields
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
//get field name and value
name := field.Name
value := v.FieldByName(field.Name)
val := value.Interface()
tag := field.Tag.Get("table")
nameTag, typeTag, listTag := parseTag(tag)
//name tag
if nameTag == "-" {
continue
} else if nameTag != "" {
name = nameTag
}
//type tag
if o, ok := obj.(Convertable); ok && typeTag != "" {
val = o.Convert(val, typeTag)
}
//list tag
valStr := fmt.Sprintf("%v", val)
detKeys = append(detKeys, name)
detVals = append(detVals, valStr)
if listTag != "nolist" {
absKeys = append(absKeys, name)
absVals = append(absVals, valStr)
}
}
return detKeys, detVals, absKeys, absVals
}
//parse tag, process tag: `table:"-|<newName>[,<newType>][,<nolist>]"`
func parseTag(tag string) (nameTag, typeTag, listTag string) {
//tokenize
values := strings.Split(tag, ",")
num := len(values)
if num > 0 {
nameTag = values[0]
}
if num > 1 {
typeTag = values[1]
}
if num > 2 {
listTag = values[2]
}
return nameTag, typeTag, listTag
}
//merge placehold woth col sep
func createEmptyHeader(colNum int) string {
fields := make([]string, colNum)
for i, _ := range fields {
fields[i] = Placeholder
}
return createRow(fields...)
}
//merge fields with col sep
func createRow(fields ...string) string {
sep := " "
if ColumnSeparator != "" {
sep = ColumnSeparator
}
var buf bytes.Buffer
for i, field := range fields {
field = strings.TrimSuffix(field, RowSeparator)
if i != 0 {
buf.WriteString(sep)
}
buf.WriteString(field)
}
buf.WriteString(RowSeparator)
return buf.String()
}
//table format
func format(data string) string {
//convert string to table
tb := preProcess(data)
//print table
if UseBoard {
return boardFormat(tb)
} else {
return simpleFormat(tb)
}
}
//utf8 table characters
const (
hrLine = "─"
vtLine = "│"
topLeft = "┌"
topCenter = "┬"
topRight = "┐"
middleLeft = "├"
middleCenter = "┼"
middleRight = "┤"
bottomLeft = "└"
bottomCenter = "┴"
bottomRight = "┘"
)
//format with board
func boardFormat(tb [][]string) string {
if len(tb) == 0 {
tb = [][]string{{string(CenterFilling) + BlankFillingForHeader + string(CenterFilling)}}
}
//table attributes
rowNum := len(tb)*2 + 1
colNum := len(tb[0])*2 + 1
colWidth := make([]int, colNum)
for i, _ := range tb[0] {
colWidth[i] = width(tb[0][i])
}
//init fill as --- ...
fill := make([]string, colNum/2)
for i, _ := range fill {
fill[i] = strings.Repeat(hrLine, colWidth[i])
}
//init top ┌───┬───┐
topLine := initLine(topLeft, topCenter, topRight, fill)
//init middle ├───┼───┤
middleLine := initLine(middleLeft, middleCenter, middleRight, fill)
//init bottom └───┴───┘
bottomLine := initLine(bottomLeft, bottomCenter, bottomRight, fill)
//create board table
table := make([][]string, rowNum)
for i, _ := range table {
switch {
case i == 0:
table[i] = topLine
case i == rowNum-1:
table[i] = bottomLine
case i%2 == 0:
table[i] = middleLine
default:
table[i] = initLine(vtLine, vtLine, vtLine, tb[i/2])
}
}
//output table
var buf bytes.Buffer
for _, line := range table {
for _, val := range line {
buf.WriteString(val)
}
buf.WriteString("\n")
}
return buf.String()
}
//format without board
func simpleFormat(tb [][]string) string {
if len(tb) == 0 {
tb = [][]string{{string(CenterFilling) + BlankFillingForHeader + string(CenterFilling)}}
}
//out put table
var buf bytes.Buffer
for _, line := range tb {
for _, val := range line {
buf.WriteString(val)
}
buf.WriteString("\n")
}
return buf.String()
}
//split str and filt empty line
func getLines(str string) []string {
var lines []string
if RowSeparator == "" {
lines = strings.Fields(str)
} else {
lines = strings.Split(str, RowSeparator)
}
//filt empty string
ret := []string{}
for _, f := range lines {
if len(f) > 0 {
ret = append(ret, f)
}
}
return ret
}
//split line and filt empty elements
func getFields(line string) []string {
var fields []string
if ColumnSeparator == "" {
fields = strings.Fields(line)
} else {
fields = strings.Split(line, ColumnSeparator)
}
//filt empty string
ret := []string{}
for _, f := range fields {
if len(f) > 0 {
ret = append(ret, f)
}
}
return ret
}
//change all the space character (\t \n _ \b) to space
func handleSpace(str string) string {
arr := make([]rune, utf8.RuneCountInString(str))
index := 0
for _, c := range str {
if unicode.IsSpace(c) && c != ' ' {
c = rune(SpaceAlt)
}
arr[index] = c
index++
}
return string(arr)
}
//how long is string in screen, Chinese chararter is 2 length
func width(str string) int {
sum := 0
for _, c := range str {
if utf8.RuneLen(c) > 1 {
sum += 2
} else {
sum++
}
}
return sum
}
//convert string to 2-D slice
func preProcess(data string) [][]string {
//get non-blank lines
lines := []string{}
//for _, line := range strings.Split(data, RowSeparator) {
for _, line := range getLines(data) {
if len(getFields(line)) != 0 {
lines = append(lines, line)
}
}
rowNum := len(lines)
//handle empty table
if rowNum == 0 {
//use place holder to represent a empty table
return [][]string{{string(CenterFilling) + BlankFillingForHeader + string(CenterFilling)}}
}
//get columns
colNum := len(getFields(lines[0]))
//max width of each column
colWidth := make([]int, colNum)
//process empty header
if IgnoreEmptyHeader {
header := getFields(lines[0])
ignore := true
for _, val := range header {
if val != Placeholder {
ignore = false
break
}
}
if ignore {
lines = lines[1:]
rowNum--
}
}
tb := make([][]string, rowNum)
for row, line := range lines {
tb[row] = make([]string, colNum)
//fillings
filling := BlankFilling
if row == 0 {
filling = BlankFillingForHeader
}
//init row as blank filling
for index, _ := range tb[row] {
tb[row][index] = filling
}
//get fields
fields := getFields(line)
for col, val := range fields {
//handle placeholder
if val == Placeholder {
val = filling
}
//handle column overflow
if col >= colNum {
if ColOverflow {
col = colNum - 1
val = tb[row][col] + OverFlowSeparator + val
} else {
//discard more cols
break
}
}
tb[row][col] = handleSpace(val)
}
}
//calcu max width, extend colwidth + 2 to store blank
for col := 0; col < colNum; col++ {
for row := 0; row < rowNum; row++ {
val := tb[row][col]
size := width(val)
if size > colWidth[col] {
colWidth[col] = size
}
}
colWidth[col] += 2
}
//middle value with blank
cfill := string(CenterFilling)
for row, line := range tb {
for col, val := range line {
size := width(val)
left := (colWidth[col] - size) / 2
right := colWidth[col] - size - left
tb[row][col] = strings.Repeat(cfill, left) + val + strings.Repeat(cfill, right)
}
}
return tb
}
//form table line
func initLine(left, center, right string, fill []string) []string {
colNum := len(fill)*2 + 1
line := make([]string, colNum)
for i, _ := range line {
tmp := ""
switch {
case i == 0:
tmp = left
case i == colNum-1:
tmp = right
case i%2 == 0:
tmp = center
default:
tmp = fill[i/2]
}
line[i] = tmp
}
return line
}