Repository navigation
Print ppaged() output when the pager program cannot be run - #1813
Merged
Merged
Conversation
ppaged() hands its output to the shell command in `pager` or `pager_chop` and prints nothing itself. Where that program is not installed the shell reports it and exits, and the output is lost: in a stock Debian slim container, which ships no `less`, a command such as `help -v` shows only "/bin/sh: 1: less: not found". Print the output when the shell could not run the pager at all. A POSIX shell says so with status 127, or 126 for a program it cannot execute. cmd.exe exits with 1, which a pager may also do for reasons of its own, so there the program is looked for as well. A pager which did run has had the output, whatever status it exits with, and nothing is printed again.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1813 +/- ##
=======================================
Coverage 99.75% 99.75%
=======================================
Files 23 23
Lines 6647 6664 +17
=======================================
+ Hits 6631 6648 +17
Misses 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
This was referenced Nov 15, 2026
ppaged() had two ways of printing without a pager: poutput() where it would not page at all, and a write of the text captured for the pager where the pager could not be started. Return early once a pager has run, and let both cases reach the one poutput() call. Chopping turns soft wrap on so that a pager can scroll sideways. That was done by overwriting the caller's soft_wrap, so it is now kept apart and used only for what the pager is given. Without a pager there is nothing to scroll, and the output is wrapped as the caller asked, as the docstring already said.
Where the pager program is not installed, every ppaged() call started a shell which failed, so the shell's error was shown before the output each time, and the output was rendered twice. Remember a pager command which the shell could not run and print without it from then on. Another command set in `pager` or `pager_chop` is still tried. The first time, the objects had been rendered for the pager and were then rendered again by poutput(). One which can only be rendered once came out empty the second time, and the output was still lost. Write the text rendered for the pager instead. Only where chopping changed the wrapping from what the caller asked for is it rendered again. The comment above the poutput() call spoke of a pager which could not be started, but that call is also where output goes when no pager is tried.
The first time a pager could not be run, the text rendered for it was written straight to stdout, so that the objects were not rendered twice. That went around poutput(), unlike every later call and the rest of cmd2, so an application which overrides poutput() or print_to() did not see it. Fall through to the one poutput() call there too. The objects are rendered a second time that once, and not again, since the pager is not retried. `pager` and `pager_chop` are separate commands and each is tried once, so the shell's error may be shown once for each. Say that in the changelog and the documentation, instead of that it is shown only once.
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ppaged()hands its output to the shell command inself.pagerorself.pager_chopand prints nothing itself. If that program is not installed, the shell reports it and exits, and the output is lost.In a stock
python:3.13-slimstyle container, which shipsmorebut noless, any command that pages shows only this:Fix
After the pager's shell exits, print the output if the shell could not run the pager at all:
cmd.exeexits with 1 for a command it cannot find, which a pager can also do for its own reasons. So there the first word of the command is also looked up withshutil.which(), and the output is printed only when the status is nonzero and no such program exists.A pager that did run has had the output, whatever status it exits with, so nothing is printed a second time. The shell's own message stays as the explanation; no extra warning is added.
Tests
cmd.exebehavior on the Windows CI runners; I have not run them on a Windows machine myself.Also driven by hand in a pseudo-terminal on macOS: with a missing pager all 200 rows are shown after the shell's error, and paging with
lessis unchanged.Notes for the reviewer