diff options
Diffstat (limited to '')
-rw-r--r-- | terminaltables3/terminal_io.py (renamed from terminaltables/terminal_io.py) | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/terminaltables/terminal_io.py b/terminaltables3/terminal_io.py index 8b8c10d..dc04431 100644 --- a/terminaltables/terminal_io.py +++ b/terminaltables3/terminal_io.py @@ -3,16 +3,17 @@ import ctypes import struct import sys +from typing import Tuple, Union DEFAULT_HEIGHT = 24 DEFAULT_WIDTH = 79 INVALID_HANDLE_VALUE = -1 -IS_WINDOWS = sys.platform == 'win32' +IS_WINDOWS = sys.platform == "win32" STD_ERROR_HANDLE = -12 STD_OUTPUT_HANDLE = -11 -def get_console_info(kernel32, handle): +def get_console_info(kernel32, handle: int) -> Tuple[int, int]: """Get information about this current console window (Windows only). https://github.com/Robpol86/colorclass/blob/ab42da59/colorclass/windows.py#L111 @@ -26,7 +27,7 @@ def get_console_info(kernel32, handle): :rtype: tuple """ if handle == INVALID_HANDLE_VALUE: - raise OSError('Invalid handle.') + raise OSError("Invalid handle.") # Query Win32 API. lpcsbi = ctypes.create_string_buffer(22) # Populated by GetConsoleScreenBufferInfo. @@ -34,12 +35,12 @@ def get_console_info(kernel32, handle): raise ctypes.WinError() # Subclass of OSError. # Parse data. - left, top, right, bottom = struct.unpack('hhhhHhhhhhh', lpcsbi.raw)[5:-2] + left, top, right, bottom = struct.unpack("hhhhHhhhhhh", lpcsbi.raw)[5:-2] width, height = right - left, bottom - top return width, height -def terminal_size(kernel32=None): +def terminal_size(kernel32=None) -> Tuple[int, int]: """Get the width and height of the terminal. http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/ @@ -56,19 +57,23 @@ def terminal_size(kernel32=None): return get_console_info(kernel32, kernel32.GetStdHandle(STD_ERROR_HANDLE)) except OSError: try: - return get_console_info(kernel32, kernel32.GetStdHandle(STD_OUTPUT_HANDLE)) + return get_console_info( + kernel32, kernel32.GetStdHandle(STD_OUTPUT_HANDLE) + ) except OSError: return DEFAULT_WIDTH, DEFAULT_HEIGHT try: - device = __import__('fcntl').ioctl(0, __import__('termios').TIOCGWINSZ, '\0\0\0\0\0\0\0\0') - except IOError: + device = __import__("fcntl").ioctl( + 0, __import__("termios").TIOCGWINSZ, "\0\0\0\0\0\0\0\0" + ) + except OSError: return DEFAULT_WIDTH, DEFAULT_HEIGHT - height, width = struct.unpack('hhhh', device)[:2] + height, width = struct.unpack("hhhh", device)[:2] return width, height -def set_terminal_title(title, kernel32=None): +def set_terminal_title(title: Union[str, bytes], kernel32=None) -> bool: """Set the terminal title. :param title: The title to set (string, unicode, bytes accepted). @@ -78,7 +83,7 @@ def set_terminal_title(title, kernel32=None): :rtype: bool """ try: - title_bytes = title.encode('utf-8') + title_bytes = title.encode("utf-8") except AttributeError: title_bytes = title @@ -90,9 +95,8 @@ def set_terminal_title(title, kernel32=None): is_ascii = all(c < 128 for c in title) # bytes. if is_ascii: return kernel32.SetConsoleTitleA(title_bytes) != 0 - else: - return kernel32.SetConsoleTitleW(title) != 0 + return kernel32.SetConsoleTitleW(title) != 0 # Linux/OSX. - sys.stdout.write(b'\033]0;' + title_bytes + b'\007') + sys.stdout.write(b"\033]0;" + title_bytes + b"\007") return True |