From e09a4ff0193731242606bad27e422a144de528c1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 13 Sep 2019 15:24:47 +0100 Subject: [PATCH] cmd: Make --progress work in git bash on Windows - fixes #3531 This detects the presence of a VT100 terminal by using the TERM environment variable and switches to using VT100 codes directly under windows if it is found. This makes --progress work correctly with git bash. --- cmd/progress.go | 19 +++++++++++++++++++ cmd/progress_other.go | 12 ++++-------- cmd/progress_windows.go | 10 ++++++++-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/cmd/progress.go b/cmd/progress.go index 0ea44501a..124d99108 100644 --- a/cmd/progress.go +++ b/cmd/progress.go @@ -23,10 +23,29 @@ const ( logTimeFormat = "2006-01-02 15:04:05" ) +var ( + initTerminal func() error + writeToTerminal func([]byte) +) + +// Initialise the VT100 terminal +func initTerminalVT100() error { + return nil +} + +// Write to the VT100 terminal +func writeToTerminalVT100(b []byte) { + _, _ = os.Stdout.Write(b) +} + // startProgress starts the progress bar printing // // It returns a func which should be called to stop the stats. func startProgress() func() { + if os.Getenv("TERM") != "" { + initTerminal = initTerminalVT100 + writeToTerminal = writeToTerminalVT100 + } err := initTerminal() if err != nil { fs.Errorf(nil, "Failed to start progress: %v", err) diff --git a/cmd/progress_other.go b/cmd/progress_other.go index ec1e223c7..ccefc1773 100644 --- a/cmd/progress_other.go +++ b/cmd/progress_other.go @@ -2,12 +2,8 @@ package cmd -import "os" - -func initTerminal() error { - return nil -} - -func writeToTerminal(b []byte) { - _, _ = os.Stdout.Write(b) +func init() { + // Default terminal is VT100 for non Windows + initTerminal = initTerminalVT100 + writeToTerminal = writeToTerminalVT100 } diff --git a/cmd/progress_windows.go b/cmd/progress_windows.go index c8943fc24..141eaeed8 100644 --- a/cmd/progress_windows.go +++ b/cmd/progress_windows.go @@ -16,7 +16,13 @@ var ( ansiParser *ansiterm.AnsiParser ) -func initTerminal() error { +func init() { + // Default terminal is Windows console for Windows + initTerminal = initTerminalWindows + writeToTerminal = writeToTerminalWindows +} + +func initTerminalWindows() error { winEventHandler := winterm.CreateWinEventHandler(os.Stdout.Fd(), os.Stdout) if winEventHandler == nil { err := syscall.GetLastError() @@ -29,7 +35,7 @@ func initTerminal() error { return nil } -func writeToTerminal(b []byte) { +func writeToTerminalWindows(b []byte) { // Remove all non-ASCII characters until this is fixed // https://github.com/Azure/go-ansiterm/issues/26 r := []rune(string(b))