[PATCH] Format the help text to properly fit to the terminal window

Martin Kolman mkolman at redhat.com
Fri May 16 15:50:48 UTC 2014


Provided it is possible to detect the terminal window size.

Signed-off-by: Martin Kolman <mkolman at redhat.com>
---
 pyanaconda/anaconda_argparse.py | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/pyanaconda/anaconda_argparse.py b/pyanaconda/anaconda_argparse.py
index 090261d..ac04a35 100644
--- a/pyanaconda/anaconda_argparse.py
+++ b/pyanaconda/anaconda_argparse.py
@@ -26,13 +26,34 @@ DESCRIPTION = "Anaconda is the installation program used by Fedora," \
 import itertools
 import os
 
-from argparse import ArgumentParser, ArgumentError
+from argparse import ArgumentParser, ArgumentError, HelpFormatter
 
 from pyanaconda.flags import BootArgs
 
 import logging
 log = logging.getLogger("anaconda")
 
+# Help text formatting constants
+
+LEFT_PADDING = 8  # the help text will start after 8 spaces
+RIGHT_PADDING = 8  # there will be 8 spaces left on the right
+HELP_WIDTH = 80
+
+# try to detect the terminal window size
+try:
+    columns = int(os.popen('stty size', 'r').read().split()[1])
+    # apply the right padding
+    columns = columns - RIGHT_PADDING
+    if columns > 0:
+        HELP_WIDTH = columns
+# pylint: disable=broad-except
+except Exception as e:
+    # detection failed, use the default
+    # NOTE: quite a lot can go wrong with the terminal window size detection
+    # so we try to make sure it doesn't bring down the rest of Anaconda
+    print("anaconda argparse: terminal size detection failed, using default width")
+    print(e)
+
 class AnacondaArgumentParser(ArgumentParser):
     """
     Subclass of ArgumentParser that also examines boot arguments.
@@ -51,7 +72,10 @@ class AnacondaArgumentParser(ArgumentParser):
         self.deprecated_bootargs = []
         self.bootarg_prefix = kwargs.pop("bootarg_prefix", "")
         self.require_prefix = kwargs.pop("require_prefix", True)
-        ArgumentParser.__init__(self, description=DESCRIPTION, *args, **kwargs)
+        ArgumentParser.__init__(self, description=DESCRIPTION,
+                                formatter_class=lambda prog: HelpFormatter(
+                                    prog, max_help_position=LEFT_PADDING, width=HELP_WIDTH),
+                                *args, **kwargs)
 
     def add_argument(self, *args, **kwargs):
         """
-- 
1.9.0



More information about the anaconda-patches mailing list