Skip to content

Commit

Permalink
Move to bufio reader to support larger tokens (#273)
Browse files Browse the repository at this point in the history
* Move to bufio reader to support larger tokens

* Cleanup

* Only write non-empty lines
  • Loading branch information
davissp14 authored Feb 6, 2025
1 parent bb46120 commit 5a04d2b
Showing 1 changed file with 23 additions and 11 deletions.
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

0 comments on commit 5a04d2b

Please sign in to comment.