[issue40134] Inconsistent ANSI escape code handling on Windows 10

Paul Moore report at bugs.python.org
Wed Apr 1 12:15:17 EDT 2020


Paul Moore <p.f.moore at gmail.com> added the comment:

This works fine for me in Windows terminal, but I see the behaviour described when using the conventional "Command prompt" window.

Enabling ANSI codes is handled via SetConsoleMode (see here: https://docs.microsoft.com/en-us/windows/console/setconsolemode). The following proof of concept script correctly displays coloured text:

    from ctypes import *
    kernel32 = windll.kernel32
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
    ansi_red = "\x1b[31m"
    ansi_normal = "\x1b[0m"
    print(ansi_red + "This is red!" + ansi_normal)

Agreed this would be worthwhile setting on stdout by default. The code at https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing seems to be an example of how to do this while still supporting older systems

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40134>
_______________________________________


More information about the Python-bugs-list mailing list