From a1fa03615cbd6dbb960f7574b2f2596a11808e0b Mon Sep 17 00:00:00 2001 From: John Doty Date: Sat, 14 Sep 2024 06:24:35 -0700 Subject: [PATCH] Fix pretty display: don't re-wrap, account for gutter --- harness.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/harness.py b/harness.py index 2489aaa..d1fb589 100644 --- a/harness.py +++ b/harness.py @@ -460,6 +460,12 @@ class Harness: sys.stdout.flush() sys.stdout.buffer.flush() + def line_number_chars(self, lines: list) -> int: + if len(lines) > 0: + return int(math.log(len(lines), 10)) + 1 + else: + return 1 + def render_lines(self, cols: int): lines = [] @@ -480,17 +486,24 @@ class Harness: self.format_document(lines, self.document) case DisplayMode.PRETTY: - lines = self.pretty_document(cols) + line_number_chars = 1 + while True: + width = cols - line_number_chars - 1 + lines = self.pretty_document(width) + new_line_number_chars = self.line_number_chars(lines) + if new_line_number_chars == line_number_chars: + break + assert new_line_number_chars > line_number_chars + line_number_chars = new_line_number_chars + + return [f"{i: >{line_number_chars}} {line}" for i, line in enumerate(lines)] case _: typing.assert_never(self.mode) # Now that we know how many lines there are we can figure out how # many characters we need for the line number... - if len(lines) > 0: - line_number_chars = int(math.log(len(lines), 10)) + 1 - else: - line_number_chars = 1 + line_number_chars = self.line_number_chars(lines) # ...which lets us wrap the lines appropriately. wrapper = textwrap.TextWrapper(