Skip to content

Fix help preview generation with newer releases of rich #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Unreleased

### Fixes
- [GH-133](https://github.com/hamdanal/rich-argparse/issues/133),
[PR-135](https://github.com/hamdanal/rich-argparse/pull/135)
Fix help preview generation with newer releases of rich.
- [GH-130](https://github.com/hamdanal/rich-argparse/issues/130),
[PR-131](https://github.com/hamdanal/rich-argparse/pull/131)
Fix a bug that caused long group titles to wrap.
Expand Down
6 changes: 3 additions & 3 deletions rich_argparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,11 @@ def __call__(
parser.exit(1, "error: help preview path must be a string\n")
if not path.endswith((".svg", ".html", ".txt")):
parser.exit(1, "error: help preview path must end with .svg, .html, or .txt\n")
import io

text = r.Text.from_ansi(parser.format_help())
console = r.Console(record=True)
with console.capture():
console.print(text, crop=False)
console = r.Console(file=io.StringIO(), record=True)
console.print(text, crop=False)

if path.endswith(".svg"):
self.export_kwds.setdefault("title", "")
Expand Down
8 changes: 6 additions & 2 deletions tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,9 @@ def test_help_preview_generation(tmp_path):
parser.parse_args(["--generate", str(svg_file)])
assert exc_info.value.code == 0
assert svg_file.exists()
assert svg_file.read_text().startswith("<svg")
svg_out = svg_file.read_text()
assert svg_out.startswith("<svg")
assert "Usage" in svg_out

# HTML file
preview_action.export_kwds = {}
Expand All @@ -987,7 +989,9 @@ def test_help_preview_generation(tmp_path):
parser.parse_args(["--generate", str(html_file)])
assert exc_info.value.code == 0
assert html_file.exists()
assert html_file.read_text().startswith("<!DOCTYPE html>")
html_out = html_file.read_text()
assert html_out.startswith("<!DOCTYPE html>")
assert "Usage" in html_out

# TXT file
preview_action.export_kwds = {}
Expand Down