corneliusweig / rakkess

Review Access - kubectl plugin to show an access matrix for k8s server resources
Apache License 2.0
1.3k stars 56 forks source link

Color codes garble output in PowerShell #6

Closed corneliusweig closed 5 years ago

corneliusweig commented 5 years ago

Expected behavior: Output on PowerShell terminals is simply monochrome and well-formatted.

Actual behavior: Unix terminal color codes are printed in PowerShell, thus making the output unreadable.

Relates to #5

itowlson commented 5 years ago

This occurs because the Go terminal.IsTerminal function returns true for PowerShell terminals (see implementation at https://go.googlesource.com/crypto/+/refs/heads/master/ssh/terminal/util_windows.go). Which is fair enough because the PowerShell console is a terminal, just not one that consistently supports VT100 sequences.

Looking at https://docs.microsoft.com/en-us/windows/console/getconsolemode, I think what we're really interested in is whether the current console mode includes the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. Obviously Go doesn't surface this from its libraries, but we could try replacing the IsTerminal call with a custom SupportsEscapeSequences function which would return IsTerminal on Unix but would additionally check the console mode on Windows. This gives a better user experience but is more invasive and risky (ripping out a stdlib function and replacing it with a custom one).

Alternatively, we could just turn colouring off entirely on Windows, i.e. the check for colouring would not be IsTerminal but IsTerminal && IsUnix.

Let me know your preference @corneliusweig - I have to admit I'm inclined to take the second option for now as it will be simpler and will still achieve the immediate goal of 'simple and well formatted.'

corneliusweig commented 5 years ago

Wow, thanks for your thorough investigation! You seem to be a PowerShell wizard :)

I had thoughts into the same direction, to completely turn off colors on windows. Alas, that would also turn off colors on terminals that do support colors, like the widespread git-shell. However, your idea opens up a far superior alternative. I think we should definitely try this one out -- this tool is not production critical, so it's a good test balloon. Other projects could benefit from this solution too, if it works reliably!

So let's try your idea about SupportsEscapeSequences.