Python – ANSI Terminal Colors w/o Enabled in Registry

Question:

Using ANSI Escape Sequences we can give terminals a nice vibrant color, however for Windows this much be enabled via Registry.

There is a module named Colorama which works even if terminal colors are disabled. I’ve looked through the source and I’m unable to find how it works.

I could set the registry key but 1. That would require elevation and 2. That would require the console to be restarted to work. Is there any way to avoid this how Colorama has?

Thank you!

Asked By: DeKrypt

||

Answers:

In the docs, it is listed that it doesnt actually use ANSI escape sequences, but insteads converts them on the fly to Win32 calls:

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

Here is the Win documentation:
https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute?redirectedfrom=MSDN

The above syscall does not require elevation nor a registry key. So Colorama is essentially a clever interpreter/converter.

Answered By: nordmanden
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.