From 64da3bcf7a9a8cf82fc7cb3e6658b91ce6304972 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 30 Dec 2020 22:08:38 -0500 Subject: [PATCH] Use correct output when enabling ANSI colors on Windows (see #39) --- tty.go | 2 +- tty_unix.go | 4 +++- tty_windows.go | 10 ++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tty.go b/tty.go index 25f2415..6e50141 100644 --- a/tty.go +++ b/tty.go @@ -15,7 +15,7 @@ func initTerminal(w io.Writer) error { return err } - enableAnsiColors() + enableAnsiColors(w) hideCursor(w) return nil } diff --git a/tty_unix.go b/tty_unix.go index f71efeb..c2b0015 100644 --- a/tty_unix.go +++ b/tty_unix.go @@ -2,6 +2,8 @@ package tea +import "io" + // enableAnsiColors is only needed for Windows, so for other systems this is // a no-op. -func enableAnsiColors() {} +func enableAnsiColors(w io.Writer) {} diff --git a/tty_windows.go b/tty_windows.go index b9ddd4b..cfb0351 100644 --- a/tty_windows.go +++ b/tty_windows.go @@ -3,6 +3,7 @@ package tea import ( + "io" "os" "golang.org/x/sys/windows" @@ -10,8 +11,13 @@ import ( // enableAnsiColors enables support for ANSI color sequences in Windows // default console. Note that this only works with Windows 10. -func enableAnsiColors() { - stdout := windows.Handle(os.Stdout.Fd()) +func enableAnsiColors(w io.Writer) { + f, ok := w.(*os.File) + if !ok { + return + } + + stdout := windows.Handle(f.Fd()) var originalMode uint32 windows.GetConsoleMode(stdout, &originalMode)