-
Notifications
You must be signed in to change notification settings - Fork 13
/
markdownw.go
150 lines (127 loc) · 3.19 KB
/
markdownw.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
// Package markdownw implements writers for Markdown.
package markdownw
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"html"
"io"
"strconv"
"strings"
"sync"
"time"
"github.com/shopspring/decimal"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/stringz"
)
// RecordWriter implements output.RecordWriter.
type RecordWriter struct {
out io.Writer
pr *output.Printing
buf *bytes.Buffer
recMeta record.Meta
mu sync.Mutex
}
var _ output.NewRecordWriterFunc = NewRecordWriter
// NewRecordWriter returns a writer instance.
func NewRecordWriter(out io.Writer, pr *output.Printing) output.RecordWriter {
return &RecordWriter{out: out, pr: pr}
}
// Open implements output.RecordWriter.
func (w *RecordWriter) Open(_ context.Context, recMeta record.Meta) error {
w.recMeta = recMeta
w.buf = &bytes.Buffer{}
// Write the header
for i, field := range recMeta {
w.buf.WriteString("| ")
w.buf.WriteString(field.MungedName() + " ")
if i == len(recMeta)-1 {
w.buf.WriteString("|\n")
}
}
// Write the header separator row
for i := range recMeta {
w.buf.WriteString("| --- ")
if i == len(recMeta)-1 {
w.buf.WriteString("|\n")
}
}
return nil
}
// Flush implements output.RecordWriter.
func (w *RecordWriter) Flush(context.Context) error {
_, err := w.buf.WriteTo(w.out) // resets buf
return err
}
// Close implements output.RecordWriter.
func (w *RecordWriter) Close(ctx context.Context) error {
return w.Flush(ctx)
}
func (w *RecordWriter) writeRecord(rec record.Record) error {
var s string
for i, field := range rec {
w.buf.WriteString("| ")
switch val := field.(type) {
default:
// should never happen
s = escapeMarkdown(fmt.Sprintf("%v", val))
case nil:
// nil is rendered as empty string, which this cell already is
case int64:
s = strconv.FormatInt(val, 10)
case string:
s = escapeMarkdown(val)
case decimal.Decimal:
s = stringz.FormatDecimal(val)
case bool:
s = strconv.FormatBool(val)
case float64:
s = stringz.FormatFloat(val)
case []byte:
s = base64.StdEncoding.EncodeToString(val)
case time.Time:
switch w.recMeta[i].Kind() { //nolint:exhaustive
default:
s = w.pr.FormatDatetime(val)
case kind.Time:
s = w.pr.FormatTime(val)
case kind.Date:
s = w.pr.FormatDate(val)
}
}
w.buf.WriteString(s + " ")
}
w.buf.WriteString("|\n")
return nil
}
// WriteRecords implements output.RecordWriter.
func (w *RecordWriter) WriteRecords(ctx context.Context, recs []record.Record) error {
w.mu.Lock()
defer w.mu.Unlock()
var err error
for _, rec := range recs {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err = w.writeRecord(rec)
if err != nil {
return err
}
}
return nil
}
// escapeMarkdown is quick effort at escaping markdown
// table cell text. It is not at all tested. Replace this
// function with a real library call at the earliest opportunity.
func escapeMarkdown(s string) string {
s = html.EscapeString(s)
s = strings.ReplaceAll(s, "|", "|")
s = strings.ReplaceAll(s, "\r\n", "<br/>")
s = strings.ReplaceAll(s, "\n", "<br/>")
return s
}