diff --git a/cmd.go b/cmd.go index 8902d11..81f9fc2 100644 --- a/cmd.go +++ b/cmd.go @@ -126,6 +126,10 @@ type Options struct { // faster and more efficient than polling Cmd.Status. The caller must read both // streaming channels, else lines are dropped silently. Streaming bool + + // If Streaming is true and SplitChar is set, Cmd.Stdout and Cmd.Stderr + // channels will be split using this char instead of the default '\n'. + SplitChar *byte } // NewCmdOptions creates a new Cmd with options. The command is not started @@ -157,6 +161,11 @@ func NewCmdOptions(options Options, name string, args ...string) *Cmd { c.Stderr = make(chan string, DEFAULT_STREAM_CHAN_SIZE) c.stderrStream = NewOutputStream(c.Stderr) + + if options.SplitChar != nil { + c.stdoutStream.splitChar = *options.SplitChar + c.stderrStream.splitChar = *options.SplitChar + } } return c @@ -554,6 +563,7 @@ type OutputStream struct { bufSize int buf []byte lastChar int + splitChar byte } // NewOutputStream creates a new streaming output on the given channel. The @@ -563,9 +573,10 @@ func NewOutputStream(streamChan chan string) *OutputStream { out := &OutputStream{ streamChan: streamChan, // -- - bufSize: DEFAULT_LINE_BUFFER_SIZE, - buf: make([]byte, DEFAULT_LINE_BUFFER_SIZE), - lastChar: 0, + bufSize: DEFAULT_LINE_BUFFER_SIZE, + buf: make([]byte, DEFAULT_LINE_BUFFER_SIZE), + lastChar: 0, + splitChar: '\n', } return out } @@ -581,7 +592,7 @@ func (rw *OutputStream) Write(p []byte) (n int, err error) { // can contain multiple lines, like "foo\nbar". So in that case nextLine // will be 0 ("foo\nbar\n") then 4 ("bar\n") on next iteration. And i // will be 3 and 7, respectively. So lines are [0:3] are [4:7]. - newlineOffset := bytes.IndexByte(p[firstChar:], '\n') + newlineOffset := bytes.IndexByte(p[firstChar:], rw.splitChar) if newlineOffset < 0 { break // no newline in stream, next line incomplete }