[issue40134] Inconsistent ANSI escape code handling on Windows 10

2020-04-01 Thread Paul Moore


Paul Moore  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40134] Inconsistent ANSI escape code handling on Windows 10

2020-04-01 Thread Dave Rove


New submission from Dave Rove :

The correct handling of ANSI escape codes by the print() function may or may 
not be enabled in the Windows 10 command prompt window, depending on previous 
system calls. The following is quite repeatable. Comment-out the apparently 
meaningless os.system("") line and ANSI codes do not work, but leave that line 
in and ANSI codes DO work:

import os
os.system("") # Comment this out to disable ANSI codes
ansi_red = "\x1b[31m"
ansi_normal = "\x1b[0m"
print(ansi_red + "This is red!" + ansi_normal)

To be consistent with Python on Linux and Mac, I believe that ANSI codes should 
be permanently enabled in Windows 10 rather than removed. ANSI code handling 
was present from the start of Windows 10, so it's reasonable to presume that 
it's now a permanent feature of the Windows command prompt window. Either way, 
the inconsistency of the handling should be fixed.

To emphasize that ANSI codes ARE a feature of the command prompt, comment out 
that line to disable the ANSI codes in print(), but redirect the output to a 
text file. Then display that file at the command prompt. The ANSI codes then 
work correctly. 

python myansi.py > myansi.txt
type myansi.txt

--
components: Windows
messages: 365458
nosy: daverove, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Inconsistent ANSI escape code handling on Windows 10
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com