diff --git a/CHANGELOG.md b/CHANGELOG.md index cb14769560..66ea8f47ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 10268ddc9b..dc1b7b92e5 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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) diff --git a/rich/syntax.py b/rich/syntax.py index cff8fd235d..3e8d8058f0 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -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: diff --git a/tests/test_syntax.py b/tests/test_syntax.py index 04edb9cc3e..a2527337b7 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -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(