WSL: ANSI colors no longer render in cmd.exe
Please use the following bug reporting template to help produce actionable and reproducible issues. Please try to ensure that the reproduction is minimal so that the team can go through more bugs!
- A brief description https://github.com/Microsoft/BashOnWindows/issues/76 and https://github.com/Microsoft/BashOnWindows/issues/345 added support for ANSI colors. This works in version 10.0.10586 but got broken in version 10.0.14393.
https://github.com/symfony/symfony/issues/19520 confirms that other people are seeing the same problem.
- Expected results ANSI colors should render in cmd.exe when applications output to stdout/stderr.
- Actual results (with terminal output if applicable) Raw ANSI codes are seen. No colors are rendered.
- Your Windows build number 10.0.14393
- Steps / All commands required to reproduce the error from a brand new installation
- Run this Java testcase:
public class Test {
public static void main(String[] args) {
System.out.println("\033[37;1mwhite\033[0m");
}
}
- You will get this output:
[37;1mwhite[0m - Redirect the application stdout to
foo.logand runtype foo.log - Notice that the output now reads
whiteusing white text
This is reproducible 100% of the time.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 39 (7 by maintainers)
Commits related to this issue
- bugfix: fixed the issue of color not rendering in cmd/powershell https://github.com/microsoft/WSL/issues/1173 — committed to nkilm/git-logs by nkilm 4 years ago
As noted here and here, the ANSI VT can be enabled for SetConsoleMode-oblivious apps, without having to re-compile them, by changing the following registry key.
This changes the system-wide default for ANSI VT processing from opt-in to opt-out, so it will affect any/all apps that don’t express a preference. Works for Windows 10.16257 (and later) (and possibly earlier).
Given the very helpful information in a comment on this page by @zadjii-msft, this registry setting would appear to be the only workaround for apps that can’t be re-compiled to include a call to
SetConsoleMode. Because yes,cmd.exedoes respect this setting while it does the fiddling described in that remark.Oh my, you leave the office for one weekend, and this happens 😛
There’s a lot going on in here, so let me try to clarify: The windows console only supports VT sequences of ANY kind with the
ConsoleModeset to enable Virtual Terminal support. See This documentation onSetConsoleMode. Specifically, you need to turn on the output mode flagENABLE_VIRTUAL_TERMINAL_PROCESSING.By default,
cmd.exeenables VT mode for itself. Now here’s the confusion:SetConsoleModeyourself. IMPORTANT cmd also wipes out the changes a child makes to the mode. So you can’t callenable_vt.exe&myapp.exeand have the VT mode fromenable_vt.exepollute the mode ofmyapp.exeTo further confuse,
bash.exeturns on VT mode itself, because obviously, it needs that.I can’t really speak about the Java issue at hand, it’s been far too long since I’ve used Java, and I don’t really have any experience calling Win32 API’s from java. I did end up writing a snippet of code that enables VT support on Windows in python, so it can be done.
In conclusion: I’m going to leave this issue closed, as this is the proper, expected behavior. Confusing, but correct.
Also, in terms of hierarchy, @bitcrazed is the PM for the console, while I’m one of the devs
Just a note to anybody using Python.
You can enable ANSI using this:
Use this to enable terminal colors by updating the registry using Java :
@cowwoc For reporting bugs, you really need to be on the fast ring insider builds. The development here is done way faster than the Windows stable builds can keep up.
@zadjii-msft Thanks for the clarification. It helps a lot.
Out of curiosity, can you please provide concrete examples where enabling this flag by default broke app compatibility?
@fpqc I don’t really care about printf. I was just using it because you were doing the same in your testcase. The only thing I care about is getting this to work with Java.
ENABLE_VIRTUAL_TERMINAL_INPUT?ENABLE_VIRTUAL_TERMINAL_PROCESSINGIs only available on Windows 10 and up, in the v2 console.The v1 console (the only version of the console available on pre-Windows 10) probably doesn’t ignore flags that it doesn’t support, so you could probably SetConsoleMode with
ENABLE_VIRTUAL_TERMINAL_PROCESSINGand then have it roundtrip to your next call to GetConsoleMode, but that doesn’t imply that it’s actually enabled. I know that the v2 console will ignore flags it doesn’t understand, but I’m not positive about the v1.Edit: To add,
VirtualTerminalLevelwill additionally do nothing on the v1 console.@WASasquatch The error you pasted has nothing to do with Windows. Please google
NoClassDefFoundErrorand discuss with the jansi authors if necessary.@fpqc There is no way to set console modes in Java without using JNI (which is not a realistic option for my use-case). It is also unlikely Sun/Oracle would enable this in the near future.
I think it would help if someone from Microsoft address point 4: why were ANSI colors working in 10.0.10586 but got broken in version 10.0.14393? Something must have changed and I’m not convinced yet this was intentional.