Skip to content

Commit 7ead68b

Browse files
committed
minor refactor and disclaimer for prettify
1 parent 92cbb1c commit 7ead68b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

cmd/sql_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,31 @@ func normalize(csvData []byte) []byte {
109109

110110
// prettify justifies columns (right-justifies numbers)
111111
// in a way which [normalize] can still parse.
112+
//
113+
// Only fit for simple, toy CSV data; does not properly
114+
// encode chars that require quoting.
112115
func prettify(csvData []byte) []byte {
113116
csvData = normalize(csvData)
114117

115118
reader := csv.NewReader(bytes.NewReader(csvData))
116119
records := must(reader.ReadAll())
117120

118121
// get max width (field length) of each column
119-
widths := make([]int, len(records[0]))
122+
nFields := len(records[0])
123+
widths := make([]int, nFields)
120124
for _, record := range records {
121125
for i, field := range record {
122126
widths[i] = max(len(field), widths[i])
123127
}
124128
}
125129

126-
const (
127-
sep = ", "
128-
)
130+
// write CSV-like output
131+
const sep = ", "
129132
var (
130133
buf = &bytes.Buffer{}
131134
write = func(s string) { must(buf.WriteString(s)) }
132135

133-
lastField = len(records[0]) - 1
136+
lastField = nFields - 1
134137
)
135138
for _, record := range records {
136139
for i, field := range record {

0 commit comments

Comments
 (0)