Skip to content

Commit

Permalink
Only set VT mode when not already set
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Jul 31, 2024
1 parent d044bdf commit ce5d027
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions jinxed/win32.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce5d027

Please # to comment.