# -*- coding: utf-8 -*- # 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 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Support functions and wrappers for calls to the Windows API """
import atexit import codecs from collections import namedtuple import ctypes from ctypes import wintypes import io import msvcrt # pylint: disable=import-error import os import platform import sys
from jinxed._util import mock, IS_WINDOWS
# Workaround for auto-doc generation on Linux ifnot IS_WINDOWS:
ctypes = mock.Mock() # noqa: F811
def _check_bool(result, func, args): # pylint: disable=unused-argument """
Used as an error handler for Windows calls
Gets last error if call isnot successful """
def set_console_mode(filehandle, mode): """
Args:
filehandle(int): Windows filehandle object as returned by :py:func:`msvcrt.get_osfhandle`
mode(int): Desired console mode
Returns:
:py:class:`os.terminal_size`: Named tuple representing terminal size
Convenience function for getting terminal size
In Python 3.3 and above, this is a wrapper for :py:func:`os.get_terminal_size`. In older versions of Python, this function calls GetConsoleScreenBufferInfo_. """
# In Python 3.3+ we can let the standard library handle this if GTS_SUPPORTED: return os.get_terminal_size(fd)
def get_term(fd, fallback=True): # pylint: disable=invalid-name """
Args:
fd(int): Python file descriptor
fallback(bool): Use fallback terminal type if type can not be determined
Returns:
str: Terminal type
Attempts to determine and enable the current terminal type
The current logic is:
- If TERM is defined in the environment, the value is returned
- Else, if ANSICON is defined in the environment, ``'ansicon'`` is returned
- Else, if virtual terminal mode is natively supported,
it is enabled and ``'vtwin10'`` is returned
- Else, if ``fallback`` is ``True``, Ansicon is loaded, and ``'ansicon'`` is returned
- If no other conditions are satisfied, ``'unknown'`` is returned
This logic may change in the future as additional terminal types are added. """
# First try TERM
term = os.environ.get('TERM', None)
if term isNone:
# See if ansicon is enabled if os.environ.get('ANSICON', None):
term = 'ansicon'
# See if Windows Terminal is being used elif os.environ.get('WT_SESSION', None):
term = 'vtwin10'
# See if the version of Windows supports VTMODE elif VTMODE_SUPPORTED: try:
filehandle = msvcrt.get_osfhandle(fd)
mode = get_console_mode(filehandle) except OSError:
term = 'unknown' else: # Proceed if VT mode is already enabled if mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING:
atexit.register(flush_and_set_console, fd, None)
# Currently falling back to Ansicon for older versions of Windows elif fallback: import ansicon # pylint: disable=import-error,import-outside-toplevel
ansicon.load()
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.