2

Enabling terminal sequences on Windows 10+ using Python works like so:

>>> print("Hello world" + "\x1b\x5b2F")  # This doesn't do much
>>> import subprocess
>>> subprocess.run("", shell=True)  # Somehow enables terminal sequences (dark magic)
>>> print("Hello world" + "\x1b\x5b2F")  # Now this goes up in console

What kind of sorcery is this?

It just opens an empty process which closes immediately after. What does that have to do with reading terminal sequences? How does it work?

Bharel
  • 23,672
  • 5
  • 40
  • 80
  • I don't know much, but the answer is likely going to be "this is implementation-independent and don't rely on it." – David Aug 13 '22 at 09:04
  • 1
    @David Yeah but how? Why? What's going on here? How did my life choices bring me to this moment? – Bharel Aug 13 '22 at 09:11
  • 1
    Well... I can't exactly answer because I only found out about the windows terminal sequences half an hour ago and I don't usually do development with Windows; my best answer would be "because somehow CPython feels like it and does it." – David Aug 13 '22 at 09:16
  • I'm thinking maybe because it opens a shell process (basically an empty console in the local console), it launches `cmd` in the local console which updates the window to a virtual terminal – Bharel Aug 13 '22 at 09:22
  • Possibly, but I'm no Windows expert and I have no idea why/how. – David Aug 13 '22 at 09:30

0 Answers0