From 0888a083ec6a23a75d3e681f08ef242acd862573 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 13 Apr 2021 20:22:01 -0400 Subject: help: switch from formatter module to textwrap Since Python has deprecated the formatter module, switch to the textwrap module instead for reflowing text. We weren't really using any other feature anyways. Verified by diffing the output before & after the change and making sure it was the same. Then made a few tweaks to tighten up the output. Change-Id: I0be1bc2a6661a311b1a4693c80d0f8366320ba55 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/303282 Reviewed-by: Raman Tenneti Tested-by: Mike Frysinger --- subcmds/help.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'subcmds/help.py') diff --git a/subcmds/help.py b/subcmds/help.py index 9ba9e706..6a767e6f 100644 --- a/subcmds/help.py +++ b/subcmds/help.py @@ -14,7 +14,7 @@ import re import sys -from formatter import AbstractFormatter, DumbWriter +import textwrap from subcmds import all_commands from color import Coloring @@ -84,8 +84,7 @@ Displays detailed usage information about a command. def __init__(self, gc): Coloring.__init__(self, gc, 'help') self.heading = self.printer('heading', attr='bold') - - self.wrap = AbstractFormatter(DumbWriter()) + self._first = True def _PrintSection(self, heading, bodyAttr): try: @@ -95,7 +94,9 @@ Displays detailed usage information about a command. if body == '' or body is None: return - self.nl() + if not self._first: + self.nl() + self._first = False self.heading('%s%s', header_prefix, heading) self.nl() @@ -105,7 +106,8 @@ Displays detailed usage information about a command. body = body.strip() body = body.replace('%prog', me) - asciidoc_hdr = re.compile(r'^\n?#+ (.+)$') + # Extract the title, but skip any trailing {#anchors}. + asciidoc_hdr = re.compile(r'^\n?#+ ([^{]+)(\{#.+\})?$') for para in body.split("\n\n"): if para.startswith(' '): self.write('%s', para) @@ -120,9 +122,12 @@ Displays detailed usage information about a command. self.nl() continue - self.wrap.add_flowing_data(para) - self.wrap.end_paragraph(1) - self.wrap.end_paragraph(0) + lines = textwrap.wrap(para.replace(' ', ' '), width=80, + break_long_words=False, break_on_hyphens=False) + for line in lines: + self.write('%s', line) + self.nl() + self.nl() out = _Out(self.client.globalConfig) out._PrintSection('Summary', 'helpSummary') -- cgit v1.2.3-54-g00ecf