fix: enable word wrapping for syntax-highlighted code output

Long code lines were clipped at the terminal width, making them
unreadable. Pass word_wrap=True to all Syntax() calls so lines wrap
instead of being cut off.
This commit is contained in:
2026-04-04 14:47:21 -07:00
parent 579df72869
commit 7df3acde9b
2 changed files with 69 additions and 3 deletions

View File

@@ -83,12 +83,20 @@ def render(
except (SyntaxError, ValueError): except (SyntaxError, ValueError):
log_debug(options.debug, "render: invalid JSON, falling back to syntax") log_debug(options.debug, "render: invalid JSON, falling back to syntax")
renderable = Syntax( renderable = Syntax(
content, "json", theme=options.theme, line_numbers=line_numbers content,
"json",
theme=options.theme,
line_numbers=line_numbers,
word_wrap=True,
) )
else: else:
try: try:
renderable = Syntax( renderable = Syntax(
content, file_type, theme=options.theme, line_numbers=line_numbers content,
file_type,
theme=options.theme,
line_numbers=line_numbers,
word_wrap=True,
) )
except Exception: except Exception:
log_debug( log_debug(
@@ -96,7 +104,11 @@ def render(
f"render: unknown lexer {file_type!r}, falling back to plain text", f"render: unknown lexer {file_type!r}, falling back to plain text",
) )
renderable = Syntax( renderable = Syntax(
content, "text", theme=options.theme, line_numbers=line_numbers content,
"text",
theme=options.theme,
line_numbers=line_numbers,
word_wrap=True,
) )
padded = Padding(renderable, (1, 2)) padded = Padding(renderable, (1, 2))

View File

@@ -94,6 +94,7 @@ class TestRenderJson:
"json", "json",
theme="monokai", theme="monokai",
line_numbers=False, line_numbers=False,
word_wrap=True,
) )
@@ -113,6 +114,7 @@ class TestRenderCode:
"python", "python",
theme="monokai", theme="monokai",
line_numbers=True, line_numbers=True,
word_wrap=True,
) )
def test_renders_unknown_type_as_text_fallback(self, capture_console) -> None: def test_renders_unknown_type_as_text_fallback(self, capture_console) -> None:
@@ -136,11 +138,13 @@ class TestRenderCode:
assert mock_syntax.call_args_list[0].kwargs == { assert mock_syntax.call_args_list[0].kwargs == {
"theme": "monokai", "theme": "monokai",
"line_numbers": True, "line_numbers": True,
"word_wrap": True,
} }
assert mock_syntax.call_args_list[1].args == ("some content", "text") assert mock_syntax.call_args_list[1].args == ("some content", "text")
assert mock_syntax.call_args_list[1].kwargs == { assert mock_syntax.call_args_list[1].kwargs == {
"theme": "monokai", "theme": "monokai",
"line_numbers": True, "line_numbers": True,
"word_wrap": True,
} }
def test_renders_rust(self, capture_console) -> None: def test_renders_rust(self, capture_console) -> None:
@@ -156,9 +160,59 @@ class TestRenderCode:
"rust", "rust",
theme="monokai", theme="monokai",
line_numbers=True, line_numbers=True,
word_wrap=True,
) )
class TestCodeWrap:
"""Code wrapping (word_wrap) for syntax-highlighted output."""
def test_python_syntax_has_word_wrap(
self, capture_console, sample_python: str
) -> None:
opts = RenderOptions(pager=False)
with patch("rp.render.Syntax", wraps=Syntax) as mock_syntax:
render(sample_python, "python", capture_console, opts)
mock_syntax.assert_called_once()
assert mock_syntax.call_args.kwargs["word_wrap"] is True
def test_long_line_wraps_in_output(self, capture_console) -> None:
long_line = "x = " + "'a' * " * 200 + "1"
opts = RenderOptions(pager=False, line_numbers=False)
render(long_line, "python", capture_console, opts)
output = capture_console.file.getvalue()
assert len(output) > 0
lines = output.split("\n")
non_empty = [line for line in lines if line.strip()]
assert len(non_empty) > 1, "Long line should wrap across multiple visual lines"
def test_text_fallback_has_word_wrap(self, capture_console) -> None:
opts = RenderOptions(pager=False)
def raise_on_bad_lexer(*args, **kwargs):
if args[1] == "unknown_xyz":
raise Exception("bad lexer")
return Syntax(*args, **kwargs)
with patch("rp.render.Syntax", side_effect=raise_on_bad_lexer) as mock_syntax:
render("some text", "unknown_xyz", capture_console, opts)
fallback_call = mock_syntax.call_args_list[-1]
assert fallback_call.args[1] == "text"
assert fallback_call.kwargs["word_wrap"] is True
def test_json_fallback_has_word_wrap(self, capture_console) -> None:
opts = RenderOptions(pager=False)
with patch("rp.render.Syntax", wraps=Syntax) as mock_syntax:
render("{invalid}", "json", capture_console, opts)
mock_syntax.assert_called_once()
assert mock_syntax.call_args.kwargs["word_wrap"] is True
class TestLineNumbers: class TestLineNumbers:
"""Line number auto-detection logic.""" """Line number auto-detection logic."""