Skip to content
Open
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
16 changes: 16 additions & 0 deletions benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ def _print_with_width(self, width):
self.console.print(self.syntax, width)


class SyntaxLineNumbersWrappingSuite:
def setup(self):
self.console = Console(
file=StringIO(), color_system="truecolor", legacy_windows=False
)
self.syntax = Syntax(
code=snippets.PYTHON_SNIPPET * 120,
lexer="python",
word_wrap=True,
line_numbers=True,
)

def time_text_thin_terminal_heavy_wrapping(self):
self.console.print(self.syntax, width=30)


class TableSuite:
def time_table_no_wrapping(self):
self._print_table(width=100)
Expand Down
39 changes: 33 additions & 6 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,41 @@ def _get_syntax(
highlight_number_style,
) = self._get_number_styles(console)

if self.word_wrap and not self.line_numbers:
text = Text("\n").join(lines)
syntax_lines = console.render_lines(
text,
render_options.update(height=None, justify="left"),
style=background_style,
pad=not transparent_background,
new_lines=True,
)
for syntax_line in syntax_lines:
yield from syntax_line
return

for line_no, line in enumerate(lines, self.start_line + line_offset):
if self.word_wrap:
wrapped_lines = console.render_lines(
line,
render_options.update(height=None, justify="left"),
style=background_style,
pad=not transparent_background,
)
wrapped_lines = [
_Segment.adjust_line_length(
list(
_Segment.apply_style(
wrapped_line.render(console), background_style
)
),
render_options.max_width,
style=background_style,
pad=not transparent_background,
)
for wrapped_line in line.wrap(
console,
render_options.max_width,
justify="left",
overflow=render_options.overflow,
tab_size=self.tab_size,
no_wrap=render_options.no_wrap,
)
]
else:
segments = list(line.render(console, end=""))
if options.no_wrap:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ def test_padding_plus_wrap() -> None:
assert output == expected


def test_word_wrap_without_line_numbers_with_line_range() -> None:
console = Console(width=14, file=io.StringIO(), legacy_windows=False, record=True)
syntax = Syntax(
"first line should not appear\n"
"second line wraps around here\n"
"third line stays\n"
"fourth line should not appear",
lexer="text",
word_wrap=True,
line_numbers=False,
line_range=(2, 3),
)

console.print(syntax)

assert (
console.export_text()
== "second line \nwraps around \nhere \nthird line \nstays \n"
)


if __name__ == "__main__":
syntax = Panel.fit(
Syntax(
Expand Down
Loading