terminalwriter: clean up markup function a bit
This commit is contained in:
parent
414a87a53f
commit
d8558e87c5
|
@ -101,15 +101,14 @@ class TerminalWriter:
|
||||||
"""Return an estimate of the width so far in the current line."""
|
"""Return an estimate of the width so far in the current line."""
|
||||||
return get_line_width(self._current_line)
|
return get_line_width(self._current_line)
|
||||||
|
|
||||||
def markup(self, text: str, **kw: bool) -> str:
|
def markup(self, text: str, **markup: bool) -> str:
|
||||||
esc = []
|
for name in markup:
|
||||||
for name in kw:
|
|
||||||
if name not in self._esctable:
|
if name not in self._esctable:
|
||||||
raise ValueError("unknown markup: {!r}".format(name))
|
raise ValueError("unknown markup: {!r}".format(name))
|
||||||
if kw[name]:
|
if self.hasmarkup:
|
||||||
esc.append(self._esctable[name])
|
esc = [self._esctable[name] for name, on in markup.items() if on]
|
||||||
if esc and self.hasmarkup:
|
if esc:
|
||||||
text = "".join("\x1b[%sm" % cod for cod in esc) + text + "\x1b[0m"
|
text = "".join("\x1b[%sm" % cod for cod in esc) + text + "\x1b[0m"
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def sep(
|
def sep(
|
||||||
|
@ -117,7 +116,7 @@ class TerminalWriter:
|
||||||
sepchar: str,
|
sepchar: str,
|
||||||
title: Optional[str] = None,
|
title: Optional[str] = None,
|
||||||
fullwidth: Optional[int] = None,
|
fullwidth: Optional[int] = None,
|
||||||
**kw: bool
|
**markup: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
if fullwidth is None:
|
if fullwidth is None:
|
||||||
fullwidth = self.fullwidth
|
fullwidth = self.fullwidth
|
||||||
|
@ -147,9 +146,9 @@ class TerminalWriter:
|
||||||
if len(line) + len(sepchar.rstrip()) <= fullwidth:
|
if len(line) + len(sepchar.rstrip()) <= fullwidth:
|
||||||
line += sepchar.rstrip()
|
line += sepchar.rstrip()
|
||||||
|
|
||||||
self.line(line, **kw)
|
self.line(line, **markup)
|
||||||
|
|
||||||
def write(self, msg: str, *, flush: bool = False, **kw: bool) -> None:
|
def write(self, msg: str, *, flush: bool = False, **markup: bool) -> None:
|
||||||
if msg:
|
if msg:
|
||||||
current_line = msg.rsplit("\n", 1)[-1]
|
current_line = msg.rsplit("\n", 1)[-1]
|
||||||
if "\n" in msg:
|
if "\n" in msg:
|
||||||
|
@ -157,16 +156,14 @@ class TerminalWriter:
|
||||||
else:
|
else:
|
||||||
self._current_line += current_line
|
self._current_line += current_line
|
||||||
|
|
||||||
if self.hasmarkup and kw:
|
msg = self.markup(msg, **markup)
|
||||||
markupmsg = self.markup(msg, **kw)
|
|
||||||
else:
|
self._file.write(msg)
|
||||||
markupmsg = msg
|
|
||||||
self._file.write(markupmsg)
|
|
||||||
if flush:
|
if flush:
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
def line(self, s: str = "", **kw: bool) -> None:
|
def line(self, s: str = "", **markup: bool) -> None:
|
||||||
self.write(s, **kw)
|
self.write(s, **markup)
|
||||||
self.write("\n")
|
self.write("\n")
|
||||||
|
|
||||||
def flush(self) -> None:
|
def flush(self) -> None:
|
||||||
|
|
|
@ -112,11 +112,13 @@ class TestTerminalWriter:
|
||||||
assert line == "- aaaaaaaaaa -\n"
|
assert line == "- aaaaaaaaaa -\n"
|
||||||
|
|
||||||
@pytest.mark.skipif(sys.platform == "win32", reason="win32 has no native ansi")
|
@pytest.mark.skipif(sys.platform == "win32", reason="win32 has no native ansi")
|
||||||
def test_markup(self, tw) -> None:
|
@pytest.mark.parametrize("bold", (True, False))
|
||||||
for bold in (True, False):
|
@pytest.mark.parametrize("color", ("red", "green"))
|
||||||
for color in ("red", "green"):
|
def test_markup(self, tw, bold: bool, color: str) -> None:
|
||||||
text2 = tw.markup("hello", **{color: True, "bold": bold})
|
text = tw.markup("hello", **{color: True, "bold": bold})
|
||||||
assert text2.find("hello") != -1
|
assert "hello" in text
|
||||||
|
|
||||||
|
def test_markup_bad(self, tw) -> None:
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
tw.markup("x", wronkw=3)
|
tw.markup("x", wronkw=3)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
|
|
Loading…
Reference in New Issue