-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
output_tablewriter.go
64 lines (56 loc) · 1.42 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package trdsql
import (
"strings"
"github.com/olekukonko/tablewriter"
)
// TWWriter provides methods of the Writer interface.
type TWWriter struct {
writeOpts *WriteOpts
writer *tablewriter.Table
outNULL string
results []string
needNULL bool
markdown bool
}
// NewTWWriter returns TWWriter.
func NewTWWriter(writeOpts *WriteOpts, markdown bool) *TWWriter {
w := &TWWriter{}
w.writeOpts = writeOpts
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
w.markdown = markdown
return w
}
// PreWrite is preparation.
func (w *TWWriter) PreWrite(columns []string, types []string) error {
w.writer = tablewriter.NewWriter(w.writeOpts.OutStream)
w.writer.SetAutoFormatHeaders(false)
w.writer.SetAutoWrapText(!w.writeOpts.OutNoWrap)
if w.markdown {
w.writer.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
w.writer.SetCenterSeparator("|")
}
w.writer.SetHeader(columns)
w.results = make([]string, len(columns))
return nil
}
// WriteRow is Addition to array.
func (w *TWWriter) WriteRow(values []any, columns []string) error {
for i, col := range values {
str := ValString(col)
if w.markdown {
str = strings.ReplaceAll(str, `|`, `\|`)
}
if col == nil && w.needNULL {
str = w.outNULL
}
w.results[i] = str
}
w.writer.Append(w.results)
return nil
}
// PostWrite is actual output.
func (w *TWWriter) PostWrite() error {
w.writer.Render()
return nil
}