-
Notifications
You must be signed in to change notification settings - Fork 207
/
record_writer_dkvp.go
49 lines (41 loc) · 1.22 KB
/
record_writer_dkvp.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
package output
import (
"bufio"
"github.com/johnkerl/miller/pkg/cli"
"github.com/johnkerl/miller/pkg/colorizer"
"github.com/johnkerl/miller/pkg/mlrval"
"github.com/johnkerl/miller/pkg/types"
)
type RecordWriterDKVP struct {
writerOptions *cli.TWriterOptions
}
func NewRecordWriterDKVP(writerOptions *cli.TWriterOptions) (*RecordWriterDKVP, error) {
return &RecordWriterDKVP{
writerOptions: writerOptions,
}, nil
}
func (writer *RecordWriterDKVP) Write(
outrec *mlrval.Mlrmap,
_ *types.Context,
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
) error {
if outrec == nil {
// End of record stream: nothing special for this output format
return nil
}
if outrec.IsEmpty() {
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
return nil
}
for pe := outrec.Head; pe != nil; pe = pe.Next {
bufferedOutputStream.WriteString(colorizer.MaybeColorizeKey(pe.Key, outputIsStdout))
bufferedOutputStream.WriteString(writer.writerOptions.OPS)
bufferedOutputStream.WriteString(colorizer.MaybeColorizeValue(pe.Value.String(), outputIsStdout))
if pe.Next != nil {
bufferedOutputStream.WriteString(writer.writerOptions.OFS)
}
}
bufferedOutputStream.WriteString(writer.writerOptions.ORS)
return nil
}