rich measure

This commit is contained in:
Will McGugan 2021-03-25 21:08:36 +00:00
parent 27379a8f1a
commit be5e9dca2d
43 changed files with 301 additions and 185 deletions

View file

@ -18,25 +18,22 @@ To define a layout, construct a Layout object and print it::
layout = Layout()
print(layout)
This will draw a box the size of the terminal with some information regarding the layout. The box is a "placeholder" because we have yet to add any content to it. Before we do that, let's create a more interesting layout by calling the :meth:`~rich.layout.Layout.split` method to divide the layout in to two sub-layouts::
This will draw a box the size of the terminal with some information regarding the layout. The box is a "placeholder" because we have yet to add any content to it. Before we do that, let's create a more interesting layout by calling the :meth:`~rich.layout.Layout.split_column` method to divide the layout in to two sub-layouts::
layout.split(
layout.split_column(
Layout(name="upper"),
Layout(name="lower")
)
print(layout)
This will divide the terminal screen in to two equal sized portions, one on top of the other. The ``name`` attribute is an internal identifier we can use to look up the sub-layout later. Let's use that to create another split::
This will divide the terminal screen in to two equal sized portions, one on top of the other. The ``name`` attribute is an internal identifier we can use to look up the sub-layout later. Let's use that to create another split, this time we will call :meth:`~rich.layout.Layout.split_row` to split the lower layout in to a row of two sub-layouts.
layout["lower"].split(
layout["lower"].split_row(
Layout(name="left"),
Layout(name="right"),
direction="horizontal"
Layout(name="right"),
)
print(layout)
The addition of the ``direction="horizontal"`` tells the Layout class to split left-to-right, rather than the default of top-to-bottom.
You should now see the screen area divided in to 3 portions; an upper half and a lower half that is split in to two quarters.
.. raw:: html

View file

@ -64,10 +64,10 @@ For complete control over how a custom object is rendered to the terminal, you c
Measuring Renderables
~~~~~~~~~~~~~~~~~~~~~
Sometimes Rich needs to know how many characters an object will take up when rendering. The :class:`~rich.table.Table` class, for instance, will use this information to calculate the optimal dimensions for the columns. If you aren't using one of the renderable objects in the Rich module, you will need to supply a ``__rich_measure__`` method which accepts a :class:`~rich.console.Console` and the maximum width and returns a :class:`~rich.measure.Measurement` object. The Measurement object should contain the *minimum* and *maximum* number of characters required to render.
Sometimes Rich needs to know how many characters an object will take up when rendering. The :class:`~rich.table.Table` class, for instance, will use this information to calculate the optimal dimensions for the columns. If you aren't using one of the renderable objects in the Rich module, you will need to supply a ``__rich_measure__`` method which accepts a :class:`~rich.console.Console` and :class:`~rich.console.ConsoleOptions` and returns a :class:`~rich.measure.Measurement` object. The Measurement object should contain the *minimum* and *maximum* number of characters required to render.
For example, if we are rendering a chess board, it would require a minimum of 8 characters to render. The maximum can be left as the maximum available width (assuming a centered board)::
class ChessBoard:
def __rich_measure__(self, console: Console, max_width: int) -> Measurement:
return Measurement(8, max_width)
def __rich_measure__(self, console: Console, options: ConsoleOptions) -> Measurement:
return Measurement(8, options.max_width)