Skip to content

Move to bufio reader to support larger tokens #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions internal/supervisor/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
Expand Down Expand Up @@ -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

Expand All @@ -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')

Expand All @@ -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),
Expand Down
Loading