peterbrittain / asciimatics

A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Apache License 2.0
3.64k stars 238 forks source link

Native VT support in Windows #197

Open peterbrittain opened 5 years ago

peterbrittain commented 5 years ago

According to a dev blog (https://blogs.msdn.microsoft.com/commandline/2018/12/10/new-experimental-console-features/), Windows 10 natively supports ANSI escape codes sufficiently well to run most Linux curses apps inside WSL. That should mean that we can get a much better experience for Windows 10 users if we create a new Screen sub-class that simply uses the ANSI escape codes instead of the win32 console API.

A quick trial shows that ANSI escape codes inside CMD.exe do indeed work and provide at least 256 colour support. Nice!

The idea here is that we give Windows 10 users a much better experience by spotting this at the time the Screen is opened and giving an ANSI terminal instead. In theory this is almost exactly the existing _CursesScreen implementation (for colours and cursor movement), but with hard-coded ANSI escape codes.

Eeems commented 5 years ago

Be aware that before Windows 10 build 1809 you could crash the console with certain ANSI codes. Eeems/intercessions#1 tartley/colorama#139 Microsoft/console#162

peterbrittain commented 5 years ago

Well worth knowing before I invest too much time into this. Thanks.

In case you're interested, I've found that all the sample code is running fine in WSL (using a Linux Python installation). It's nice to (finally) see the animated Julia set running full colour in Windows!

Eeems commented 5 years ago

@peterbrittain a fix has been delivered in 1809, you can probably start looking into this now :)

iamthad commented 4 years ago

In case it helps, this is a minimal working example of enabling the support:

import platform
if platform.system() == 'Windows':
    from ctypes import windll
    kernel32 = windll.kernel32
    try:
        kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), kernel32.GetConsoleMode()|7)
    except Exception as e:
        raise OSError("Could not enable color handling") from e