diff --git a/internal/supervisor/output.go b/internal/supervisor/output.go index 10c3342..9cd3601 100644 --- a/internal/supervisor/output.go +++ b/internal/supervisor/output.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io" "log" "os" "sync" @@ -54,21 +55,23 @@ func (m *multiOutput) PipeOutput(proc *process) { pipe := m.openPipe(proc) go func(proc *process, pipe *ptyPipe) { - scanner := bufio.NewScanner(pipe.pty) - - for scanner.Scan() { - m.WriteLine(proc, scanner.Bytes()) + reader := bufio.NewReader(pipe.pty) + for { + line, err := reader.ReadBytes('\n') + // Only write non-empty lines. + if len(line) > 0 { + m.WriteLine(proc, line) + } + if err != nil { + if err != io.EOF { + log.Printf("reader error: %v", err) + } + break + } } }(proc, pipe) } -func (m *multiOutput) ClosePipe(proc *process) { - if pipe := m.pipes[proc]; pipe != nil { - _ = pipe.pty.Close() - _ = pipe.tty.Close() - } -} - func (m *multiOutput) WriteLine(proc *process, p []byte) { var buf bytes.Buffer @@ -83,6 +86,8 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) { buf.WriteString("\033[0m | ") + // remove trailing newline if present. + p = bytes.TrimSuffix(p, []byte("\n")) buf.Write(p) buf.WriteByte('\n') @@ -95,6 +100,13 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) { } } +func (m *multiOutput) ClosePipe(proc *process) { + if pipe := m.pipes[proc]; pipe != nil { + _ = pipe.pty.Close() + _ = pipe.tty.Close() + } +} + func (m *multiOutput) WriteErr(proc *process, err error) { m.WriteLine(proc, []byte( fmt.Sprintf("\033[0;31m%v\033[0m", err),