This commit is contained in:
Stranger-Jie 2025-12-06 14:41:04 +01:00 committed by GitHub
commit 9090a849fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View file

@ -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)

View file

@ -6,7 +6,7 @@ from typing import Dict, Iterable, List, Optional, Tuple
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,29 @@ class Columns(JupyterMixin):
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]
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 = maximum_width + padding + 2
else:
width = self.width
return Measurement(width, width)
if __name__ == "__main__": # pragma: no cover
import os

View file

@ -3,6 +3,7 @@ import io
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 @@ tests = [
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 @@ expected = [
"╭────────────────────────────────────────────────╮\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",
]