From 1cccfa7331ebf7d983e27c1d6847649a8e6a5dd0 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 17 Aug 2018 16:59:59 +0100 Subject: [PATCH] cmd: Make --progress work on Windows --- cmd/progress.go | 2 +- cmd/progress_other.go | 9 +++++++++ cmd/progress_windows.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cmd/progress_other.go create mode 100644 cmd/progress_windows.go diff --git a/cmd/progress.go b/cmd/progress.go index 5aa43fc7d..a76acf882 100644 --- a/cmd/progress.go +++ b/cmd/progress.go @@ -114,5 +114,5 @@ func printProgress(logMessage string) { out("\n") } } - os.Stdout.Write(buf.Bytes()) + writeToTerminal(buf.Bytes()) } diff --git a/cmd/progress_other.go b/cmd/progress_other.go new file mode 100644 index 000000000..aef00cd4b --- /dev/null +++ b/cmd/progress_other.go @@ -0,0 +1,9 @@ +//+build !windows + +package cmd + +import "os" + +func writeToTerminal(b []byte) { + _, _ = os.Stdout.Write(b) +} diff --git a/cmd/progress_windows.go b/cmd/progress_windows.go new file mode 100644 index 000000000..aa7493b8f --- /dev/null +++ b/cmd/progress_windows.go @@ -0,0 +1,28 @@ +//+build windows + +package cmd + +import ( + "fmt" + "os" + "sync" + + ansiterm "github.com/Azure/go-ansiterm" + "github.com/Azure/go-ansiterm/winterm" +) + +var ( + initAnsiParser sync.Once + ansiParser *ansiterm.AnsiParser +) + +func writeToTerminal(b []byte) { + initAnsiParser.Do(func() { + winEventHandler := winterm.CreateWinEventHandler(os.Stdout.Fd(), os.Stdout) + ansiParser = ansiterm.CreateParser("Ground", winEventHandler) + }) + _, err := ansiParser.Parse(b) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "\n*** Error from ANSI parser: %v\n", err) + } +}