summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Clark IV <starwarsfan2099@gmail.com>2023-04-14 09:25:17 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2023-05-02 15:06:55 -0400
commit18587ab263fa89509c040ea3bed49656eca926b2 (patch)
treef926ba01b5a881037282fdff968e5112524121eb
parent950d92aa853eb03b284b76c06383cfeb548fce72 (diff)
downloadcmd2-git-18587ab263fa89509c040ea3bed49656eca926b2.tar.gz
Simplify sys.stdout check
-rw-r--r--cmd2/rl_utils.py73
1 files changed, 36 insertions, 37 deletions
diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py
index 9a77d7a0..8c697715 100644
--- a/cmd2/rl_utils.py
+++ b/cmd2/rl_utils.py
@@ -70,43 +70,42 @@ if 'pyreadline3' in sys.modules:
)
# Check if we are running in a terminal
- if sys.stdout:
- if sys.stdout.isatty(): # pragma: no cover
- # noinspection PyPep8Naming,PyUnresolvedReferences
- def enable_win_vt100(handle: HANDLE) -> bool:
- """
- Enables VT100 character sequences in a Windows console
- This only works on Windows 10 and up
- :param handle: the handle on which to enable vt100
- :return: True if vt100 characters are enabled for the handle
- """
- ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
-
- # Get the current mode for this handle in the console
- cur_mode = DWORD(0)
- readline.rl.console.GetConsoleMode(handle, byref(cur_mode))
-
- retVal = False
-
- # Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled
- if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0:
- retVal = True
-
- elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING):
- # Restore the original mode when we exit
- atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode)
- retVal = True
-
- return retVal
-
- # Enable VT100 sequences for stdout and stderr
- STD_OUT_HANDLE = -11
- STD_ERROR_HANDLE = -12
- # noinspection PyUnresolvedReferences
- vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE))
- # noinspection PyUnresolvedReferences
- vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE))
- vt100_support = vt100_stdout_support and vt100_stderr_support
+ if sys.stdout and sys.stdout.isatty(): # pragma: no cover
+ # noinspection PyPep8Naming,PyUnresolvedReferences
+ def enable_win_vt100(handle: HANDLE) -> bool:
+ """
+ Enables VT100 character sequences in a Windows console
+ This only works on Windows 10 and up
+ :param handle: the handle on which to enable vt100
+ :return: True if vt100 characters are enabled for the handle
+ """
+ ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
+
+ # Get the current mode for this handle in the console
+ cur_mode = DWORD(0)
+ readline.rl.console.GetConsoleMode(handle, byref(cur_mode))
+
+ retVal = False
+
+ # Check if ENABLE_VIRTUAL_TERMINAL_PROCESSING is already enabled
+ if (cur_mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0:
+ retVal = True
+
+ elif readline.rl.console.SetConsoleMode(handle, cur_mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING):
+ # Restore the original mode when we exit
+ atexit.register(readline.rl.console.SetConsoleMode, handle, cur_mode)
+ retVal = True
+
+ return retVal
+
+ # Enable VT100 sequences for stdout and stderr
+ STD_OUT_HANDLE = -11
+ STD_ERROR_HANDLE = -12
+ # noinspection PyUnresolvedReferences
+ vt100_stdout_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_OUT_HANDLE))
+ # noinspection PyUnresolvedReferences
+ vt100_stderr_support = enable_win_vt100(readline.rl.console.GetStdHandle(STD_ERROR_HANDLE))
+ vt100_support = vt100_stdout_support and vt100_stderr_support
############################################################################################################
# pyreadline3 is incomplete in terms of the Python readline API. Add the missing functions we need.