diff --git a/common.go b/common.go index c2cb6ad..f16f56c 100644 --- a/common.go +++ b/common.go @@ -5,6 +5,7 @@ package jsh import ( "encoding/json" "fmt" + "os" ) type JshFrame struct { @@ -22,22 +23,44 @@ func (j JshFrame) ToJson() *string { return &jsonString } +func (j JshFrame) StdOutToJson() *string { + jsonOut, err := json.Marshal(j.StdOut) + if err != nil { + panic(err) + } + jsonString := string(jsonOut) + return &jsonString +} + +func (j JshFrame) StdErrToJson() *string { + jsonOut, err := json.Marshal(j.StdErr) + if err != nil { + panic(err) + } + jsonString := string(jsonOut) + return &jsonString +} + // goroutine for outputing frames. Pass it a channel of pointers to JshFrames, // and it will send "true" to the done channel once you close the queue channel. func OutputFrames(queue chan *JshFrame, done chan bool) { fmt.Printf("[") + fmt.Fprintf(os.Stderr, "[") isFirst := true for { frame, more := <-queue if more { if !isFirst { fmt.Printf(",") + fmt.Fprintf(os.Stderr, ",") } else { isFirst = false } - fmt.Printf(*frame.ToJson()) + fmt.Printf(*frame.StdOutToJson()) + fmt.Fprintf(os.Stderr, *frame.StdErrToJson()) } else { fmt.Printf("]\n") + fmt.Fprintf(os.Stderr, "]\n") done <- true return }