From 335a69bae53b57004a6b5190ca66ee48e329a8b5 Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Sun, 16 Feb 2020 19:31:53 +0100 Subject: [PATCH] Bug fix: Make sure pager is available before trying to run it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Noticed this while testing the humanfriendly command line interface on Windows (don't ask 😛) but really this applies to all platforms. --- humanfriendly/terminal.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/humanfriendly/terminal.py b/humanfriendly/terminal.py index 0a4bd2a..fd3f876 100644 --- a/humanfriendly/terminal.py +++ b/humanfriendly/terminal.py @@ -18,6 +18,7 @@ # Standard library modules. import codecs +import distutils.spawn import numbers import os import re @@ -614,13 +615,15 @@ def show_pager(formatted_text, encoding=DEFAULT_ENCODING): that's used to invoke the pager. """ if connected_to_terminal(): + # Make sure the selected pager command is available. command_line = get_pager_command(formatted_text) - pager = subprocess.Popen(command_line, stdin=subprocess.PIPE) - if is_unicode(formatted_text): - formatted_text = formatted_text.encode(encoding) - pager.communicate(input=formatted_text) - else: - output(formatted_text) + if distutils.spawn.find_executable(command_line[0]): + pager = subprocess.Popen(command_line, stdin=subprocess.PIPE) + if is_unicode(formatted_text): + formatted_text = formatted_text.encode(encoding) + pager.communicate(input=formatted_text) + return + output(formatted_text) def terminal_supports_colors(stream=None):