Skip to content
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