Skip to content

Commit d8558e8

Browse files
committed
terminalwriter: clean up markup function a bit
1 parent 414a87a commit d8558e8

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/_pytest/_io/terminalwriter.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,22 @@ def width_of_current_line(self) -> int:
101101
"""Return an estimate of the width so far in the current line."""
102102
return get_line_width(self._current_line)
103103

104-
def markup(self, text: str, **kw: bool) -> str:
105-
esc = []
106-
for name in kw:
104+
def markup(self, text: str, **markup: bool) -> str:
105+
for name in markup:
107106
if name not in self._esctable:
108107
raise ValueError("unknown markup: {!r}".format(name))
109-
if kw[name]:
110-
esc.append(self._esctable[name])
111-
if esc and self.hasmarkup:
112-
text = "".join("\x1b[%sm" % cod for cod in esc) + text + "\x1b[0m"
108+
if self.hasmarkup:
109+
esc = [self._esctable[name] for name, on in markup.items() if on]
110+
if esc:
111+
text = "".join("\x1b[%sm" % cod for cod in esc) + text + "\x1b[0m"
113112
return text
114113

115114
def sep(
116115
self,
117116
sepchar: str,
118117
title: Optional[str] = None,
119118
fullwidth: Optional[int] = None,
120-
**kw: bool
119+
**markup: bool
121120
) -> None:
122121
if fullwidth is None:
123122
fullwidth = self.fullwidth
@@ -147,26 +146,24 @@ def sep(
147146
if len(line) + len(sepchar.rstrip()) <= fullwidth:
148147
line += sepchar.rstrip()
149148

150-
self.line(line, **kw)
149+
self.line(line, **markup)
151150

152-
def write(self, msg: str, *, flush: bool = False, **kw: bool) -> None:
151+
def write(self, msg: str, *, flush: bool = False, **markup: bool) -> None:
153152
if msg:
154153
current_line = msg.rsplit("\n", 1)[-1]
155154
if "\n" in msg:
156155
self._current_line = current_line
157156
else:
158157
self._current_line += current_line
159158

160-
if self.hasmarkup and kw:
161-
markupmsg = self.markup(msg, **kw)
162-
else:
163-
markupmsg = msg
164-
self._file.write(markupmsg)
159+
msg = self.markup(msg, **markup)
160+
161+
self._file.write(msg)
165162
if flush:
166163
self.flush()
167164

168-
def line(self, s: str = "", **kw: bool) -> None:
169-
self.write(s, **kw)
165+
def line(self, s: str = "", **markup: bool) -> None:
166+
self.write(s, **markup)
170167
self.write("\n")
171168

172169
def flush(self) -> None:

testing/io/test_terminalwriter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ def test_sep_longer_than_width(self, tw) -> None:
112112
assert line == "- aaaaaaaaaa -\n"
113113

114114
@pytest.mark.skipif(sys.platform == "win32", reason="win32 has no native ansi")
115-
def test_markup(self, tw) -> None:
116-
for bold in (True, False):
117-
for color in ("red", "green"):
118-
text2 = tw.markup("hello", **{color: True, "bold": bold})
119-
assert text2.find("hello") != -1
115+
@pytest.mark.parametrize("bold", (True, False))
116+
@pytest.mark.parametrize("color", ("red", "green"))
117+
def test_markup(self, tw, bold: bool, color: str) -> None:
118+
text = tw.markup("hello", **{color: True, "bold": bold})
119+
assert "hello" in text
120+
121+
def test_markup_bad(self, tw) -> None:
120122
with pytest.raises(ValueError):
121123
tw.markup("x", wronkw=3)
122124
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)