Skip to content

Commit 7f381b6

Browse files
authored
fix(iostreams): Translation of ANSI escape on Windows for stderr (#2209)
Reviewed-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io> Approved-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
2 parents df0070d + d678c56 commit 7f381b6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

iostreams/iostreams.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type FileWriter interface {
5252
Fd() uintptr
5353
}
5454

55-
type fileReader interface {
55+
type FileReader interface {
5656
io.ReadCloser
5757
Fd() uintptr
5858
}
@@ -69,9 +69,9 @@ type Terminal interface {
6969
type IOStreams struct {
7070
term Terminal
7171

72-
In fileReader
72+
In FileReader
7373
Out FileWriter
74-
ErrOut io.Writer
74+
ErrOut FileWriter
7575

7676
terminalTheme string
7777

@@ -158,7 +158,7 @@ func (s *IOStreams) SetOut(out FileWriter) {
158158
s.Out = out
159159
}
160160

161-
func (s *IOStreams) SetIn(in fileReader) {
161+
func (s *IOStreams) SetIn(in FileReader) {
162162
s.In = in
163163
}
164164

@@ -429,10 +429,21 @@ func System() *IOStreams {
429429
}
430430
}
431431

432+
var stderr FileWriter = os.Stderr
433+
// On Windows with no virtual terminal processing support, translate ANSI escape
434+
// sequences to console syscalls.
435+
if colorableStderr := colorable.NewColorable(os.Stderr); colorableStderr != os.Stderr {
436+
// Ensure that the file descriptor of the original stderr is preserved.
437+
stderr = &fdWriter{
438+
fd: os.Stderr.Fd(),
439+
Writer: colorableStderr,
440+
}
441+
}
442+
432443
io := &IOStreams{
433444
In: os.Stdin,
434445
Out: stdout,
435-
ErrOut: colorable.NewColorable(os.Stderr),
446+
ErrOut: stderr,
436447
pagerCommand: os.Getenv("PAGER"),
437448
term: &terminal,
438449
}

tui/processtree/processtree.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func NewProcessTree(ctx context.Context, opts []ProcessTreeOption, tree ...*Proc
148148
}
149149

150150
ios.Out = iostreams.NewNoTTYWriter(item, iostreams.G(ctx).Out.Fd())
151-
ios.ErrOut = item
151+
ios.ErrOut = iostreams.NewNoTTYWriter(item, iostreams.G(ctx).ErrOut.Fd())
152152
ios.In = iostreams.G(ctx).In
153153
ictx = iostreams.WithIOStreams(ictx, ios)
154154

@@ -189,7 +189,7 @@ func (pti *ProcessTreeItem) Write(p []byte) (int, error) {
189189
return len(p), nil
190190
}
191191

192-
func (pti *ProcessTreeItem) Fd() int {
192+
func (pti *ProcessTreeItem) Fd() uintptr {
193193
return 0
194194
}
195195

0 commit comments

Comments
 (0)