6

After Win10 conhost got VT100 support, I played around with it a bit. Eventually I found out os.system('') activated the support in Python and got the following result:

import os
print('\033[36mTest\033[0m')
os.system('')
print('\033[36mTest\033[0m')

Output

[36mTest[0m
Test

(Second Test was printed in blue.)

I have read here that Python does not request that Windows enables VT100, so why does os.system activate it anyways?

martineau
  • 119,623
  • 25
  • 170
  • 301
Grub4K
  • 101
  • 6
  • 2
    Because `os.system()` invokes `cmd.exe` to run the specified command. `cmd.exe` must do the enabling. – kindall Jun 28 '18 at 21:54
  • @kindall If `cmd.exe` does the enabeling, why doesn't it activate it in the first place? if you for example call `python` from `cmd.exe`, it still doesnt work. Also why does the subprocess influence the parent Python one? It shouldnt, right? – Grub4K Jun 28 '18 at 22:05
  • I assume that the VT100 meulation is a property of the console window, not any particular process. As to the internals, I don't know specifically how the VT100 mode gets activated but it is pretty clear that it must be being done by `cmd.exe` since it's the only other process involved besides Python, and Python sure isn't doing it. – kindall Jun 28 '18 at 22:22
  • 2
    Virtual terminal mode of the console (hosted by conhost.exe, not cmd.exe) is enabled for a screen-buffer via `GetConsoleMode(hFile, &mode);` `SetConsoleMode(hFile, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)`. cmd.exe enables it for itself at startup and leaves it enabled at exit, which explains the behavior with running `cmd /c`. OTOH, it restores the previous console mode before running an external program such as python.exe. You can enable VT mode by default for the current user by setting a DWORD value of 2 named "VirtualTerminalLevel" in "HKCU\Console". – Eryk Sun Jun 28 '18 at 23:10
  • @eryksun: Is there a way for a Python script to enable it for itself—perhaps by modifying the registry itself? – martineau Jun 28 '18 at 23:33
  • @martineau, of course the mode can be enabled at runtime via ctypes or PyWin32, if that's what the OP wants. – Eryk Sun Jun 29 '18 at 02:23
  • Looks like adding a rgb sequence with bold style does not work using this way. Is there any library that enables vt100 sequences for using rgb values? (I am trying to get pygments to print truecolor output) – AmaanK Apr 22 '21 at 14:36

0 Answers0