-
Notifications
You must be signed in to change notification settings - Fork 220
/
record_writer_pprint.go
353 lines (322 loc) · 11 KB
/
record_writer_pprint.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package output
import (
"bufio"
"container/list"
"fmt"
"strings"
"unicode/utf8"
"github.com/johnkerl/miller/pkg/cli"
"github.com/johnkerl/miller/pkg/colorizer"
"github.com/johnkerl/miller/pkg/mlrval"
)
type RecordWriterPPRINT struct {
writerOptions *cli.TWriterOptions
// Input:
records *list.List
// State:
lastJoinedHeader *string
batch *list.List
}
func NewRecordWriterPPRINT(writerOptions *cli.TWriterOptions) (*RecordWriterPPRINT, error) {
return &RecordWriterPPRINT{
writerOptions: writerOptions,
records: list.New(),
lastJoinedHeader: nil,
batch: list.New(),
}, nil
}
// ----------------------------------------------------------------
func (writer *RecordWriterPPRINT) Write(
outrec *mlrval.Mlrmap,
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
) error {
// Group records by have-same-schema or not. Pretty-print each
// homoegeneous sublist, or "batch".
//
// No output until end of a homogeneous batch of records, since we need to
// find out max width down each column.
if outrec != nil { // Not end of record stream
if writer.lastJoinedHeader == nil {
// First output record:
// * New batch
// * No old batch to print
writer.batch.PushBack(outrec)
temp := strings.Join(outrec.GetKeys(), ",")
writer.lastJoinedHeader = &temp
} else {
// May or may not continue the same homogeneous batch
joinedHeader := strings.Join(outrec.GetKeys(), ",")
if *writer.lastJoinedHeader != joinedHeader {
// Print and free old batch
nonEmpty := writer.writeHeterogenousList(
writer.batch,
writer.writerOptions.BarredPprintOutput,
bufferedOutputStream,
outputIsStdout,
)
if nonEmpty {
// Print a newline
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
// Start a new batch
writer.batch = list.New()
writer.batch.PushBack(outrec)
writer.lastJoinedHeader = &joinedHeader
} else {
// Continue the batch
writer.batch.PushBack(outrec)
}
}
} else { // End of record stream
if writer.batch.Front() != nil {
writer.writeHeterogenousList(writer.batch, writer.writerOptions.BarredPprintOutput,
bufferedOutputStream, outputIsStdout)
}
}
return nil
}
// ----------------------------------------------------------------
// Returns false if there was nothing but empty record(s), e.g. 'mlr gap -n 10'.
func (writer *RecordWriterPPRINT) writeHeterogenousList(
records *list.List,
barred bool,
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
) bool {
maxWidths := make(map[string]int)
var maxNR int64 = 0
for e := records.Front(); e != nil; e = e.Next() {
outrec := e.Value.(*mlrval.Mlrmap)
nr := outrec.FieldCount
if maxNR < nr {
maxNR = nr
}
for pe := outrec.Head; pe != nil; pe = pe.Next {
width := utf8.RuneCountInString(pe.Value.String())
if width == 0 {
width = 1 // We'll rewrite "" to "-" below
}
oldMaxWidth := maxWidths[pe.Key]
if width > oldMaxWidth {
maxWidths[pe.Key] = width
}
}
}
if maxNR == 0 {
return false
} else {
// Column name may be longer/shorter than all data values in the column
for key, oldMaxWidth := range maxWidths {
width := utf8.RuneCountInString(key)
if width > oldMaxWidth {
maxWidths[key] = width
}
}
if barred {
writer.writeHeterogenousListBarred(records, maxWidths, bufferedOutputStream, outputIsStdout)
} else {
writer.writeHeterogenousListNonBarred(records, maxWidths, bufferedOutputStream, outputIsStdout)
}
return true
}
}
// ----------------------------------------------------------------
// Example:
//
// a b i x y
// pan pan 1 0.3467901443380824 0.7268028627434533
// eks pan 2 -0.7586799647899636 0.5221511083334797
// wye wye 3 0.20460330576630303 0.33831852551664776
// eks wye 4 -0.38139939387114097 0.13418874328430463
// wye pan 5 0.5732889198020006 0.8636244699032729
func (writer *RecordWriterPPRINT) writeHeterogenousListNonBarred(
records *list.List,
maxWidths map[string]int,
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
) {
onFirst := true
for e := records.Front(); e != nil; e = e.Next() {
outrec := e.Value.(*mlrval.Mlrmap)
// Print header line
if onFirst && !writer.writerOptions.HeaderlessOutput {
for pe := outrec.Head; pe != nil; pe = pe.Next {
if !writer.writerOptions.RightAlignedPPRINTOutput { // left-align
if pe.Next != nil {
// Header line, left-align, not last column
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
writer.writePadding(pe.Key, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(writer.writerOptions.OFS)
} else {
// Header line, left-align, last column
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
} else { // right-align
writer.writePadding(pe.Key, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
if pe.Next != nil {
// Header line, right-align, not last column
bufferedOutputStream.WriteString(writer.writerOptions.OFS)
} else {
// Header line, right-align, last column
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
}
}
onFirst = false
// Print data lines
for pe := outrec.Head; pe != nil; pe = pe.Next {
s := pe.Value.String()
if s == "" {
s = "-"
}
if !writer.writerOptions.RightAlignedPPRINTOutput { // left-align
if pe.Next != nil {
// Data line, left-align, not last column
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(s, outputIsStdout))
writer.writePadding(s, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(writer.writerOptions.OFS)
} else {
// Data line, left-align, last column
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(s, outputIsStdout))
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
} else { // right-align
writer.writePadding(s, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(s, outputIsStdout))
if pe.Next != nil {
// Data line, right-align, not last column
bufferedOutputStream.WriteString(writer.writerOptions.OFS)
} else {
// Data line, right-align, last column
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
}
if writer.writerOptions.FlushOnEveryRecord {
bufferedOutputStream.Flush()
}
}
}
// ----------------------------------------------------------------
// Example:
//
// +-----+-----+----+----------------------+---------------------+
// | a | b | i | x | y |
// +-----+-----+----+----------------------+---------------------+
// | pan | pan | 1 | 0.3467901443380824 | 0.7268028627434533 |
// | eks | pan | 2 | -0.7586799647899636 | 0.5221511083334797 |
// | wye | wye | 3 | 0.20460330576630303 | 0.33831852551664776 |
// | eks | wye | 4 | -0.38139939387114097 | 0.13418874328430463 |
// | wye | pan | 5 | 0.5732889198020006 | 0.8636244699032729 |
// +-----+-----+----+----------------------+---------------------+
func (writer *RecordWriterPPRINT) writeHeterogenousListBarred(
records *list.List,
maxWidths map[string]int,
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
) {
horizontalBars := make(map[string]string)
for key, width := range maxWidths {
horizontalBars[key] = strings.Repeat("-", width)
}
ofs := writer.writerOptions.OFS
horizontalStart := "+-"
horizontalMiddle := "-+-"
horizontalEnd := "-+"
verticalStart := "|" + ofs
verticalMiddle := ofs + "|" + ofs
verticalEnd := ofs + "|"
onFirst := true
for e := records.Front(); e != nil; e = e.Next() {
outrec := e.Value.(*mlrval.Mlrmap)
// Print header line
if onFirst && !writer.writerOptions.HeaderlessOutput {
bufferedOutputStream.WriteString(horizontalStart)
for pe := outrec.Head; pe != nil; pe = pe.Next {
bufferedOutputStream.WriteString(horizontalBars[pe.Key])
if pe.Next != nil {
bufferedOutputStream.WriteString(horizontalMiddle)
} else {
bufferedOutputStream.WriteString(horizontalEnd)
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
bufferedOutputStream.WriteString(verticalStart)
for pe := outrec.Head; pe != nil; pe = pe.Next {
if !writer.writerOptions.RightAlignedPPRINTOutput { // left-align
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
writer.writePadding(pe.Key, maxWidths[pe.Key], bufferedOutputStream)
} else { // right-align
writer.writePadding(pe.Key, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
}
if pe.Next != nil {
bufferedOutputStream.WriteString(verticalMiddle)
} else {
bufferedOutputStream.WriteString(verticalEnd)
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
bufferedOutputStream.WriteString(horizontalStart)
for pe := outrec.Head; pe != nil; pe = pe.Next {
bufferedOutputStream.WriteString(horizontalBars[pe.Key])
if pe.Next != nil {
bufferedOutputStream.WriteString(horizontalMiddle)
} else {
bufferedOutputStream.WriteString(horizontalEnd)
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
}
onFirst = false
// Print data lines
bufferedOutputStream.WriteString(verticalStart)
for pe := outrec.Head; pe != nil; pe = pe.Next {
s := pe.Value.String()
if !writer.writerOptions.RightAlignedPPRINTOutput { // left-align
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(s, outputIsStdout))
writer.writePadding(s, maxWidths[pe.Key], bufferedOutputStream)
} else { // right-align
writer.writePadding(s, maxWidths[pe.Key], bufferedOutputStream)
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(s, outputIsStdout))
}
if pe.Next != nil {
bufferedOutputStream.WriteString(fmt.Sprint(verticalMiddle))
} else {
bufferedOutputStream.WriteString(verticalEnd)
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
if e.Next() == nil {
bufferedOutputStream.WriteString(horizontalStart)
for pe := outrec.Head; pe != nil; pe = pe.Next {
bufferedOutputStream.WriteString(horizontalBars[pe.Key])
if pe.Next != nil {
bufferedOutputStream.WriteString(horizontalMiddle)
} else {
bufferedOutputStream.WriteString(horizontalEnd)
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
}
}
}
if writer.writerOptions.FlushOnEveryRecord {
bufferedOutputStream.Flush()
}
}
}
func (writer *RecordWriterPPRINT) writePadding(
text string,
fieldWidth int,
bufferedOutputStream *bufio.Writer,
) {
textWidth := utf8.RuneCountInString(text)
padWidth := fieldWidth - textWidth
ofs := writer.writerOptions.OFS
for i := 0; i < padWidth; i++ {
bufferedOutputStream.WriteString(ofs)
}
}