io: combine _io.TerminalWriter and _io.terminalwriter.TerminalWriter
Previously it extended an external type but now it come move to the type itself.
This commit is contained in:
parent
dac05ccd9a
commit
94a57d2353
|
@ -1,39 +1,6 @@
|
|||
from typing import List
|
||||
from typing import Sequence
|
||||
|
||||
from .terminalwriter import TerminalWriter as BaseTerminalWriter
|
||||
from .terminalwriter import TerminalWriter
|
||||
|
||||
|
||||
class TerminalWriter(BaseTerminalWriter):
|
||||
def _write_source(self, lines: List[str], indents: Sequence[str] = ()) -> None:
|
||||
"""Write lines of source code possibly highlighted.
|
||||
|
||||
Keeping this private for now because the API is clunky. We should discuss how
|
||||
to evolve the terminal writer so we can have more precise color support, for example
|
||||
being able to write part of a line in one color and the rest in another, and so on.
|
||||
"""
|
||||
if indents and len(indents) != len(lines):
|
||||
raise ValueError(
|
||||
"indents size ({}) should have same size as lines ({})".format(
|
||||
len(indents), len(lines)
|
||||
)
|
||||
)
|
||||
if not indents:
|
||||
indents = [""] * len(lines)
|
||||
source = "\n".join(lines)
|
||||
new_lines = self._highlight(source).splitlines()
|
||||
for indent, new_line in zip(indents, new_lines):
|
||||
self.line(indent + new_line)
|
||||
|
||||
def _highlight(self, source):
|
||||
"""Highlight the given source code if we have markup support"""
|
||||
if not self.hasmarkup:
|
||||
return source
|
||||
try:
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.lexers.python import PythonLexer
|
||||
from pygments import highlight
|
||||
except ImportError:
|
||||
return source
|
||||
else:
|
||||
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
|
||||
__all__ = [
|
||||
"TerminalWriter",
|
||||
]
|
||||
|
|
|
@ -5,6 +5,7 @@ import sys
|
|||
import unicodedata
|
||||
from functools import lru_cache
|
||||
from io import StringIO
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
|
||||
|
@ -191,6 +192,39 @@ class TerminalWriter:
|
|||
self.write(s, **kw)
|
||||
self.write("\n")
|
||||
|
||||
def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> None:
|
||||
"""Write lines of source code possibly highlighted.
|
||||
|
||||
Keeping this private for now because the API is clunky. We should discuss how
|
||||
to evolve the terminal writer so we can have more precise color support, for example
|
||||
being able to write part of a line in one color and the rest in another, and so on.
|
||||
"""
|
||||
if indents and len(indents) != len(lines):
|
||||
raise ValueError(
|
||||
"indents size ({}) should have same size as lines ({})".format(
|
||||
len(indents), len(lines)
|
||||
)
|
||||
)
|
||||
if not indents:
|
||||
indents = [""] * len(lines)
|
||||
source = "\n".join(lines)
|
||||
new_lines = self._highlight(source).splitlines()
|
||||
for indent, new_line in zip(indents, new_lines):
|
||||
self.line(indent + new_line)
|
||||
|
||||
def _highlight(self, source):
|
||||
"""Highlight the given source code if we have markup support"""
|
||||
if not self.hasmarkup:
|
||||
return source
|
||||
try:
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.lexers.python import PythonLexer
|
||||
from pygments import highlight
|
||||
except ImportError:
|
||||
return source
|
||||
else:
|
||||
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))
|
||||
|
||||
|
||||
if win32_and_ctypes:
|
||||
import ctypes # noqa: F811
|
||||
|
|
Loading…
Reference in New Issue