The latest Windows 10 updates include support for ANSI escape sequences in conhost.exe.
I have been able to confirm that the escape sequences are properly picked up in cmd.exe, so I have the necessary updates. In particular, I tried typing in prompt $e[?25l
, which hides the cursor, and then prompt $e[?25h
, which again shows the cursor.
However, if I start a Python interpreter, and then do the following:
>>> import sys
>>> sys.stdout.write("\033[?25l")
Well, the cursor isn't hidden. How can I set things up the right way so that the console is able to get escape sequences from Python?
The problem is that the Python interpreter doesn't enable the processing of ANSI escape sequences. The ANSI sequences work from the Windows command prompt because cmd
does enable them. If you start Python from the command prompt you'll find the ANSI sequences do work, including the ones for enabling and disabling the cursor. That's because cmd
has already enabled them for that console window.
If you want have something you can click on to start the Python interpreter with ANSI escapes enabled you can create a shortcut that runs a command something like cmd /c C:\PythonXY\python
.
Another, harder, solution would be to use ctypes to enable ANSI escape sequence processing for the console window by calling the SetConsoleMode
Windows API with the ENABLE_VIRTUAL_TERMINAL_PROCESSING
flag set. For example:
import ctypes
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)