From ce5d02724049faa346d19a03839dab91f5e7229f Mon Sep 17 00:00:00 2001 From: Avram Lubkin Date: Wed, 31 Jul 2024 18:09:49 -0400 Subject: [PATCH] Only set VT mode when not already set --- jinxed/win32.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/jinxed/win32.py b/jinxed/win32.py index 7d403f3..16cde76 100644 --- a/jinxed/win32.py +++ b/jinxed/win32.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2019 - 2023 Avram Lubkin, All Rights Reserved +# Copyright 2019 - 2024 Avram Lubkin, All Rights Reserved # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this @@ -258,11 +258,11 @@ def get_terminal_size(fd): # pylint: disable=invalid-name return TerminalSize(window.Right - window.Left + 1, window.Bottom - window.Top + 1) -def flush_and_set_console(fd, mode): # pylint: disable=invalid-name +def flush_and_set_console(fd, mode=None): # pylint: disable=invalid-name """ Args: filehandle(int): Windows filehandle object as returned by :py:func:`msvcrt.get_osfhandle` - mode(int): Desired console mode + mode(int): Desired console mode. If None, mode is not changed Attempts to set console to specified mode, but will not raise on failure @@ -276,11 +276,13 @@ def flush_and_set_console(fd, mode): # pylint: disable=invalid-name except (AttributeError, TypeError, io.UnsupportedOperation): pass - try: - filehandle = msvcrt.get_osfhandle(fd) - set_console_mode(filehandle, mode) - except OSError: - pass + if mode is not None: + + try: + filehandle = msvcrt.get_osfhandle(fd) + set_console_mode(filehandle, mode) + except OSError: + pass def get_term(fd, fallback=True): # pylint: disable=invalid-name @@ -326,9 +328,16 @@ def get_term(fd, fallback=True): # pylint: disable=invalid-name except OSError: term = 'unknown' else: - atexit.register(flush_and_set_console, fd, mode) - # pylint: disable=unsupported-binary-operation - set_console_mode(filehandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) + # Proceed if VT mode is already enabled + if mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING: + atexit.register(flush_and_set_console, fd, None) + + # Otherwise, enable VT mode + else: + atexit.register(flush_and_set_console, fd, mode) + # pylint: disable=unsupported-binary-operation + set_console_mode(filehandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) + term = 'vtwin10' # Currently falling back to Ansicon for older versions of Windows