Skip to content

feat: read output width from the environment when there is no tty - #65

Merged
zen-xu merged 1 commit into
mainfrom
feat/respect-columns-env
Aug 6, 2026
Merged

zen-xu merged 1 commit into
mainfrom
feat/respect-columns-env

Conversation

@zen-xu

@zen-xu zen-xu commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Problem

The reporter console is built with force_terminal=True so colors survive being
piped. That flag also makes rich skip its own COLUMNS handling, and with no tty
to measure the width was pinned:

width=None if sys.stdout.isatty() else 200          # console
generate_header_group(session), title=title_msg, width=120   # session header

So in a CI log there was no way to adjust the width — setting COLUMNS had no
effect, because the explicit width= argument overrides it.

Change

Add console_width(), which resolves in the order: no width on a tty (rich
measures it) → COLUMNSPYTEST_MODERN_WIDTH → 200 as the fallback.
Non-numeric and non-positive values are ignored rather than raising.

PYTEST_MODERN_WIDTH is there so the width can be set without COLUMNS
affecting every other tool in the same shell.

The session header now uses min(120, self.console.width) so it tracks a
narrower console instead of overflowing it.

Verification

console_width() across inputs:

COLUMNS result
unset 200
100 100
160 160
240 240
abc 200
0 200
-5 200

Test suite compared against main with reruns disabled to remove the
nondeterminism — identical on both: 12 passed, 4 failed, 2 timeout. (Those
failures are the intentional demo fixtures in tests/.) ruff check and
ruff format pass.

Note

The hunk at terminal.py:277 reformats a pre-existing long lambda line and is
unrelated to this change — ruff format insists on it, so reverting it would
fail pre-commit.ci.

The reporter console is built with force_terminal=True so colors survive
being piped, but that also makes rich skip its own COLUMNS handling. With
no tty to measure, the width was pinned to 200 and the session header to
120, so neither could be adjusted for a CI log viewer.

Read COLUMNS, or PYTEST_MODERN_WIDTH to set the width without affecting
other tools, and keep 200 as the fallback. The header panel now tracks the
console width instead of overflowing a narrower one.
@zen-xu
zen-xu merged commit b2431a3 into main Aug 6, 2026
3 checks passed
@zen-xu
zen-xu deleted the feat/respect-columns-env branch August 6, 2026 14:00
@zen-xu zen-xu mentioned this pull request Aug 6, 2026
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.

1 participant