Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors don't work in Windows 10 command prompt + powershell #54

Closed
SemmieDev opened this issue Feb 24, 2021 · 2 comments
Closed

Colors don't work in Windows 10 command prompt + powershell #54

SemmieDev opened this issue Feb 24, 2021 · 2 comments
Assignees

Comments

@SemmieDev
Copy link

Describe the bug
This works fine everywhere else for me except in the command prompt and powershell in Windows 10. I am not talking about the Windows Terminal app, it works fine in there.

To Reproduce
Steps to reproduce the behavior:

  1. Make a new Java project or edit an existing one.
  2. Add a simple line of code that will log colorized text into console.
  3. Run the project in the command prompt or in powershell.
  4. The colorized text will have gibberish around it.

Expected behavior
I expected the text to be colorized, which I have seen some programs doing.

Screenshots
Powershell:
image
Command prompt:
image
Windows Terminal (expected behavior):
image

Environment

  • OS: Windows
  • Version: 10
@dialex
Copy link
Owner

dialex commented Feb 27, 2021

Apparently this is the reason why it doesn't work for cmd and powershell: https://superuser.com/a/1300251/97034

the new support for ANSI Terminal Control which was added to the Windows 10 console host in build 16257 (and later). Namely, it's n̲o̲t̲ e̲n̲a̲b̲l̲e̲d̲ by default. Unless the specific software you're using enables ANSI processing by calling the SetConsoleMode API with the ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x0400) flag, you won't see colors or get ANSI processing for that application.

It's confirmed again at https://stackoverflow.com/a/35864976/675577

I'll try to fix it with some kind of if windows then set variable. Apparently I used to have that behaviour e467333, through this method

private void enableWindows10AnsiSupport() {
, but dropped it for some reason. I think it was because I thought after all this time Microsoft had enabled that behaviour by default.

P.S: Thanks for raising the ticket with all those details! 👌

@dialex
Copy link
Owner

dialex commented Feb 27, 2021

Looks like you as the user can enable that flag once and forever: https://stackoverflow.com/a/51681675/675577

Can you try these instructions on your machine, and tell me if it fixes the issue? Don't run both, choose powershell or cmd, and run the fix only for that one. We want to have one of the programs behaving incorrectly so we can test my fix later.


  • (a) Activate support globally by default, persistently, via the registry, as detailed in this SU answer.
    • In short: In registry key [HKEY_CURRENT_USER\Console], create or set the VirtualTerminalLevel DWORD value to 1
      • From PowerShell, you can do this programmatically as follows:
        Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1
      • From cmd.exe (also works from PowerShell):
        reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1
    • Open a new console window for changes to take effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants