Skip to content
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed `typing_extensions` from runtime dependencies https://github.com/Textualize/rich/pull/3763
- Live objects (including Progress) may now be nested https://github.com/Textualize/rich/pull/3768

### Fixed

- Fix: word‐wrapped `Syntax` with padding clipped characters (#3727).

## [14.0.0] - 2025-03-30

### Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following people have contributed to the development of Rich:
- [Hugo van Kemenade](https://github.com/hugovk)
- [Andrew Kettmann](https://github.com/akettmann)
- [Alexander Krasnikov](https://github.com/askras)
- [Adam Kuhn](https://github.com/arkuhn)
- [Martin Larralde](https://github.com/althonos)
- [Hedy Li](https://github.com/hedythedev)
- [Henry Mai](https://github.com/tanducmai)
Expand Down
11 changes: 10 additions & 1 deletion rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,16 @@ def __rich_measure__(
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
segments = Segments(self._get_syntax(console, options))
# Reduce available width by padding if necessary, otherwise content is clipped
inner_options = options
if self.padding:
_, pad_right, _, pad_left = Padding.unpack(self.padding)
if pad_left or pad_right:
inner_width = max(0, options.max_width - pad_left - pad_right)
inner_options = options.update_width(inner_width)

segments = Segments(self._get_syntax(console, inner_options))

if self.padding:
yield Padding(segments, style=self._get_base_style(), pad=self.padding)
else:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,31 @@ def test_background_color_override_includes_padding():
)


def test_syntax_word_wrap_with_horizontal_padding():
"""Regression test for https://github.com/Textualize/rich/issues/3727"""

EXAMPLE_CODE = """\
class ListViewExample(App):
def compose(self) -> ComposeResult:
yield ListView(
ListItem(Label("One")),
ListItem(Label("Two")),
ListItem(Label("Three")),
)
yield Footer()\
"""
width = 32
console = Console(width=width, file=io.StringIO(), record=True)

syntax = Syntax(EXAMPLE_CODE, "python", word_wrap=True, padding=(0, 2))
console.print(syntax)
text_output = console.export_text()

assert "One" in text_output
assert "Two" in text_output
assert "Three" in text_output


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