-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
129 lines (118 loc) · 2.77 KB
/
renderer.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
package tdat
import (
"bytes"
"fmt"
"io"
"os"
)
// RenderToString is like RenderToWriter but renders to a string.
func RenderToString(model *Model, colWidth int) (string, error) {
buffer := &bytes.Buffer{}
err := RenderToWriter(model, colWidth, buffer)
if err != nil {
return "", err
}
txt := string(buffer.Bytes())
return txt, nil
}
// RenderToFile is like RenderToWriter but renders into a file.
// If the file exists, it is overwritten.
func RenderToFile(model *Model, colWidth int, filename string) error {
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
return RenderToWriter(model, colWidth, file)
}
// RenderToWriter renders a model to a io.Writer.
// The renderer pads columns with spaces, so that each column
// has at least colWidth characters.
// If colWidth <= 0, no padding is applied.
func RenderToWriter(model *Model, colWidth int, w io.Writer) error {
r := renderer{w: w, colWidth: colWidth}
r.renderModel(model)
return r.err
}
// ------------------------------------------------------------
type renderer struct {
w io.Writer
colWidth int
err error
}
func (r renderer) renderModel(model *Model) {
for _, table := range model.Tables {
r.renderTable(table)
}
}
func (r renderer) renderTable(table *Table) {
if r.err != nil {
return
}
// table name
r.printf("%s\n", table.Name)
// columns
r.renderColumns(table.Columns)
// rows
for _, row := range table.Rows {
if r.err != nil {
return
}
r.renderRow(row)
}
r.printf("\n")
}
func (r renderer) renderColumns(columns []*Column) {
colCount := len(columns)
for colIndex, col := range columns {
cell := fmt.Sprintf("%s:%c", col.Name, col.Type)
if r.colWidth <= 0 || colIndex >= colCount-1 {
r.printf("|%s", cell)
} else {
r.printf("|%-*s", r.colWidth, cell)
}
}
if colCount > 0 {
r.printf("\n")
}
}
func (r renderer) renderRow(row *Row) {
valCount := len(row.Values)
for valIndex, val := range row.Values {
cell := ""
if !val.Null {
switch val.Type {
case IntValue:
cell = fmt.Sprintf("%d", val.AsInt)
case FloatValue:
cell = fmt.Sprintf("%f", val.AsFloat)
case BoolValue:
cell = fmt.Sprintf("%t", val.AsBool)
case StringValue:
cell = fmt.Sprintf("%q", val.AsString)
case TimeValue:
cell = val.AsTime.UTC().Format("2006-01-02T15:04:05.999")
default:
panic("wrong value type")
}
}
if r.colWidth <= 0 || valIndex >= valCount-1 {
r.printf("|%s", cell)
} else {
r.printf("|%-*s", r.colWidth, cell)
}
}
if valCount > 0 {
r.printf("\n")
}
}
func (r renderer) printf(format string, args ...interface{}) {
if r.err != nil {
return
}
s := fmt.Sprintf(format, args...)
_, err := r.w.Write([]byte(s))
if err != nil {
r.err = err
}
}