1from operator
import itemgetter
2from typing
import TYPE_CHECKING, Callable, NamedTuple, Optional, Sequence
5from .protocol
import is_renderable, rich_cast
8 from .console
import Console, ConsoleOptions, RenderableType
12 """Stores the minimum and maximum widths (in characters) required to render an object."""
15 """Minimum number of cells required to render."""
17 """Maximum number of cells required to render."""
21 """Get difference between maximum and minimum."""
25 """Get measurement that ensures that minimum <= maximum and minimum >= 0
28 Measurement: A normalized measurement.
30 minimum, maximum = self
31 minimum = min(max(0, minimum), maximum)
32 return Measurement(max(0, minimum), max(0, max(minimum, maximum)))
35 """Get a RenderableWith where the widths are <= width.
38 width (int): Maximum desired width.
41 Measurement: New Measurement object.
43 minimum, maximum = self
44 return Measurement(min(minimum, width), min(maximum, width))
47 """Get a RenderableWith where the widths are >= width.
50 width (int): Minimum desired width.
53 Measurement: New Measurement object.
55 minimum, maximum = self
57 return Measurement(max(minimum, width), max(maximum, width))
60 self, min_width: Optional[int] =
None, max_width: Optional[int] =
None
62 """Clamp a measurement within the specified range.
65 min_width (int): Minimum desired width, or ``None`` for no minimum. Defaults to None.
66 max_width (int): Maximum desired width, or ``None`` for no maximum. Defaults to None.
69 Measurement: New Measurement object.
72 if min_width
is not None:
74 if max_width
is not None:
80 cls, console:
"Console", options:
"ConsoleOptions", renderable:
"RenderableType"
82 """Get a measurement for a renderable.
85 console (~rich.console.Console): Console instance.
86 options (~rich.console.ConsoleOptions): Console options.
87 renderable (RenderableType): An object that may be rendered with Rich.
90 errors.NotRenderableError: If the object is not renderable.
93 Measurement: Measurement object containing range of character widths required to render the object.
102 renderable = rich_cast(renderable)
103 if is_renderable(renderable):
104 get_console_width: Optional[
105 Callable[[
"Console",
"ConsoleOptions"],
"Measurement"]
106 ] =
getattr(renderable,
"__rich_measure__",
None)
107 if get_console_width
is not None:
120 f
"Unable to get render width for {renderable!r}; "
121 "a str, Segment, or object with __rich_console__ method is required"
125def measure_renderables(
127 options:
"ConsoleOptions",
128 renderables: Sequence[
"RenderableType"],
130 """Get a measurement that would fit a number of renderables.
133 console (~rich.console.Console): Console instance.
134 options (~rich.console.ConsoleOptions): Console options.
135 renderables (Iterable[RenderableType]): One or more renderable objects.
138 Measurement: Measurement object containing range of character widths required to
139 contain all given renderables.
145 get_measurement(console, options, renderable)
for renderable
in renderables
148 max(measurements, key=itemgetter(0)).minimum,
149 max(measurements, key=itemgetter(1)).maximum,
151 return measured_width
"Measurement" with_minimum(self, int width)
"Measurement" normalize(self)
"Measurement" with_maximum(self, int width)
"Measurement" clamp(self, Optional[int] min_width=None, Optional[int] max_width=None)