-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_tablewriter.go
46 lines (39 loc) · 1.02 KB
/
output_tablewriter.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
package trdsql
import (
"github.com/olekukonko/tablewriter"
)
// TWWriter is tablewriter struct
type TWWriter struct {
writer *tablewriter.Table
results []string
}
// NewTWWriter returns TWWriter.
func NewTWWriter(writeOpts *WriteOpts, markdown bool) *TWWriter {
w := &TWWriter{}
w.writer = tablewriter.NewWriter(writeOpts.OutStream)
w.writer.SetAutoFormatHeaders(false)
if markdown {
w.writer.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
w.writer.SetCenterSeparator("|")
}
return w
}
// PreWrite is preparation.
func (w *TWWriter) PreWrite(columns []string, types []string) error {
w.writer.SetHeader(columns)
w.results = make([]string, len(columns))
return nil
}
// WriteRow is Addition to array.
func (w *TWWriter) WriteRow(values []interface{}, columns []string) error {
for i, col := range values {
w.results[i] = ValString(col)
}
w.writer.Append(w.results)
return nil
}
// PostWrite is Actual output.
func (w *TWWriter) PostWrite() error {
w.writer.Render()
return nil
}