From 3d1b206ac85e970831de70d1e825af9b84d18216 Mon Sep 17 00:00:00 2001 From: Shaun Davis Date: Mon, 3 Feb 2025 09:18:59 -0600 Subject: [PATCH 1/3] Move to bufio reader to support larger tokens --- internal/supervisor/output.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/supervisor/output.go b/internal/supervisor/output.go index 10c33423..ee0e2f53 100644 --- a/internal/supervisor/output.go +++ b/internal/supervisor/output.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io" "log" "os" "sync" @@ -54,10 +55,17 @@ 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') + if err != nil { + // EOF is expected, so don't log it + if err != io.EOF { + log.Printf("reader error: %v", err) + } + break + } + m.WriteLine(proc, line) } }(proc, pipe) } From e1683cffe54f17f8269c446a8d0eeb4ebee81d46 Mon Sep 17 00:00:00 2001 From: Shaun Davis Date: Thu, 6 Feb 2025 11:10:05 -0600 Subject: [PATCH 2/3] Cleanup --- internal/supervisor/output.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/supervisor/output.go b/internal/supervisor/output.go index ee0e2f53..2b0af525 100644 --- a/internal/supervisor/output.go +++ b/internal/supervisor/output.go @@ -58,25 +58,20 @@ func (m *multiOutput) PipeOutput(proc *process) { reader := bufio.NewReader(pipe.pty) for { line, err := reader.ReadBytes('\n') + + // Write to console regardless of whether there's an error. + m.WriteLine(proc, line) + if err != nil { - // EOF is expected, so don't log it if err != io.EOF { log.Printf("reader error: %v", err) } break } - m.WriteLine(proc, line) } }(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 @@ -91,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') @@ -103,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), From 44b0b8e978990f918304576ef65936f72470eca5 Mon Sep 17 00:00:00 2001 From: Shaun Davis Date: Thu, 6 Feb 2025 11:12:17 -0600 Subject: [PATCH 3/3] Only write non-empty lines --- internal/supervisor/output.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/supervisor/output.go b/internal/supervisor/output.go index 2b0af525..9cd36017 100644 --- a/internal/supervisor/output.go +++ b/internal/supervisor/output.go @@ -58,10 +58,10 @@ func (m *multiOutput) PipeOutput(proc *process) { reader := bufio.NewReader(pipe.pty) for { line, err := reader.ReadBytes('\n') - - // Write to console regardless of whether there's an error. - m.WriteLine(proc, line) - + // 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)