From 86657918d4caea070abf7e00baf23913c12f4802 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 12 Jan 2020 01:49:45 +0100 Subject: [PATCH] Simplify checkIfTerminal for Windows Instead of relying on EnableVirtualTerminalProcessing from github.com/konsorten/go-windows-terminal-sequences which just calls GetConsoleMode, sets ENABLE_VIRTUAL_TERMINAL_PROCESSING and calls SetConsoleMode with the new modified mode, implement it directly inside checkIfTerminal. This also avoids the duplicate call to GetConsoleMode. --- go.mod | 1 - go.sum | 4 ---- terminal_check_windows.go | 29 +++++++++++------------------ travis/install.sh | 2 +- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index d413296..324cefd 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module github.com/sirupsen/logrus require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/konsorten/go-windows-terminal-sequences v1.0.3 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 golang.org/x/sys v0.0.0-20190422165155-953cdadca894 diff --git a/go.sum b/go.sum index 49c690f..c574edc 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= diff --git a/terminal_check_windows.go b/terminal_check_windows.go index 572889d..2879eb5 100644 --- a/terminal_check_windows.go +++ b/terminal_check_windows.go @@ -5,30 +5,23 @@ package logrus import ( "io" "os" - "syscall" - sequences "github.com/konsorten/go-windows-terminal-sequences" + "golang.org/x/sys/windows" ) -func initTerminal(w io.Writer) { - switch v := w.(type) { - case *os.File: - sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) - } -} - func checkIfTerminal(w io.Writer) bool { - var ret bool switch v := w.(type) { case *os.File: + handle := windows.Handle(v.Fd()) var mode uint32 - err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode) - ret = (err == nil) - default: - ret = false + if err := windows.GetConsoleMode(handle, &mode); err != nil { + return false + } + mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING + if err := windows.SetConsoleMode(handle, mode); err != nil { + return false + } + return true } - if ret { - initTerminal(w) - } - return ret + return false } diff --git a/travis/install.sh b/travis/install.sh index 6fa9486..5fc40dd 100755 --- a/travis/install.sh +++ b/travis/install.sh @@ -18,5 +18,5 @@ fi if [[ "$GO111MODULE" == "off" ]]; then # Should contain all regular (not indirect) modules from go.mod - go get github.com/stretchr/testify golang.org/x/sys/unix github.com/konsorten/go-windows-terminal-sequences + go get github.com/stretchr/testify golang.org/x/sys/unix golang.org/x/sys/windows fi