From 485beb126bdb7c1fa8591c3e875aac73aec6fe90 Mon Sep 17 00:00:00 2001 From: Thijs Brobbel Date: Tue, 5 Jul 2022 16:37:47 +0200 Subject: [PATCH] Enable ANSI escape sequence processing on Windows (#1045) Windows added support for ANSI escape sequences processing, and showing colors, and such. However, this needs to be enabled explicitly, for that to work. This enables that on startup. --- go.mod | 1 + go.sum | 2 ++ internal/pkg/entrypoint/entrypoint.go | 4 ++++ internal/pkg/platform/terminal_notwindows.go | 8 ++++++++ internal/pkg/platform/terminal_windows.go | 16 ++++++++++++++++ 5 files changed, 31 insertions(+) create mode 100644 internal/pkg/platform/terminal_notwindows.go create mode 100644 internal/pkg/platform/terminal_windows.go diff --git a/go.mod b/go.mod index 20b4de04c..c468ea0cb 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/lestrrat-go/strftime v1.0.6 github.com/mattn/go-isatty v0.0.14 + github.com/nine-lives-later/go-windows-terminal-sequences v1.0.4 // indirect github.com/pkg/profile v1.6.0 github.com/stretchr/testify v1.8.0 golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 diff --git a/go.sum b/go.sum index c93d58408..952915574 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/nine-lives-later/go-windows-terminal-sequences v1.0.4 h1:NC4H8hewgaktBqMI5yzy6L/Vln5/H7BEziyxaE2fX3Y= +github.com/nine-lives-later/go-windows-terminal-sequences v1.0.4/go.mod h1:eUQxpEiJy001RoaLXrNa5+QQLYiEgmEafwWuA3ppJSo= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM= diff --git a/internal/pkg/entrypoint/entrypoint.go b/internal/pkg/entrypoint/entrypoint.go index 6445b8733..91e769f8c 100644 --- a/internal/pkg/entrypoint/entrypoint.go +++ b/internal/pkg/entrypoint/entrypoint.go @@ -33,6 +33,10 @@ func Main() MainReturn { // as-is.) os.Args = platform.GetArgs() + // Enable ANSI escape sequence processing on the Windows pseudo terminal, + // otherwise, we only raw ANSI escape sequences like ←[0;30m 0←[0m ←[0;31m 1 + platform.EnableAnsiEscapeSequences() + // Expand "-xyz" into "-x -y -z" while leaving "--xyz" intact. This is a // keystroke-saver for the user. // diff --git a/internal/pkg/platform/terminal_notwindows.go b/internal/pkg/platform/terminal_notwindows.go new file mode 100644 index 000000000..0277dcbab --- /dev/null +++ b/internal/pkg/platform/terminal_notwindows.go @@ -0,0 +1,8 @@ + +//go:build !windows +// +build !windows + +package platform + +func EnableAnsiEscapeSequences() { +} \ No newline at end of file diff --git a/internal/pkg/platform/terminal_windows.go b/internal/pkg/platform/terminal_windows.go new file mode 100644 index 000000000..844c382cb --- /dev/null +++ b/internal/pkg/platform/terminal_windows.go @@ -0,0 +1,16 @@ + +//go:build windows +// +build windows + +package platform + +import ( + "syscall" + + sequences "github.com/nine-lives-later/go-windows-terminal-sequences" +) + +func EnableAnsiEscapeSequences() { + sequences.EnableVirtualTerminalProcessing(syscall.Stdout, true) + sequences.EnableVirtualTerminalProcessing(syscall.Stderr, true) +} \ No newline at end of file