textual.geometry
Functions and classes to manage terminal geometry (anything involving coordinates or dimensions).
NULL_REGION
module-attribute
¶
NULL_REGION = Region(0, 0, 0, 0)
A Region constant for a null region (at the origin, with both width and height set to zero).
NULL_SIZE
module-attribute
¶
NULL_SIZE = Size(0, 0)
A Size constant for a null size (with zero area).
SpacingDimensions
module-attribute
¶
The valid ways in which you can specify spacing.
Offset
¶
Bases: NamedTuple
A cell offset defined by x and y coordinates.
Offsets are typically relative to the top left of the terminal or other container.
Textual prefers the names x
and y
, but you could consider x
to be the column and y
to be the row.
Offsets support addition, subtraction, multiplication, and negation.
Example
blend
¶
blend(destination, factor)
Calculate a new offset on a line between this offset and a destination offset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Offset
|
Point where factor would be 1.0. |
required |
|
float
|
A value between 0 and 1.0. |
required |
Returns:
Type | Description |
---|---|
Offset
|
A new point on a line between self and destination. |
clamp
¶
Region
¶
Bases: NamedTuple
Defines a rectangular region.
A Region consists of a coordinate (x and y) and dimensions (width and height).
(x, y)
┌────────────────────┐ ▲
│ │ │
│ │ │
│ │ height
│ │ │
│ │ │
└────────────────────┘ ▼
◀─────── width ──────▶
Example
bottom_left
property
¶
bottom_right
property
¶
center
property
¶
column_span
property
¶
A pair of integers for the start and end columns (x coordinates) in this region.
The end value is exclusive.
line_span
property
¶
A pair of integers for the start and end lines (y coordinates) in this region.
The end value is exclusive.
offset
property
¶
reset_offset
property
¶
top_right
property
¶
clip
¶
constrain
¶
constrain(constrain_x, constrain_y, margin, container)
Constrain a region to fit within a container, using different methods per axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Literal['none', 'inside', 'inflect']
|
Constrain method for the X-axis. |
required |
|
Literal['none', 'inside', 'inflect']
|
Constrain method for the Y-axis. |
required |
|
Spacing
|
Margin to maintain around region. |
required |
|
Region
|
Container to constrain to. |
required |
Returns:
Type | Description |
---|---|
Region
|
New widget, that fits inside the container (if possible). |
contains
¶
from_corners
classmethod
¶
from_offset
classmethod
¶
from_union
classmethod
¶
from_union(regions)
Create a Region from the union of other regions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Collection[Region]
|
One or more regions. |
required |
Returns:
Type | Description |
---|---|
Region
|
A Region that encloses all other regions. |
get_scroll_to_visible
classmethod
¶
get_scroll_to_visible(window_region, region, *, top=False)
Calculate the smallest offset required to translate a window so that it contains another region.
This method is used to calculate the required offset to scroll something in to view.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Region
|
The window region. |
required |
|
Region
|
The region to move inside the window. |
required |
|
bool
|
Get offset to top of window. |
False
|
Returns:
Type | Description |
---|---|
Offset
|
An offset required to add to region to move it inside window_region. |
inflect
¶
Inflect a region around one or both axis.
The x_axis
and y_axis
parameters define which direction to move the region.
A positive value will move the region right or down, a negative value will move
the region left or up. A value of 0
will leave that axis unmodified.
If a margin is provided, it will add space between the resulting region.
Note that if margin is specified it overlaps, so the space will be the maximum of two edges, and not the total.
╔══════════╗ │
║ ║
║ Self ║ │
║ ║
╚══════════╝ │
─ ─ ─ ─ ─ ─ ─ ─ ┌──────────┐
│ │
│ Result │
│ │
└──────────┘
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
+1 to inflect in the positive direction, -1 to inflect in the negative direction. |
+1
|
|
int
|
+1 to inflect in the positive direction, -1 to inflect in the negative direction. |
+1
|
|
Spacing | None
|
Additional margin. |
None
|
Returns:
Type | Description |
---|---|
Region
|
A new region. |
split
cached
¶
Split a region in to 4 from given x and y offsets (cuts).
cut_x ↓
┌────────┐ ┌───┐
│ │ │ │
│ 0 │ │ 1 │
│ │ │ │
cut_y → └────────┘ └───┘
┌────────┐ ┌───┐
│ 2 │ │ 3 │
└────────┘ └───┘
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
Offset from self.x where the cut should be made. If negative, the cut is taken from the right edge. |
required |
|
int
|
Offset from self.y where the cut should be made. If negative, the cut is taken from the lower edge. |
required |
Returns:
Type | Description |
---|---|
tuple[Region, Region, Region, Region]
|
Four new regions which add up to the original (self). |
split_horizontal
cached
¶
split_horizontal(cut)
Split a region in to two, from a given y offset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
An offset from self.y where the cut should be made. May be negative, for the offset to start from the lower edge. |
required |
Returns:
Type | Description |
---|---|
tuple[Region, Region]
|
Two regions, which add up to the original (self). |
split_vertical
cached
¶
split_vertical(cut)
Split a region in to two, from a given x offset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
An offset from self.x where the cut should be made. If cut is negative, it is taken from the right edge. |
required |
Returns:
Type | Description |
---|---|
tuple[Region, Region]
|
Two regions, which add up to the original (self). |
translate_inside
¶
Translate this region, so it fits within a container.
This will ensure that there is as little overlap as possible. The top left of the returned region is guaranteed to be within the container.
┌──────────────────┐ ┌──────────────────┐
│ container │ │ container │
│ │ │ ┌─────────────┤
│ │ ──▶ │ │ return │
│ ┌──────────┴──┐ │ │ │
│ │ self │ │ │ │
└───────┤ │ └────┴─────────────┘
│ │
└─────────────┘
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Region
|
A container region. |
required |
|
bool
|
Allow translation of X axis. |
True
|
|
bool
|
Allow translation of Y axis. |
True
|
Returns:
Type | Description |
---|---|
Region
|
A new region with same dimensions that fits with inside container. |
Size
¶
Spacing
¶
Bases: NamedTuple
Stores spacing around a widget, such as padding and border.
Spacing is defined by four integers for the space at the top, right, bottom, and left of a region.
┌ ─ ─ ─ ─ ─ ─ ─▲─ ─ ─ ─ ─ ─ ─ ─ ┐
│ top
│ ┏━━━━━▼━━━━━━┓ │
◀──────▶┃ ┃◀───────▶
│ left ┃ ┃ right │
┃ ┃
│ ┗━━━━━▲━━━━━━┛ │
│ bottom
└ ─ ─ ─ ─ ─ ─ ─▼─ ─ ─ ─ ─ ─ ─ ─ ┘
Example
css
property
¶
A string containing the spacing in CSS format.
For example: "1" or "2 4" or "4 2 8 2".
max_height
property
¶
The space between regions in the Y direction if margins overlap, i.e. max(self.top, self.bottom)
.
max_width
property
¶
The space between regions in the X direction if margins overlap, i.e. max(self.left, self.right)
.
unpack
classmethod
¶
unpack(pad)
Unpack padding specified in CSS style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SpacingDimensions
|
An integer, or tuple of 1, 2, or 4 integers. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Returns:
Type | Description |
---|---|
Spacing
|
New Spacing object. |
clamp
¶
Restrict a value to a given range.
If value
is less than the minimum, return the minimum.
If value
is greater than the maximum, return the maximum.
Otherwise, return value
.
The minimum
and maximum
arguments values may be given in reverse order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
T
|
A value. |
required |
|
T
|
Minimum value. |
required |
|
T
|
Maximum value. |
required |
Returns:
Type | Description |
---|---|
T
|
New value that is not less than the minimum or greater than the maximum. |