Skip to content

Print ppaged() output when the pager program cannot be run - #1813

Merged
tleonhardt merged 8 commits into
mainfrom
fix-ppaged-missing-pager
Nov 17, 2026
Merged

tleonhardt merged 8 commits into
mainfrom
fix-ppaged-missing-pager

Conversation

@tleonhardt

Copy link
Copy Markdown
Member

Problem

ppaged() hands its output to the shell command in self.pager or self.pager_chop and 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-slim style container, which ships more but no less, any command that pages shows only this:

/bin/sh: 1: less: not found

Fix

After the pager's shell exits, print the output if the shell could not run the pager at all:

  • POSIX: the shell exited with 127 (command not found) or 126 (found but not executable).
  • Windows: cmd.exe exits 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 with shutil.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

  • Two tests run a real shell on every platform: one with a pager program that does not exist (the output must appear), one with a pager that reads its input and exits with 1 (the output must not be repeated). These are what check the cmd.exe behavior on the Windows CI runners; I have not run them on a Windows machine myself.
  • A parametrized test covers the decision for both platforms' status codes, a quoted Windows path, and a command that cannot be split.

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 less is unchanged.

Notes for the reviewer

  • The Windows rule looks at the first word of the pager command. A command that starts with something other than the program (unusual for a pager) and exits nonzero would be treated as "could not start" only if that first word is also not a program.
  • Found while comparing pagers for Bottom bar should persist during command execution #1743. It does not depend on any of the toolbar branches.

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

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (ff372de) to head (66edab0).

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           
Flag Coverage Δ
unittests 99.75% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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.
@tleonhardt
tleonhardt merged commit b3579d1 into main Nov 17, 2026
32 checks passed
@tleonhardt
tleonhardt deleted the fix-ppaged-missing-pager branch November 17, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant