Better check for ANSI-enabled terminal on Windows

I just started to learning Elixir. I’m a very visual guy, so I like my terminal colors very much. I noticed on YouTube, that the mix command does actually produce colored output, which led me down a rabbit hole.

Somewhere in the middle of it, I found the following code part in C:\Program Files (x86)\Elixir\bin\elixir.bat

reg query HKCU\Console /v VirtualTerminalLevel 2>nul | findstr /e "0x1" >nul 2>nul
if %errorlevel% == 0 (
  set beforeExtra=-elixir ansi_enabled true !beforeExtra!

Windows Terminal does support ANSI/VT100 escape sequences, regardless of whether this key exists or not. I don’t know when support was added, but I believe it was in 2021, if not 2022. Changing the above code part to

set beforeExtra=-elixir ansi_enabled true !beforeExtra!

successfully enabled colors on commands like mix hello when in a Windows Terminal session.

Unfortunately, I don’t know how to reliably check if the current console host is Windows Terminal. But I can observe that Windows Terminal adds two environment variables WT_PROFILE_ID and WT_SESSION. Maybe this could be leveraged to improve elixir.bat to not rely solely on the existence of a registry key that the majority of people don’t know or care about (no offense).

What do you think?

Erlang/OTP 26 (to be released in May 2023) has a completely rewritten shell implementation that solves the problem for windows. So if you wait a while the problem will go away on its own.

1 Like

In the meanwhile probably just set the registry key.
Documentation about it is probably what is hard to find if starting with Elixir.
IEx - Encoding and coloring

But then you’re are also good to go on Windows with the current situation.
PS: I’m also waiting with pleasure for the new shell implementation in OTP 26.

And yes your suggestion, detecting Windows Terminal, would probably be a good one with Windows Terminal getting standard anywhere in the future…

& welcome to Elixir.

Great, thanks for pointing me to the PR. Until then, I just leave the workaround in the script file.