Skip to content

Commit 18587ab

Browse files
Starwarsfan2099kmvanbrunt
authored andcommitted
Simplify sys.stdout check
1 parent 950d92a commit 18587ab

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

cmd2/rl_utils.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -70,43 +70,42 @@ class RlType(Enum):
7070
)
7171

7272
# Check if we are running in a terminal
73-
if sys.stdout:
74-
if sys.stdout.isatty(): # pragma: no cover
75-
# noinspection PyPep8Naming,PyUnresolvedReferences
76-
def enable_win_vt100(handle: HANDLE) -> bool:
77-
"""
78-
Enables VT100 character sequences in a Windows console
79-
This only works on Windows 10 and up
80-
:param handle: the handle on which to enable vt100
81-
:return: True if vt100 characters are enabled for the handle
82-
"""
83-
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
84-
85-
# Get the current mode for this handle in the console
86-
cur_mode = DWORD(0)
87-
readline.rl.console.GetConsoleMode(handle, byref(cur_mode))
88-
89-
retVal = False
90-
91-
# Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled
92-
if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0:
93-
retVal = True
94-
95-
elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING):
96-
# Restore the original mode when we exit
97-
atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode)
98-
retVal = True
99-
100-
return retVal
101-
102-
# Enable VT100 sequences for stdout and stderr
103-
STD_OUT_HANDLE = -11
104-
STD_ERROR_HANDLE = -12
105-
# noinspection PyUnresolvedReferences
106-
vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE))
107-
# noinspection PyUnresolvedReferences
108-
vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE))
109-
vt100_support = vt100_stdout_support and vt100_stderr_support
73+
if sys.stdout and sys.stdout.isatty(): # pragma: no cover
74+
# noinspection PyPep8Naming,PyUnresolvedReferences
75+
def enable_win_vt100(handle: HANDLE) -> bool:
76+
"""
77+
Enables VT100 character sequences in a Windows console
78+
This only works on Windows 10 and up
79+
:param handle: the handle on which to enable vt100
80+
:return: True if vt100 characters are enabled for the handle
81+
"""
82+
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
83+
84+
# Get the current mode for this handle in the console
85+
cur_mode = DWORD(0)
86+
readline.rl.console.GetConsoleMode(handle, byref(cur_mode))
87+
88+
retVal = False
89+
90+
# Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled
91+
if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0:
92+
retVal = True
93+
94+
elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING):
95+
# Restore the original mode when we exit
96+
atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode)
97+
retVal = True
98+
99+
return retVal
100+
101+
# Enable VT100 sequences for stdout and stderr
102+
STD_OUT_HANDLE = -11
103+
STD_ERROR_HANDLE = -12
104+
# noinspection PyUnresolvedReferences
105+
vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE))
106+
# noinspection PyUnresolvedReferences
107+
vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE))
108+
vt100_support = vt100_stdout_support and vt100_stderr_support
110109

111110
############################################################################################################
112111
# pyreadline3 is incomplete in terms of the Python readline API. Add the missing functions we need.

0 commit comments

Comments
 (0)