terminalwriter: move Win32ConsoleWriter definition under win32 conditional

This way non-Windows platforms skip it. It also uses things defined
inside the `if`.
This commit is contained in:
Ran Benita 2020-04-29 16:17:44 +03:00
parent 5e2d820308
commit 1d596b27a7
1 changed files with 33 additions and 34 deletions

View File

@ -300,6 +300,25 @@ class TerminalWriter:
self.write(" " * diff2last)
class WriteFile:
def __init__(self, writemethod, encoding=None):
self.encoding = encoding
self._writemethod = writemethod
def write(self, data):
if self.encoding:
data = data.encode(self.encoding, "replace")
self._writemethod(data)
def flush(self):
return
if win32_and_ctypes:
import ctypes # noqa: F811
from ctypes import wintypes
from ctypes import windll # type: ignore[attr-defined] # noqa: F821
class Win32ConsoleWriter(TerminalWriter):
def write(self, msg, **kw):
if msg:
@ -333,26 +352,6 @@ class Win32ConsoleWriter(TerminalWriter):
if oldcolors:
SetConsoleTextAttribute(handle, oldcolors)
class WriteFile:
def __init__(self, writemethod, encoding=None):
self.encoding = encoding
self._writemethod = writemethod
def write(self, data):
if self.encoding:
data = data.encode(self.encoding, "replace")
self._writemethod(data)
def flush(self):
return
if win32_and_ctypes:
import ctypes # noqa: F811
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