From a7ee414c67c50a7f6b2711c66f30a3de111eb9c1 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 15 Jul 2022 11:18:02 -0700 Subject: [PATCH] add pager functionality to parser documentation --- jc/cli.py | 8 +++++--- jc/utils.py | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index ac83863e..19f52b31 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -225,9 +225,11 @@ def help_doc(options): f'Compatibility: {compatible}\n\n'\ f'Version {version} by {author} ({author_email})\n' - return doc_text + utils._safe_pager(doc_text) + return - return helptext() + utils._safe_print(helptext()) + return def versiontext(): @@ -504,7 +506,7 @@ def main(): sys.exit(0) if help_me: - utils._safe_print(help_doc(sys.argv)) + help_doc(sys.argv) sys.exit(0) if version_info: diff --git a/jc/utils.py b/jc/utils.py index 2bde89ee..f7f195b1 100644 --- a/jc/utils.py +++ b/jc/utils.py @@ -29,6 +29,15 @@ def _safe_print(string: str, sep=' ', end='\n', file=sys.stdout, flush=False) -> print(_asciify(string), sep=sep, end=end, file=file, flush=flush) +def _safe_pager(string: str) -> None: + """Pager output for both UTF-8 and ASCII encoding systems""" + from pydoc import pager + try: + pager(string) + except UnicodeEncodeError: + pager(_asciify(string)) + + def warning_message(message_lines: List[str]) -> None: """ Prints warning message to `STDERR` for non-fatal issues. The first line