Skip to content

Add --pager flag to pipe output into a pager - #376

Merged
superbrothers merged 1 commit into
stern:masterfrom
jojinkb:345-pager
Oct 18, 2026
Merged

superbrothers merged 1 commit into
stern:masterfrom
jojinkb:345-pager

Conversation

@jojinkb

@jojinkb jojinkb commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

What this PR does

Adds an opt-in --pager flag that makes stern pipe its output into a pager command (e.g. less), as requested in #345.

stern --pager less backend

Since stern spawns the pager itself and stays attached to the terminal, colors are preserved with the default --color=auto — no more typing stern <args> --color=always | less -R. Like every other flag, it can be set permanently in the config file:

pager: less

Closes #345

Behavior details

  • Only used when stdout is a terminal (same gating as bat and gh), so redirects and scripts are unaffected even when pager is set in the config file.
  • Pager-friendly environment: the pager runs with LESS=FRX and LV=-c unless those variables are already set (same defaults as git), so plain less shows raw ANSI colors, quits if the output fits on one screen, and doesn't clear the screen on exit.
  • Quitting the pager stops stern: when the pager exits (e.g. the user presses q in less), the root context is canceled so stern stops tailing and exits cleanly instead of writing into a broken pipe. Tail.Start already treats context.Canceled as a normal stop, so no changes to the stern package were needed.
  • --no-follow keeps the pager open: once all logs have been shown, stern closes the pipe (EOF) and waits for the pager, so you can keep browsing until you quit.
  • The command is split with github.com/kballard/go-shellquote (promoted from an indirect to a direct dependency; go.sum unchanged), so --pager 'less -S' works.
  • The pager is started after the interactive --prompt handler so the two don't conflict, and stderr (the +/- tail markers and errors) intentionally bypasses the pager, matching stern | less semantics.
  • The flag is strictly opt-in: the default (empty) leaves current behavior untouched. If you'd prefer a $PAGER environment fallback or a bat-style --paging=auto|always|never mode, I'm happy to extend it — kept the first iteration minimal on purpose.

How to validate

Unit tests cover the pager lifecycle (output piped through, context canceled on pager exit, error cases, LESS/LV environment handling, tty detection):

go test ./cmd/...

Interactively, with any cluster:

# colors preserved inside less; press q and stern exits
stern --pager less <query>

# stern waits for you to quit the pager after showing all logs
stern --no-follow --pager less <query>

Without a cluster (pager engages only on a terminal; --stdin mode uses the same output path):

printf 'l1\nl2\n' | stern --stdin --only-log-lines --pager 'less'

README.md was regenerated with make update-readme, and a "Using a pager" section documents the feature.

@tksm tksm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 🎉 Love the pager-friendly environment defaults. Please rebase after #375 is merged.

Piping stern into a pager in the shell loses colors unless you type
`stern <args> --color=always | less -R` every time. The new --pager flag
makes stern spawn the pager itself, so colors are preserved with the
default --color=auto because stern remains attached to the terminal.

The pager is used only when the standard output is a terminal, so
redirects and scripts are unaffected even when `pager` is set in the
config file. It runs with LESS=FRX and LV=-c set (unless already set),
following git's pager defaults, so `--pager less` shows colors out of
the box. When the pager exits, stern stops tailing and exits; when all
logs have been shown (e.g. --no-follow), stern keeps the pager open
until the user quits it.

Closes stern#345

Signed-off-by: Jojin <[email protected]>
@superbrothers
superbrothers merged commit 4772306 into stern:master Oct 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better UX of using stern with a pager

3 participants