From 342cd0312addb73978efa4569aa71d0e491fe7b8 Mon Sep 17 00:00:00 2001 From: Stranger-Jie <1013170776@qq.com> Date: Thu, 7 Aug 2025 21:56:14 +0800 Subject: [PATCH 1/3] fix issues 3820 --- CONTRIBUTORS.md | 1 + rich/columns.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4b04786b9c..75351f6991 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -70,6 +70,7 @@ The following people have contributed to the development of Rich: - [Damian Shaw](https://github.com/notatallshaw) - [Nicolas Simonds](https://github.com/0xDEC0DE) - [Aaron Stephens](https://github.com/aaronst) +- [Stranger](https://github.com/Stranger-Jie) - [Karolina Surma](https://github.com/befeleme) - [Gabriele N. Tornetta](https://github.com/p403n1x87) - [Nils Vu](https://github.com/nilsvu) diff --git a/rich/columns.py b/rich/columns.py index 669a3a7074..ff19ca6159 100644 --- a/rich/columns.py +++ b/rich/columns.py @@ -6,7 +6,7 @@ from .align import Align, AlignMethod from .console import Console, ConsoleOptions, RenderableType, RenderResult from .constrain import Constrain -from .measure import Measurement +from .measure import Measurement, measure_renderables from .padding import Padding, PaddingDimensions from .table import Table from .text import TextType @@ -170,6 +170,28 @@ def iter_renderables( add_row(*row) yield table + def __rich_measure__( + self, console: "Console", options: "ConsoleOptions" + ) -> "Measurement": + title = self.title + _, right, _, left = Padding.unpack(self.padding) + padding = left + right + renderables = [*self.renderables, title] if title else [*self.renderables] + + if self.width is None: + width = ( + measure_renderables( + console, + options.update_width(options.max_width - padding - 2), + renderables, + ).maximum + + padding + + 2 + ) + else: + width = self.width + return Measurement(width, width) + if __name__ == "__main__": # pragma: no cover import os From 55fe577346cd71947fed3ac10d46ae640c33c664 Mon Sep 17 00:00:00 2001 From: Stranger-Jie Date: Mon, 27 Oct 2025 12:45:44 +0800 Subject: [PATCH 2/3] fix maximum width of columns measure --- rich/columns.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/rich/columns.py b/rich/columns.py index ff19ca6159..bd06c67185 100644 --- a/rich/columns.py +++ b/rich/columns.py @@ -178,16 +178,17 @@ def __rich_measure__( padding = left + right renderables = [*self.renderables, title] if title else [*self.renderables] + maximum_width = sum( + measure_renderables( + console, + options.update_width(options.max_width - padding - 2), + [renderable], + ).maximum + for renderable in renderables + ) + if self.width is None: - width = ( - measure_renderables( - console, - options.update_width(options.max_width - padding - 2), - renderables, - ).maximum - + padding - + 2 - ) + width = maximum_width + padding + 2 else: width = self.width return Measurement(width, width) From 92523486fba071b9a692a358bbc628752339dbfb Mon Sep 17 00:00:00 2001 From: Stranger-Jie Date: Mon, 27 Oct 2025 12:46:58 +0800 Subject: [PATCH 3/3] test for columns in panel with expand --- tests/test_panel.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_panel.py b/tests/test_panel.py index 33ce1afa14..36593528a0 100644 --- a/tests/test_panel.py +++ b/tests/test_panel.py @@ -3,6 +3,7 @@ import pytest from rich.console import Console +from rich.columns import Columns from rich.panel import Panel from rich.segment import Segment from rich.style import Style @@ -16,6 +17,8 @@ Panel(Panel("Hello, World", padding=0), padding=0), Panel("Hello, World", title="FOO", padding=0), Panel("Hello, World", subtitle="FOO", padding=0), + Panel(Columns(["Hello", "World"]), padding=0), + Panel(Columns(["Hello", "World"]), expand=False, padding=0), ] expected = [ @@ -26,6 +29,8 @@ "╭────────────────────────────────────────────────╮\n│╭──────────────────────────────────────────────╮│\n││Hello, World ││\n│╰──────────────────────────────────────────────╯│\n╰────────────────────────────────────────────────╯\n", "╭───────────────────── FOO ──────────────────────╮\n│Hello, World │\n╰────────────────────────────────────────────────╯\n", "╭────────────────────────────────────────────────╮\n│Hello, World │\n╰───────────────────── FOO ──────────────────────╯\n", + "╭────────────────────────────────────────────────╮\n│Hello World │\n╰────────────────────────────────────────────────╯\n", + "╭──────────────╮\n│Hello World │\n╰──────────────╯\n", ]