|
9 | 9 |
|
10 | 10 | try: |
11 | 11 | import readline # noqa: F401 |
12 | | -except ImportError: # Windows has no `readline` normally |
| 12 | +except ImportError: # readline isn't available on all platforms |
13 | 13 | pass |
14 | 14 |
|
15 | 15 | from .frames import Close |
16 | 16 | from .sync.client import ClientConnection, connect |
17 | 17 | from .version import version as websockets_version |
18 | 18 |
|
19 | 19 |
|
20 | | -if sys.platform == "win32": |
21 | | - |
22 | | - def win_enable_vt100() -> None: |
23 | | - """ |
24 | | - Enable VT-100 for console output on Windows. |
25 | | -
|
26 | | - See also https://github.com/python/cpython/issues/73245. |
27 | | -
|
28 | | - """ |
29 | | - import ctypes |
30 | | - |
31 | | - STD_OUTPUT_HANDLE = ctypes.c_uint(-11) |
32 | | - INVALID_HANDLE_VALUE = ctypes.c_uint(-1) |
33 | | - ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x004 |
34 | | - |
35 | | - handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) |
36 | | - if handle == INVALID_HANDLE_VALUE: |
37 | | - raise RuntimeError("unable to obtain stdout handle") |
38 | | - |
39 | | - cur_mode = ctypes.c_uint() |
40 | | - if ctypes.windll.kernel32.GetConsoleMode(handle, ctypes.byref(cur_mode)) == 0: |
41 | | - raise RuntimeError("unable to query current console mode") |
42 | | - |
43 | | - # ctypes ints lack support for the required bit-OR operation. |
44 | | - # Temporarily convert to Py int, do the OR and convert back. |
45 | | - py_int_mode = int.from_bytes(cur_mode, sys.byteorder) |
46 | | - new_mode = ctypes.c_uint(py_int_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
47 | | - |
48 | | - if ctypes.windll.kernel32.SetConsoleMode(handle, new_mode) == 0: |
49 | | - raise RuntimeError("unable to set console mode") |
50 | | - |
51 | | - |
52 | 20 | def print_during_input(string: str) -> None: |
53 | 21 | sys.stdout.write( |
54 | 22 | # Save cursor position |
@@ -116,17 +84,10 @@ def main() -> None: |
116 | 84 | if args.uri is None: |
117 | 85 | parser.error("the following arguments are required: <uri>") |
118 | 86 |
|
119 | | - # If we're on Windows, enable VT100 terminal support. |
120 | | - if sys.platform == "win32": |
121 | | - try: |
122 | | - win_enable_vt100() |
123 | | - except RuntimeError as exc: |
124 | | - sys.stderr.write( |
125 | | - f"Unable to set terminal to VT100 mode. This is only " |
126 | | - f"supported since Win10 anniversary update. Expect " |
127 | | - f"weird symbols on the terminal.\nError: {exc}\n" |
128 | | - ) |
129 | | - sys.stderr.flush() |
| 87 | + # Enable VT100 to support ANSI escape codes in Command Prompt on Windows. |
| 88 | + # See https://github.com/python/cpython/issues/74261 for why this works. |
| 89 | + if sys.platform == "win32": # pragma: no cover |
| 90 | + os.system("") |
130 | 91 |
|
131 | 92 | try: |
132 | 93 | websocket = connect(args.uri) |
|
0 commit comments