简体   繁体   中英

How to pack/bundle Windows Terminal with Python program

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, ie, 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM