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:
@@ -94,6 +94,7 @@ class TestRenderJson:
|
||||
"json",
|
||||
theme="monokai",
|
||||
line_numbers=False,
|
||||
word_wrap=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -113,6 +114,7 @@ class TestRenderCode:
|
||||
"python",
|
||||
theme="monokai",
|
||||
line_numbers=True,
|
||||
word_wrap=True,
|
||||
)
|
||||
|
||||
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 == {
|
||||
"theme": "monokai",
|
||||
"line_numbers": True,
|
||||
"word_wrap": True,
|
||||
}
|
||||
assert mock_syntax.call_args_list[1].args == ("some content", "text")
|
||||
assert mock_syntax.call_args_list[1].kwargs == {
|
||||
"theme": "monokai",
|
||||
"line_numbers": True,
|
||||
"word_wrap": True,
|
||||
}
|
||||
|
||||
def test_renders_rust(self, capture_console) -> None:
|
||||
@@ -156,9 +160,59 @@ class TestRenderCode:
|
||||
"rust",
|
||||
theme="monokai",
|
||||
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:
|
||||
"""Line number auto-detection logic."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user