aboutsummaryrefslogtreecommitdiff
path: root/writer/linesplitwriter.go
blob: ebb90c4e90849640c1cf51a88bc3633ae0f3da0d (plain)
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
package writer

import (
	"bytes"
	"io"
)

func LineSplitting(sink io.Writer) io.Writer {
	return lineSplittingWriter{writer: sink}
}

type lineSplittingWriter struct {
	writer io.Writer
}

func (lsw lineSplittingWriter) Write(p []byte) (int, error) {
	for _, line := range bytes.Split(p, []byte("\n")) {
		if len(line) == 0 {
			continue
		}

		_, err := lsw.writer.Write(append(line, '\n'))
		if err != nil {
			return -1, err
		}
	}

	return len(p), nil
}