4
4
"bufio"
5
5
"bytes"
6
6
"fmt"
7
+ "io"
7
8
"log"
8
9
"os"
9
10
"sync"
@@ -54,21 +55,23 @@ func (m *multiOutput) PipeOutput(proc *process) {
54
55
pipe := m .openPipe (proc )
55
56
56
57
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
+ }
61
71
}
62
72
}(proc , pipe )
63
73
}
64
74
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
-
72
75
func (m * multiOutput ) WriteLine (proc * process , p []byte ) {
73
76
var buf bytes.Buffer
74
77
@@ -83,6 +86,8 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) {
83
86
84
87
buf .WriteString ("\033 [0m | " )
85
88
89
+ // remove trailing newline if present.
90
+ p = bytes .TrimSuffix (p , []byte ("\n " ))
86
91
buf .Write (p )
87
92
buf .WriteByte ('\n' )
88
93
@@ -95,6 +100,13 @@ func (m *multiOutput) WriteLine(proc *process, p []byte) {
95
100
}
96
101
}
97
102
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
+
98
110
func (m * multiOutput ) WriteErr (proc * process , err error ) {
99
111
m .WriteLine (proc , []byte (
100
112
fmt .Sprintf ("\033 [0;31m%v\033 [0m" , err ),
0 commit comments