terminalwriter: compute width_of_current_line lazily
Currently this property is computed eagerly, which means get_line_width() is computed on everything written, but that is a slow function. Compute it lazily, so that get_line_width() only runs when needed.
This commit is contained in:
parent
dd32c72ff0
commit
d5584c7207
|
@ -83,7 +83,7 @@ class TerminalWriter:
|
||||||
assert file is not None
|
assert file is not None
|
||||||
self._file = file
|
self._file = file
|
||||||
self.hasmarkup = should_do_markup(file)
|
self.hasmarkup = should_do_markup(file)
|
||||||
self._width_of_current_line = 0
|
self._current_line = ""
|
||||||
self._terminal_width = None # type: Optional[int]
|
self._terminal_width = None # type: Optional[int]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -99,7 +99,7 @@ class TerminalWriter:
|
||||||
@property
|
@property
|
||||||
def width_of_current_line(self) -> int:
|
def width_of_current_line(self) -> int:
|
||||||
"""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 self._width_of_current_line
|
return get_line_width(self._current_line)
|
||||||
|
|
||||||
def markup(self, text: str, **kw: bool) -> str:
|
def markup(self, text: str, **kw: bool) -> str:
|
||||||
esc = []
|
esc = []
|
||||||
|
@ -153,9 +153,9 @@ class TerminalWriter:
|
||||||
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:
|
||||||
self._width_of_current_line = get_line_width(current_line)
|
self._current_line = current_line
|
||||||
else:
|
else:
|
||||||
self._width_of_current_line += get_line_width(current_line)
|
self._current_line += current_line
|
||||||
|
|
||||||
if self.hasmarkup and kw:
|
if self.hasmarkup and kw:
|
||||||
markupmsg = self.markup(msg, **kw)
|
markupmsg = self.markup(msg, **kw)
|
||||||
|
|
Loading…
Reference in New Issue