2016-05-23 11:19:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
type messageChanWriter struct {
|
|
|
|
msgChan chan string
|
|
|
|
|
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
2016-05-23 14:04:50 +00:00
|
|
|
func newMessageChanWriter(outputChannel chan string) *messageChanWriter {
|
2016-05-23 11:19:25 +00:00
|
|
|
return &messageChanWriter{
|
|
|
|
msgChan: outputChannel,
|
|
|
|
buffer: []byte{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:06:28 +00:00
|
|
|
func (m *messageChanWriter) Write(p []byte) (int, error) {
|
|
|
|
var (
|
|
|
|
n = len(p)
|
|
|
|
err error
|
|
|
|
)
|
2016-05-23 11:19:25 +00:00
|
|
|
|
|
|
|
m.buffer = append(m.buffer, p...)
|
|
|
|
if strings.Contains(string(m.buffer), "\n") {
|
|
|
|
lines := strings.Split(string(m.buffer), "\n")
|
|
|
|
for _, l := range lines[:len(lines)-1] {
|
|
|
|
m.msgChan <- l
|
|
|
|
}
|
|
|
|
m.buffer = []byte(lines[len(lines)-1])
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:06:28 +00:00
|
|
|
return n, err
|
2016-05-23 11:19:25 +00:00
|
|
|
}
|