-1

I am writing a console-based program in Python.

I saw how to change colours in a terminal.

After seeing a SO question, I applied it to my program:
print('\033[91m' + "Empty command not accepted." + '\033[0m'

Which gives the following in cmd.exe:
←[91mEmpty command not accepted.←[0m

But running the same in Windows Terminal which came pre-installed on Windows 11 (also available in Microsoft Store) produced the desired output, i.e., text printed in red colour.

After researching for a while, I saw that using colorama, the problem could be fixed.

But I quite liked the console GUI of Windows Terminal, so I was wondering if I could somehow pack/bundle it up with my program, so that users who doesn't have Windows Terminal installed on their PCs can use the program.

Is there a way for it?
Sorry, if it's a silly question.
Thanks.

  • Did you try _fatuous_ [`os.system('')`](https://stackoverflow.com/a/39675059/3439404) or [```subprocess.call('', shell=True)```](https://bugs.python.org/issue30075)? – JosefZ Feb 12 '22 at 19:04
  • @JosefZ Thank you! That solved it! Thank you everyone who have responded. –  Feb 13 '22 at 11:16
  • @JosefZ `os.system('')` worked for me. – NameError Feb 13 '22 at 11:17

1 Answers1

0

In order to enable ANSI "VT sequence" support in Windows console applications, you're expected to call SetConsoleMode with ENABLE_VT_PROCESSING. The Terminal helps hold your hand a little bit and enables that by default, whereas the vintage console won't.

I've got a helper function I use for python for enabling this.

def enable_vt_support():
    if os.name == 'nt':
        import ctypes
        hOut = ctypes.windll.kernel32.GetStdHandle(-11)
        out_modes = ctypes.c_uint32()
        ENABLE_VT_PROCESSING = ctypes.c_uint32(0x0004)
        ctypes.windll.kernel32.GetConsoleMode(hOut, ctypes.byref(out_modes))
        out_modes = ctypes.c_uint32(out_modes.value | 0x0004)
        ctypes.windll.kernel32.SetConsoleMode(hOut, out_modes)
zadjii
  • 529
  • 2
  • 4