Skip to content

xnano.geometry

xnano.geometry

xnano.geometry

Points, sizes and rectangles, measured in cells.

These are plain values; the position of something that moves is an Expression instead.

Classes:

  • Point –

    A cell position, counted in columns (x) and rows (y) from the top-left.

  • Rectangle –

    A rectangle of cells with half-open bounds.

  • Size –

    A width and a height, counted in cells.

  • SizeRule –

    How much space one child takes along a layout's main axis.

Point

Point(x: int = 0, y: int = 0)

A cell position, counted in columns (x) and rows (y) from the top-left.

Maps to ratatui::layout::Position.

Parameters:

  • x (int, default: 0 ) –

    The column.

  • y (int, default: 0 ) –

    The row.

Attributes:

  • x (int) –

    The column.

  • y (int) –

    The row.

Examples:

point = Point(2, 3) + Point(1, 1)
assert (point.x, point.y) == (3, 4)
assert point - Point(3, 4) == Point(0, 0)

Create a Point.

Parameters:

  • x (int, default: 0 ) –

    int. Defaults to 0.

  • y (int, default: 0 ) –

    int. Defaults to 0.

Methods:

  • __add__ –

    Add two positions component by component.

  • __sub__ –

    Subtract two positions component by component.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, x: int = 0, y: int = 0) -> None:
    """
    Create a Point.

    Args:
        x: ``int``. Defaults to ``0``.
        y: ``int``. Defaults to ``0``.
    """

x property

x: int

The column.

y property

y: int

The row.

__add__

__add__(other: Point | Offset) -> Point

Add two positions component by component.

Examples:

assert Point(1, 2) + Point(3, 4) == Point(4, 6)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __add__(self, other: Point | Offset) -> Point:
    """
    Add two positions component by component.

    Examples:
        ```python
        assert Point(1, 2) + Point(3, 4) == Point(4, 6)
        ```
    """

__sub__

__sub__(other: Point | Offset) -> Point

Subtract two positions component by component.

Examples:

assert Point(4, 6) - Point(3, 4) == Point(1, 2)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __sub__(self, other: Point | Offset) -> Point:
    """
    Subtract two positions component by component.

    Examples:
        ```python
        assert Point(4, 6) - Point(3, 4) == Point(1, 2)
        ```
    """

Rectangle

Rectangle(x: int = 0, y: int = 0, width: int = 0, height: int = 0)

A rectangle of cells with half-open bounds.

The left and top edges are inclusive, and the right and bottom edges are exclusive.

Maps to ratatui::layout::Rect.

Parameters:

  • x (int, default: 0 ) –

    The left column.

  • y (int, default: 0 ) –

    The top row.

  • width (int, default: 0 ) –

    The number of columns.

  • height (int, default: 0 ) –

    The number of rows.

Attributes:

Examples:

area = Rectangle(2, 1, 10, 4)
assert area.area == 40
assert area.contains(Point(2, 1))
assert not area.contains(Point(12, 1))

Create a Rectangle.

Parameters:

  • x (int, default: 0 ) –

    int. Defaults to 0.

  • y (int, default: 0 ) –

    int. Defaults to 0.

  • width (int, default: 0 ) –

    int. Defaults to 0.

  • height (int, default: 0 ) –

    int. Defaults to 0.

Methods:

  • contains –

    Whether point lies inside the rectangle.

  • intersects –

    Whether the two rectangles share at least one cell.

  • intersection –

    The cells both rectangles share. The result is empty when they do not overlap.

  • union –

    The smallest rectangle that holds both rectangles.

  • translate –

    Move the rectangle by a number of columns and rows.

  • inset –

    Shrink the rectangle by a margin on every side.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, x: int = 0, y: int = 0, width: int = 0, height: int = 0) -> None:
    """
    Create a Rectangle.

    Args:
        x: ``int``. Defaults to ``0``.
        y: ``int``. Defaults to ``0``.
        width: ``int``. Defaults to ``0``.
        height: ``int``. Defaults to ``0``.
    """

x property

x: int

The left column.

y property

y: int

The top row.

width property

width: int

The number of columns.

height property

height: int

The number of rows.

position property

position: Point

The top-left corner.

size property

size: Size

The width and height.

area property

area: int

The number of cells, width * height.

is_empty property

is_empty: bool

Whether the rectangle holds no cells.

contains

contains(point: Point) -> bool

Whether point lies inside the rectangle.

Examples:

area = Rectangle(0, 0, 4, 2)
assert area.contains(Point(3, 1))
assert not area.contains(Point(4, 1))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def contains(self, point: Point) -> bool:
    """
    Whether ``point`` lies inside the rectangle.

    Examples:
        ```python
        area = Rectangle(0, 0, 4, 2)
        assert area.contains(Point(3, 1))
        assert not area.contains(Point(4, 1))
        ```
    """

intersects

intersects(other: Rectangle) -> bool

Whether the two rectangles share at least one cell.

Examples:

assert Rectangle(0, 0, 4, 4).intersects(Rectangle(3, 3, 4, 4))
assert not Rectangle(0, 0, 4, 4).intersects(Rectangle(4, 0, 4, 4))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def intersects(self, other: Rectangle) -> bool:
    """
    Whether the two rectangles share at least one cell.

    Examples:
        ```python
        assert Rectangle(0, 0, 4, 4).intersects(Rectangle(3, 3, 4, 4))
        assert not Rectangle(0, 0, 4, 4).intersects(Rectangle(4, 0, 4, 4))
        ```
    """

intersection

intersection(other: Rectangle) -> Rectangle

The cells both rectangles share. The result is empty when they do not overlap.

Examples:

shared = Rectangle(0, 0, 10, 5).intersection(Rectangle(5, 2, 10, 10))
assert shared == Rectangle(5, 2, 5, 3)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def intersection(self, other: Rectangle) -> Rectangle:
    """
    The cells both rectangles share. The result is empty when they do not overlap.

    Examples:
        ```python
        shared = Rectangle(0, 0, 10, 5).intersection(Rectangle(5, 2, 10, 10))
        assert shared == Rectangle(5, 2, 5, 3)
        ```
    """

union

union(other: Rectangle) -> Rectangle

The smallest rectangle that holds both rectangles.

Examples:

joined = Rectangle(0, 0, 10, 5).union(Rectangle(8, 8, 4, 4))
assert joined == Rectangle(0, 0, 12, 12)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def union(self, other: Rectangle) -> Rectangle:
    """
    The smallest rectangle that holds both rectangles.

    Examples:
        ```python
        joined = Rectangle(0, 0, 10, 5).union(Rectangle(8, 8, 4, 4))
        assert joined == Rectangle(0, 0, 12, 12)
        ```
    """

translate

translate(x: int = 0, y: int = 0) -> Rectangle

Move the rectangle by a number of columns and rows.

Parameters:

  • x (int, default: 0 ) –

    Columns to move right; negative values move left.

  • y (int, default: 0 ) –

    Rows to move down; negative values move up.

Examples:

assert Rectangle(0, 0, 4, 2).translate(2, 1) == Rectangle(2, 1, 4, 2)
assert Rectangle(5, 5, 4, 2).translate(x=-1, y=-2) == Rectangle(4, 3, 4, 2)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def translate(self, x: int = 0, y: int = 0) -> Rectangle:
    """
    Move the rectangle by a number of columns and rows.

    Args:
        x: Columns to move right; negative values move left.
        y: Rows to move down; negative values move up.

    Examples:
        ```python
        assert Rectangle(0, 0, 4, 2).translate(2, 1) == Rectangle(2, 1, 4, 2)
        assert Rectangle(5, 5, 4, 2).translate(x=-1, y=-2) == Rectangle(4, 3, 4, 2)
        ```
    """

inset

inset(horizontal: int = 0, vertical: int = 0) -> Rectangle

Shrink the rectangle by a margin on every side.

Examples:

assert Rectangle(0, 0, 10, 5).inset(1, 1) == Rectangle(1, 1, 8, 3)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def inset(self, horizontal: int = 0, vertical: int = 0) -> Rectangle:
    """
    Shrink the rectangle by a margin on every side.

    Examples:
        ```python
        assert Rectangle(0, 0, 10, 5).inset(1, 1) == Rectangle(1, 1, 8, 3)
        ```
    """

Size

Size(width: int = 0, height: int = 0)

A width and a height, counted in cells.

Maps to ratatui::layout::Size.

Parameters:

  • width (int, default: 0 ) –

    The number of columns.

  • height (int, default: 0 ) –

    The number of rows.

Attributes:

Examples:

size = Size(80, 24)
assert (size.width, size.height) == (80, 24)

Create a Size.

Parameters:

  • width (int, default: 0 ) –

    int. Defaults to 0.

  • height (int, default: 0 ) –

    int. Defaults to 0.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __init__(self, width: int = 0, height: int = 0) -> None:
    """
    Create a Size.

    Args:
        width: ``int``. Defaults to ``0``.
        height: ``int``. Defaults to ``0``.
    """

width property

width: int

The number of columns.

height property

height: int

The number of rows.

SizeRule

How much space one child takes along a layout's main axis.

Maps to ratatui::layout::Constraint.

Examples:

rules = [SizeRule.cells(4), SizeRule.fill()]
parent = Buffer(width=12, height=3)
first, second = Buffer(), Buffer()
parent.attach(first, geometry='inherit')
parent.attach(second, geometry='inherit')
parent.update(Layout('row', sizes=rules))
assert (first.width, second.width) == (4, 8)

Methods:

  • cells –

    Take exactly count cells.

  • percent –

    Take percentage percent of the available space.

  • fill –

    Share the remaining space with other fill rules in proportion to weight.

  • fit –

    Take the size of the child's own content.

  • minimum –

    Take at least count cells.

  • maximum –

    Take at most count cells.

cells classmethod

cells(count: int) -> SizeRule

Take exactly count cells.

Examples:

assert repr(SizeRule.cells(4)) == 'SizeRule.cells(4)'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def cells(cls, count: int) -> SizeRule:
    """
    Take exactly ``count`` cells.

    Examples:
        ```python
        assert repr(SizeRule.cells(4)) == 'SizeRule.cells(4)'
        ```
    """

percent classmethod

percent(percentage: int) -> SizeRule

Take percentage percent of the available space.

Examples:

assert repr(SizeRule.percent(50)) == 'SizeRule.percent(50)'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def percent(cls, percentage: int) -> SizeRule:
    """
    Take ``percentage`` percent of the available space.

    Examples:
        ```python
        assert repr(SizeRule.percent(50)) == 'SizeRule.percent(50)'
        ```
    """

fill classmethod

fill(weight: int = 1) -> SizeRule

Share the remaining space with other fill rules in proportion to weight.

Examples:

assert repr(SizeRule.fill(2)) == 'SizeRule.fill(2)'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def fill(cls, weight: int = 1) -> SizeRule:
    """
    Share the remaining space with other fill rules in proportion to ``weight``.

    Examples:
        ```python
        assert repr(SizeRule.fill(2)) == 'SizeRule.fill(2)'
        ```
    """

fit classmethod

fit() -> SizeRule

Take the size of the child's own content.

Examples:

assert repr(SizeRule.fit()) == 'SizeRule.fit()'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def fit(cls) -> SizeRule:
    """
    Take the size of the child's own content.

    Examples:
        ```python
        assert repr(SizeRule.fit()) == 'SizeRule.fit()'
        ```
    """

minimum classmethod

minimum(count: int) -> SizeRule

Take at least count cells.

Examples:

assert repr(SizeRule.minimum(2)) == 'SizeRule.minimum(2)'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def minimum(cls, count: int) -> SizeRule:
    """
    Take at least ``count`` cells.

    Examples:
        ```python
        assert repr(SizeRule.minimum(2)) == 'SizeRule.minimum(2)'
        ```
    """

maximum classmethod

maximum(count: int) -> SizeRule

Take at most count cells.

Examples:

assert repr(SizeRule.maximum(3)) == 'SizeRule.maximum(3)'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def maximum(cls, count: int) -> SizeRule:
    """
    Take at most ``count`` cells.

    Examples:
        ```python
        assert repr(SizeRule.maximum(3)) == 'SizeRule.maximum(3)'
        ```
    """