Home > OS >  Python - ANSI Terminal Colors w/o Enabled in Registry
Python - ANSI Terminal Colors w/o Enabled in Registry

Time:08-22

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!

CodePudding user response:

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.

  • Related