Skip to content

Commit 5a04d2b

Browse files
authored
Move to bufio reader to support larger tokens (#273)
* Move to bufio reader to support larger tokens * Cleanup * Only write non-empty lines
1 parent bb46120 commit 5a04d2b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

internal/supervisor/output.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"fmt"
7+
"io"
78
"log"
89
"os"
910
"sync"
@@ -54,21 +55,23 @@ func (m *multiOutput) PipeOutput(proc *process) {
5455
pipe := m.openPipe(proc)
5556

5657
go func(proc *process, pipe *ptyPipe) {
57-
scanner := bufio.NewScanner(pipe.pty)
58-
59-
for scanner.Scan() {
60-
m.WriteLine(proc, scanner.Bytes())
58+
reader := bufio.NewReader(pipe.pty)
59+
for {
60+
line, err := reader.ReadBytes('\n')
61+
// Only write non-empty lines.
62+
if len(line) > 0 {
63+
m.WriteLine(proc, line)
64+
}
65+
if err != nil {
66+
if err != io.EOF {
67+
log.Printf("reader error: %v", err)
68+
}
69+
break
70+
}
6171
}
6272
}(proc, pipe)
6373
}
6474

65-
func (m *multiOutput) ClosePipe(proc *process) {
66-
if pipe := m.pipes[proc]; pipe != nil {
67-
_ = pipe.pty.Close()
68-
_ = pipe.tty.Close()
69-
}
70-
}
71-
7275
func (m *multiOutput) WriteLine(proc *process, p []byte) {
7376
var buf bytes.Buffer
7477

@@ -83,6 +86,8 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) {
8386

8487
buf.WriteString("\033[0m | ")
8588

89+
// remove trailing newline if present.
90+
p = bytes.TrimSuffix(p, []byte("\n"))
8691
buf.Write(p)
8792
buf.WriteByte('\n')
8893

@@ -95,6 +100,13 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) {
95100
}
96101
}
97102

103+
func (m *multiOutput) ClosePipe(proc *process) {
104+
if pipe := m.pipes[proc]; pipe != nil {
105+
_ = pipe.pty.Close()
106+
_ = pipe.tty.Close()
107+
}
108+
}
109+
98110
func (m *multiOutput) WriteErr(proc *process, err error) {
99111
m.WriteLine(proc, []byte(
100112
fmt.Sprintf("\033[0;31m%v\033[0m", err),

0 commit comments

Comments
 (0)