terminalwriter: fix lints
This commit is contained in:
parent
3014d9a3f7
commit
5e2d820308
|
@ -1,16 +1,13 @@
|
||||||
"""Helper functions for writing to terminals and files."""
|
"""Helper functions for writing to terminals and files."""
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
import py
|
|
||||||
from py.builtin import text, bytes
|
|
||||||
|
|
||||||
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
|
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
|
||||||
|
|
||||||
py3k = sys.version_info[0] >= 3
|
|
||||||
py33 = sys.version_info >= (3, 3)
|
|
||||||
|
|
||||||
|
|
||||||
win32_and_ctypes = False
|
win32_and_ctypes = False
|
||||||
colorama = None
|
colorama = None
|
||||||
|
@ -20,35 +17,24 @@ if sys.platform == "win32":
|
||||||
except ImportError:
|
except ImportError:
|
||||||
try:
|
try:
|
||||||
import ctypes
|
import ctypes
|
||||||
|
|
||||||
win32_and_ctypes = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
win32_and_ctypes = True
|
||||||
|
|
||||||
|
|
||||||
def _getdimensions():
|
def _getdimensions():
|
||||||
if py33:
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
size = shutil.get_terminal_size()
|
size = shutil.get_terminal_size()
|
||||||
return size.lines, size.columns
|
return size.lines, size.columns
|
||||||
else:
|
|
||||||
import termios
|
|
||||||
import fcntl
|
|
||||||
import struct
|
|
||||||
|
|
||||||
call = fcntl.ioctl(1, termios.TIOCGWINSZ, "\000" * 8)
|
|
||||||
height, width = struct.unpack("hhhh", call)[:2]
|
|
||||||
return height, width
|
|
||||||
|
|
||||||
|
|
||||||
def get_terminal_width():
|
def get_terminal_width():
|
||||||
width = 0
|
width = 0
|
||||||
try:
|
try:
|
||||||
_, width = _getdimensions()
|
_, width = _getdimensions()
|
||||||
except py.builtin._sysex:
|
except (KeyboardInterrupt, SystemExit, MemoryError, GeneratorExit):
|
||||||
raise
|
raise
|
||||||
except:
|
except BaseException:
|
||||||
# pass to fallback below
|
# pass to fallback below
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -150,7 +136,7 @@ def should_do_markup(file):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TerminalWriter(object):
|
class TerminalWriter:
|
||||||
_esctable = dict(
|
_esctable = dict(
|
||||||
black=30,
|
black=30,
|
||||||
red=31,
|
red=31,
|
||||||
|
@ -178,12 +164,10 @@ class TerminalWriter(object):
|
||||||
def __init__(self, file=None, stringio=False, encoding=None):
|
def __init__(self, file=None, stringio=False, encoding=None):
|
||||||
if file is None:
|
if file is None:
|
||||||
if stringio:
|
if stringio:
|
||||||
self.stringio = file = py.io.TextIO()
|
self.stringio = file = StringIO()
|
||||||
else:
|
else:
|
||||||
from sys import stdout as file
|
from sys import stdout as file
|
||||||
elif py.builtin.callable(file) and not (
|
elif callable(file) and not (hasattr(file, "write") and hasattr(file, "flush")):
|
||||||
hasattr(file, "write") and hasattr(file, "flush")
|
|
||||||
):
|
|
||||||
file = WriteFile(file, encoding=encoding)
|
file = WriteFile(file, encoding=encoding)
|
||||||
if hasattr(file, "isatty") and file.isatty() and colorama:
|
if hasattr(file, "isatty") and file.isatty() and colorama:
|
||||||
file = colorama.AnsiToWin32(file).stream
|
file = colorama.AnsiToWin32(file).stream
|
||||||
|
@ -236,7 +220,7 @@ class TerminalWriter(object):
|
||||||
esc = []
|
esc = []
|
||||||
for name in kw:
|
for name in kw:
|
||||||
if name not in self._esctable:
|
if name not in self._esctable:
|
||||||
raise ValueError("unknown markup: %r" % (name,))
|
raise ValueError("unknown markup: {!r}".format(name))
|
||||||
if kw[name]:
|
if kw[name]:
|
||||||
esc.append(self._esctable[name])
|
esc.append(self._esctable[name])
|
||||||
return self._escaped(text, tuple(esc))
|
return self._escaped(text, tuple(esc))
|
||||||
|
@ -259,7 +243,7 @@ class TerminalWriter(object):
|
||||||
# N <= (fullwidth - len(title) - 2) // (2*len(sepchar))
|
# N <= (fullwidth - len(title) - 2) // (2*len(sepchar))
|
||||||
N = max((fullwidth - len(title) - 2) // (2 * len(sepchar)), 1)
|
N = max((fullwidth - len(title) - 2) // (2 * len(sepchar)), 1)
|
||||||
fill = sepchar * N
|
fill = sepchar * N
|
||||||
line = "%s %s %s" % (fill, title, fill)
|
line = "{} {} {}".format(fill, title, fill)
|
||||||
else:
|
else:
|
||||||
# we want len(sepchar)*N <= fullwidth
|
# we want len(sepchar)*N <= fullwidth
|
||||||
# i.e. N <= fullwidth // len(sepchar)
|
# i.e. N <= fullwidth // len(sepchar)
|
||||||
|
@ -274,8 +258,8 @@ class TerminalWriter(object):
|
||||||
|
|
||||||
def write(self, msg, **kw):
|
def write(self, msg, **kw):
|
||||||
if msg:
|
if msg:
|
||||||
if not isinstance(msg, (bytes, text)):
|
if not isinstance(msg, (bytes, str)):
|
||||||
msg = text(msg)
|
msg = str(msg)
|
||||||
|
|
||||||
self._update_chars_on_current_line(msg)
|
self._update_chars_on_current_line(msg)
|
||||||
|
|
||||||
|
@ -319,8 +303,8 @@ class TerminalWriter(object):
|
||||||
class Win32ConsoleWriter(TerminalWriter):
|
class Win32ConsoleWriter(TerminalWriter):
|
||||||
def write(self, msg, **kw):
|
def write(self, msg, **kw):
|
||||||
if msg:
|
if msg:
|
||||||
if not isinstance(msg, (bytes, text)):
|
if not isinstance(msg, (bytes, str)):
|
||||||
msg = text(msg)
|
msg = str(msg)
|
||||||
|
|
||||||
self._update_chars_on_current_line(msg)
|
self._update_chars_on_current_line(msg)
|
||||||
|
|
||||||
|
@ -350,7 +334,7 @@ class Win32ConsoleWriter(TerminalWriter):
|
||||||
SetConsoleTextAttribute(handle, oldcolors)
|
SetConsoleTextAttribute(handle, oldcolors)
|
||||||
|
|
||||||
|
|
||||||
class WriteFile(object):
|
class WriteFile:
|
||||||
def __init__(self, writemethod, encoding=None):
|
def __init__(self, writemethod, encoding=None):
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
self._writemethod = writemethod
|
self._writemethod = writemethod
|
||||||
|
@ -365,9 +349,11 @@ class WriteFile(object):
|
||||||
|
|
||||||
|
|
||||||
if win32_and_ctypes:
|
if win32_and_ctypes:
|
||||||
TerminalWriter = Win32ConsoleWriter
|
import ctypes # noqa: F811
|
||||||
import ctypes
|
|
||||||
from ctypes import wintypes
|
from ctypes import wintypes
|
||||||
|
from ctypes import windll # type: ignore[attr-defined] # noqa: F821
|
||||||
|
|
||||||
|
TerminalWriter = Win32ConsoleWriter # type: ignore[misc] # noqa: F821
|
||||||
|
|
||||||
# ctypes access to the Windows console
|
# ctypes access to the Windows console
|
||||||
STD_OUTPUT_HANDLE = -11
|
STD_OUTPUT_HANDLE = -11
|
||||||
|
@ -407,18 +393,18 @@ if win32_and_ctypes:
|
||||||
("dwMaximumWindowSize", COORD),
|
("dwMaximumWindowSize", COORD),
|
||||||
]
|
]
|
||||||
|
|
||||||
_GetStdHandle = ctypes.windll.kernel32.GetStdHandle
|
_GetStdHandle = windll.kernel32.GetStdHandle
|
||||||
_GetStdHandle.argtypes = [wintypes.DWORD]
|
_GetStdHandle.argtypes = [wintypes.DWORD]
|
||||||
_GetStdHandle.restype = wintypes.HANDLE
|
_GetStdHandle.restype = wintypes.HANDLE
|
||||||
|
|
||||||
def GetStdHandle(kind):
|
def GetStdHandle(kind):
|
||||||
return _GetStdHandle(kind)
|
return _GetStdHandle(kind)
|
||||||
|
|
||||||
SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
|
SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
|
||||||
SetConsoleTextAttribute.argtypes = [wintypes.HANDLE, wintypes.WORD]
|
SetConsoleTextAttribute.argtypes = [wintypes.HANDLE, wintypes.WORD]
|
||||||
SetConsoleTextAttribute.restype = wintypes.BOOL
|
SetConsoleTextAttribute.restype = wintypes.BOOL
|
||||||
|
|
||||||
_GetConsoleScreenBufferInfo = ctypes.windll.kernel32.GetConsoleScreenBufferInfo
|
_GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
|
||||||
_GetConsoleScreenBufferInfo.argtypes = [
|
_GetConsoleScreenBufferInfo.argtypes = [
|
||||||
wintypes.HANDLE,
|
wintypes.HANDLE,
|
||||||
ctypes.POINTER(CONSOLE_SCREEN_BUFFER_INFO),
|
ctypes.POINTER(CONSOLE_SCREEN_BUFFER_INFO),
|
||||||
|
@ -430,7 +416,7 @@ if win32_and_ctypes:
|
||||||
_GetConsoleScreenBufferInfo(handle, ctypes.byref(info))
|
_GetConsoleScreenBufferInfo(handle, ctypes.byref(info))
|
||||||
return info
|
return info
|
||||||
|
|
||||||
def _getdimensions():
|
def _getdimensions(): # noqa: F811
|
||||||
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||||
info = GetConsoleInfo(handle)
|
info = GetConsoleInfo(handle)
|
||||||
# Substract one from the width, otherwise the cursor wraps
|
# Substract one from the width, otherwise the cursor wraps
|
||||||
|
|
Loading…
Reference in New Issue