Skip to content

xnano.expressions

xnano.expressions

xnano.expressions

The expression language: values that are evaluated again on every frame.

An Expression stands for a number, a position or a state that changes, such as the elapsed time, the width of the parent or the pointer position. Combine them with arithmetic and give them to anything that takes a number, or read them from the parent, root and time attributes on a Canvas or a Placement.

Classes:

Attributes:

FPS module-attribute

The runtime's estimated frames per second.

BufferRegion module-attribute

BufferRegion: Value[Rectangle]

The rectangle of a buffer on the screen, counting every parent offset.

BufferSize module-attribute

BufferSize: Value[Size]

The dimensions of a buffer as a Size.

DeviceHeight module-attribute

DeviceHeight: NumericValue[int | float]

The terminal height, in cells.

DeviceWidth module-attribute

DeviceWidth: NumericValue[int | float]

The terminal width, in cells.

Focused module-attribute

Focused: Value[bool]

Whether the runtime has focus.

Height module-attribute

Height: NumericValue[int | float]

The height of a buffer or region, in terminal cells.

IsVisible module-attribute

IsVisible: Property[bool]

Whether a buffer is drawn. Assign it with IsVisible(False).

KeyboardChord module-attribute

KeyboardChord: Value[str | None]

The latest key event with its modifiers, such as 'ctrl+s', or None.

KeyboardKey module-attribute

KeyboardKey: Value[str | None]

The key of the latest key event, or None while the latest input was not a key.

KeyboardKind module-attribute

KeyboardKind: Value[Literal['press', 'repeat', 'release'] | None]

Whether the latest key event was a press, repeat or release, or None.

MouseButtonValue module-attribute

MouseButtonValue: Value[MouseButton | None]

The button of the latest button or motion report, or None.

MouseIsDown module-attribute

MouseIsDown: Predicate

Whether any mouse button is held.

MouseKind module-attribute

MouseKind: Value[Literal['press', 'release', 'drag', 'move'] | None]

The kind of the latest button or motion report, or None (also for a wheel report).

MousePosition module-attribute

MousePosition: Value[Point | None]

The cell of the latest button or motion report, or None.

PointerPosition is the last cell the pointer was seen at, whatever came since; this one belongs to the event being handled.

ParentHeight module-attribute

ParentHeight: NumericValue[int]

The height of the parent buffer, in cells.

ParentSize module-attribute

ParentSize: Value[Size]

The size of the parent buffer, or of the buffer a region belongs to.

Evaluating it raises TypeError for a buffer without a parent.

ParentWidth module-attribute

ParentWidth: NumericValue[int]

The width of the parent buffer, in cells.

PointerPosition module-attribute

PointerPosition: Value[Point | None]

The cell under the pointer, or None until the runtime has seen the pointer.

It is the last known cell, held between events. MousePosition is the cell of the event being handled. Reading it needs a mounted runtime.

Position module-attribute

Position: Value[Point]

The position of a buffer or region as a Point.

ScreenBounds module-attribute

ScreenBounds: Value[Rectangle]

The rectangle of a buffer or region on the screen, counting every parent offset.

ScreenPosition module-attribute

ScreenPosition: Value[Point]

The top-left cell of a buffer or region on the screen, counting every parent offset.

ScrollX module-attribute

ScrollX: NumericValue[int]

Horizontal steps of the latest wheel report of the current step (right is positive).

ScrollY module-attribute

ScrollY: NumericValue[int]

Vertical steps of the latest wheel report of the step (down is positive, up is -1).

TimeDelta module-attribute

TimeDelta: NumericValue[int | float]

The elapsed time since the previous runtime frame, in seconds.

TimeDeltaMilliseconds module-attribute

TimeDeltaMilliseconds: NumericValue[int | float]

The elapsed time since the previous runtime frame, in milliseconds.

TimeElapsed module-attribute

TimeElapsed: NumericValue[int | float]

The elapsed runtime time, in seconds.

TimeElapsedMilliseconds module-attribute

TimeElapsedMilliseconds: NumericValue[int | float]

The elapsed runtime time, in milliseconds.

Width module-attribute

The width of a buffer or region, in terminal cells.

X module-attribute

The horizontal position of a buffer or region, in terminal cells.

Y module-attribute

The vertical position of a buffer or region, in terminal cells.

Parent module-attribute

Symbolic expressions for the parent buffer's geometry (Parent.width, Parent.height).

Root module-attribute

Symbolic expressions for the root canvas's geometry and clock (Root.width, Root.time).

BufferExpressions module-attribute

BufferExpressions: BufferExpressionScope = BufferScope

Symbolic expressions for the current buffer's geometry (BufferExpressions.width, BufferExpressions.height) and its rectangle on the screen (BufferExpressions.region).

Keyboard module-attribute

Symbolic expressions for the latest key event (Keyboard.key, Keyboard.kind, Keyboard.chord).

Mouse module-attribute

Symbolic expressions for the latest mouse button or movement report (Mouse.position, Mouse.button, Mouse.kind) and whether a button is held (Mouse.is_down).

Scroll module-attribute

Symbolic expressions for the mouse wheel steps of the current step (Scroll.x, Scroll.y).

BufferExpressionScope

Bases: ExpressionScope

A buffer's own position and size, as expressions.

Attributes:

Examples:

assert BufferScope.width is Width
assert BufferScope.region is BufferRegion

x property

The column in the parent.

y property

The row in the parent.

position property

position: Value[Point]

The position in the parent.

width property

The width in cells.

height property

height: NumericValue[int | float]

The height in cells.

size property

size: Value[Size]

The size in cells.

region property

region: Value[Rectangle]

The rectangle on the screen, counting every parent offset.

Expression

Bases: Generic[ValueType_co]

A value or action described now and evaluated later against a buffer or runtime.

Evaluation is explicit. Using an expression where Python wants a truth value raises TypeError; combine conditions with &, | and ~ instead.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
buffer = Buffer(width=8, height=1)
assert buffer.inspect(width + 2) == 10
assert (width + 2).evaluate(EvaluationContext(buffer=buffer)) == 10

Methods:

  • evaluate –

    Compute the expression's value.

evaluate

evaluate(context: EvaluationContext) -> ValueType_co

Compute the expression's value.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
    """
    Compute the expression's value.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        context = EvaluationContext(buffer=Buffer(width=5, height=1))
        assert (width * 2).evaluate(context) == 10
        ```
    """

KeyboardExpressionScope

Bases: ExpressionScope

The latest key event, as expressions.

Every member has no value while the latest input was not a key event, so a condition on a key is false for mouse, wheel and paste events. Comparing a member with None raises TypeError; compare it with the key you wait for, such as KeyboardScope.key == 'q'.

Attributes:

  • key (Value[str | None]) –

    The key name, such as 'q' or 'enter'.

  • kind (Value[Literal['press', 'repeat', 'release'] | None]) –

    'press', 'repeat' or 'release'.

  • chord (Value[str | None]) –

    The key with its modifiers, such as 'ctrl+s'.

Examples:

with Runtime.offscreen(width=10, height=3) as runtime:
    buffer = Buffer(width=10, height=3)
    runtime.mount(buffer)
    seen = []
    buffer.add_hook(
        lambda context: seen.append('quit'),
        events=('key',),
        condition=KeyboardScope.key == 'q',
    )
    for key in ('w', 'q'):
        runtime.inject(Event.key_press(key))
        runtime.step()
    assert seen == ['quit']
    assert buffer.inspect(KeyboardScope.chord) == 'q'

key property

key: Value[str | None]

The key name of the latest key event.

kind property

kind: Value[Literal['press', 'repeat', 'release'] | None]

Whether the latest key event was a press, repeat or release.

chord property

chord: Value[str | None]

The latest key event with its modifiers, such as 'ctrl+s'.

MouseExpressionScope

Bases: ExpressionScope

The latest mouse button or motion report, as expressions.

position, button and kind describe the event being handled and have no value while the latest input was a key, a paste or a wheel report, so a condition on them is false (comparing with None raises TypeError); is_down is the held state and stays true between events while a button is held. Wheel reports belong to ScrollScope.

Attributes:

Examples:

with Runtime.offscreen(width=10, height=3) as runtime:
    buffer = Buffer(width=4, height=2, x=1, y=1)
    runtime.mount(buffer)
    clicks = []
    buffer.add_hook(
        lambda context: clicks.append('inside'),
        events=('mouse',),
        condition=BufferScope.region.contains(MouseScope.position),
    )
    runtime.inject(Event.mouse_event(column=2, row=2, kind='move'))
    runtime.inject(Event.mouse_event(column=8, row=2, kind='move'))
    runtime.step()
    assert clicks == ['inside']
    assert buffer.inspect(MouseScope.position.x) == 8

position property

position: Value[Point | None]

The cell of the latest button or motion report.

button property

button: Value[MouseButton | None]

The button of the latest button or motion report.

kind property

kind: Value[Literal['press', 'release', 'drag', 'move'] | None]

The kind of the latest button or motion report.

is_down property

is_down: Predicate

Whether any mouse button is held.

NumericValue

Bases: Value[NumberType]

A numeric expression that supports comparisons and arithmetic.

Arithmetic and comparisons build new expressions instead of computing right away.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
buffer = Buffer(width=8, height=3)
assert buffer.inspect(width + 2) == 10
assert buffer.inspect(10 - width) == 2
assert buffer.inspect(width > 4)
assert buffer.inspect(-width) == -8

Methods:

  • evaluate –

    Compute the expression's value.

  • equals –

    A predicate that is true when the value equals other.

  • not_equals –

    A predicate that is true when the value differs from other.

  • contains –

    A condition that is true while this rectangle contains the point other.

  • __eq__ –

    A predicate that is true when the value equals other; the same as equals.

  • __ne__ –

    A predicate that is true when the value differs from other; the same as not_equals.

  • __hash__ –

    The identity hash, which keeps expressions usable in sets and dictionaries.

  • __lt__ –

    A predicate that is true when the value is less than other.

  • __le__ –

    A predicate that is true when the value is at most other.

  • __gt__ –

    A predicate that is true when the value is greater than other.

  • __ge__ –

    A predicate that is true when the value is at least other.

  • __add__ –

    The sum of the value and other.

  • __sub__ –

    The value minus other.

  • __mul__ –

    The product of the value and other.

  • __truediv__ –

    The value divided by other.

  • __mod__ –

    The remainder of dividing the value by other.

  • __neg__ –

    The value with its sign flipped.

  • __rtruediv__ –

    other divided by the value.

  • __rmod__ –

    The remainder of dividing other by the value.

  • __floordiv__ –

    The value divided by other, rounded toward negative infinity like Python's //.

  • __rfloordiv__ –

    other divided by the value, rounded toward negative infinity.

  • __pow__ –

    The value raised to the power other.

  • __rpow__ –

    other raised to the power of the value.

  • __abs__ –

    The absolute value.

  • __floor__ –

    The value rounded down to a whole number, used by math.floor.

  • __ceil__ –

    The value rounded up to a whole number, used by math.ceil.

  • __round__ –

    The value rounded to the nearest whole number, halves away from zero.

  • minimum –

    The smaller of the value and other.

  • maximum –

    The larger of the value and other.

  • clamp –

    The value limited to the range from lowest to highest.

Attributes:

x property

The column of a Point or the left edge of a Rectangle.

Examples:

from xnano_core._xnano_core import Position

assert Buffer(width=4, height=1, x=3, y=2).inspect(Position.x) == 3

y property

The row of a Point or the top edge of a Rectangle.

Examples:

from xnano_core._xnano_core import Position

assert Buffer(width=4, height=1, x=3, y=2).inspect(Position.y) == 2

width property

width: NumericValue[int]

The width of a Size or a Rectangle.

Examples:

from xnano_core._xnano_core import BufferSize

assert Buffer(width=6, height=2).inspect(BufferSize.width) == 6

height property

height: NumericValue[int]

The height of a Size or a Rectangle.

Examples:

from xnano_core._xnano_core import BufferSize

assert Buffer(width=6, height=2).inspect(BufferSize.height) == 2

right property

right: NumericValue[int]

The first column after a Rectangle.

Examples:

from xnano_core._xnano_core import Bounds

assert Buffer(width=6, height=2, x=1).inspect(Bounds.right) == 7

bottom property

bottom: NumericValue[int]

The first row after a Rectangle.

Examples:

from xnano_core._xnano_core import Bounds

assert Buffer(width=6, height=2, y=1).inspect(Bounds.bottom) == 3

evaluate

evaluate(context: EvaluationContext) -> ValueType_co

Compute the expression's value.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
    """
    Compute the expression's value.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        context = EvaluationContext(buffer=Buffer(width=5, height=1))
        assert (width * 2).evaluate(context) == 10
        ```
    """

equals

equals(other: ValueType | Value[ValueType]) -> Predicate

A predicate that is true when the value equals other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width.equals(20))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def equals(self, other: ValueType | Value[ValueType]) -> Predicate:
    """
    A predicate that is true when the value equals ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width.equals(20))
        ```
    """

not_equals

not_equals(other: ValueType | Value[ValueType]) -> Predicate

A predicate that is true when the value differs from other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width.not_equals(5))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def not_equals(self, other: ValueType | Value[ValueType]) -> Predicate:
    """
    A predicate that is true when the value differs from ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width.not_equals(5))
        ```
    """

contains

contains(other: Point | Value[Point] | Value[Point | None]) -> Predicate

A condition that is true while this rectangle contains the point other.

It is false, never an error, while either side has no value yet, such as a mouse position before any mouse event.

Parameters:

Examples:

from xnano_core._xnano_core import Bounds, Point

buffer = Buffer(width=4, height=2)
assert buffer.inspect(Bounds.contains(Point(3, 1)))
assert not buffer.inspect(Bounds.contains(Point(4, 1)))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def contains(
    self: Value[Rectangle],
    other: Point | Value[Point] | Value[Point | None],
) -> Predicate:
    """
    A condition that is true while this rectangle contains the point ``other``.

    It is false, never an error, while either side has no value yet, such as a mouse
    position before any mouse event.

    Args:
        other: The ``Point`` (or an expression that produces one) to look for.

    Examples:
        ```python
        from xnano_core._xnano_core import Bounds, Point

        buffer = Buffer(width=4, height=2)
        assert buffer.inspect(Bounds.contains(Point(3, 1)))
        assert not buffer.inspect(Bounds.contains(Point(4, 1)))
        ```
    """

__eq__

__eq__(other: object) -> Predicate

A predicate that is true when the value equals other; the same as equals.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width == 20)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __eq__(self, other: object) -> Predicate:  # type: ignore[override]  # pyright: ignore[reportIncompatibleMethodOverride]
    """
    A predicate that is true when the value equals ``other``; the same as ``equals``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width == 20)
        ```
    """

__ne__

__ne__(other: object) -> Predicate

A predicate that is true when the value differs from other; the same as not_equals.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width != 5)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __ne__(self, other: object) -> Predicate:  # type: ignore[override]  # pyright: ignore[reportIncompatibleMethodOverride]
    """
    A predicate that is true when the value differs from ``other``; the same as ``not_equals``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width != 5)
        ```
    """

__hash__

__hash__() -> int

The identity hash, which keeps expressions usable in sets and dictionaries.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __hash__(self) -> int:
    """The identity hash, which keeps expressions usable in sets and dictionaries."""

__lt__

A predicate that is true when the value is less than other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=3, height=1).inspect(width < 4)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __lt__(self, other: float | NumericValue[int] | NumericValue[float]) -> Predicate:
    """
    A predicate that is true when the value is less than ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=3, height=1).inspect(width < 4)
        ```
    """

__le__

A predicate that is true when the value is at most other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=4, height=1).inspect(width <= 4)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __le__(self, other: float | NumericValue[int] | NumericValue[float]) -> Predicate:
    """
    A predicate that is true when the value is at most ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=4, height=1).inspect(width <= 4)
        ```
    """

__gt__

A predicate that is true when the value is greater than other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=5, height=1).inspect(width > 4)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __gt__(self, other: float | NumericValue[int] | NumericValue[float]) -> Predicate:
    """
    A predicate that is true when the value is greater than ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=5, height=1).inspect(width > 4)
        ```
    """

__ge__

A predicate that is true when the value is at least other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=4, height=1).inspect(width >= 4)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __ge__(self, other: float | NumericValue[int] | NumericValue[float]) -> Predicate:
    """
    A predicate that is true when the value is at least ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=4, height=1).inspect(width >= 4)
        ```
    """

__add__

The sum of the value and other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=4, height=1).inspect(width + 1) == 5
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __add__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[float]:
    """
    The sum of the value and ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=4, height=1).inspect(width + 1) == 5
        ```
    """

__sub__

The value minus other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=4, height=1).inspect(width - 1) == 3
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __sub__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[float]:
    """
    The value minus ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=4, height=1).inspect(width - 1) == 3
        ```
    """

__mul__

The product of the value and other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
height = _reference('Height', 'numeric_property')
assert Buffer(width=4, height=3).inspect(width * height) == 12
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __mul__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[float]:
    """
    The product of the value and ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        height = _reference('Height', 'numeric_property')
        assert Buffer(width=4, height=3).inspect(width * height) == 12
        ```
    """

__truediv__

__truediv__(other: float | NumericValue[int] | NumericValue[float]) -> NumericValue[float]

The value divided by other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=8, height=1).inspect(width / 2) == 4.0
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __truediv__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[float]:
    """
    The value divided by ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=8, height=1).inspect(width / 2) == 4.0
        ```
    """

__mod__

The remainder of dividing the value by other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=8, height=1).inspect(width % 3) == 2
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __mod__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[float]:
    """
    The remainder of dividing the value by ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=8, height=1).inspect(width % 3) == 2
        ```
    """

__neg__

__neg__() -> NumericValue[NumberType]

The value with its sign flipped.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=8, height=1).inspect(-width) == -8
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __neg__(self) -> NumericValue[NumberType]:
    """
    The value with its sign flipped.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=8, height=1).inspect(-width) == -8
        ```
    """

__rtruediv__

__rtruediv__(other: float) -> NumericValue[float]

other divided by the value.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=8, height=1).inspect(16 / Width) == 2.0
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __rtruediv__(self, other: float) -> NumericValue[float]:
    """
    ``other`` divided by the value.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=8, height=1).inspect(16 / Width) == 2.0
        ```
    """

__rmod__

__rmod__(other: float) -> NumericValue[float]

The remainder of dividing other by the value.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=3, height=1).inspect(10 % Width) == 1
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __rmod__(self, other: float) -> NumericValue[float]:
    """
    The remainder of dividing ``other`` by the value.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=3, height=1).inspect(10 % Width) == 1
        ```
    """

__floordiv__

__floordiv__(other: float | NumericValue[int] | NumericValue[float]) -> NumericValue[int | float]

The value divided by other, rounded toward negative infinity like Python's //.

Dividing by zero raises ZeroDivisionError when the expression is evaluated.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=7, height=1).inspect(Width // 2) == 3
assert Buffer(width=7, height=1).inspect(-Width // 2) == -4
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __floordiv__(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[int | float]:
    """
    The value divided by ``other``, rounded toward negative infinity like Python's ``//``.

    Dividing by zero raises ``ZeroDivisionError`` when the expression is evaluated.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=7, height=1).inspect(Width // 2) == 3
        assert Buffer(width=7, height=1).inspect(-Width // 2) == -4
        ```
    """

__rfloordiv__

__rfloordiv__(other: float) -> NumericValue[int | float]

other divided by the value, rounded toward negative infinity.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=4, height=1).inspect(9 // Width) == 2
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __rfloordiv__(self, other: float) -> NumericValue[int | float]:
    """
    ``other`` divided by the value, rounded toward negative infinity.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=4, height=1).inspect(9 // Width) == 2
        ```
    """

__pow__

__pow__(other: float | NumericValue[int] | NumericValue[float], modulo: None = None) -> NumericValue[int | float]

The value raised to the power other.

Whole-number results stay integers; a negative or fractional exponent gives a float.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=3, height=1).inspect(Width**2) == 9
assert Buffer(width=4, height=1).inspect(Width**0.5) == 2.0
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __pow__(
    self, other: float | NumericValue[int] | NumericValue[float], modulo: None = None
) -> NumericValue[int | float]:
    """
    The value raised to the power ``other``.

    Whole-number results stay integers; a negative or fractional exponent gives a float.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=3, height=1).inspect(Width**2) == 9
        assert Buffer(width=4, height=1).inspect(Width**0.5) == 2.0
        ```
    """

__rpow__

__rpow__(other: float, modulo: None = None) -> NumericValue[int | float]

other raised to the power of the value.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=3, height=1).inspect(2**Width) == 8
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __rpow__(self, other: float, modulo: None = None) -> NumericValue[int | float]:
    """
    ``other`` raised to the power of the value.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=3, height=1).inspect(2**Width) == 8
        ```
    """

__abs__

__abs__() -> NumericValue[NumberType]

The absolute value.

Examples:

from xnano_core._xnano_core import X

assert Buffer(width=1, height=1, x=-5).inspect(abs(X)) == 5
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __abs__(self) -> NumericValue[NumberType]:
    """
    The absolute value.

    Examples:
        ```python
        from xnano_core._xnano_core import X

        assert Buffer(width=1, height=1, x=-5).inspect(abs(X)) == 5
        ```
    """

__floor__

__floor__() -> NumericValue[int]

The value rounded down to a whole number, used by math.floor.

Examples:

import math

from xnano_core._xnano_core import Width

assert Buffer(width=7, height=1).inspect(math.floor(Width / 2)) == 3
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __floor__(self) -> NumericValue[int]:
    """
    The value rounded down to a whole number, used by ``math.floor``.

    Examples:
        ```python
        import math

        from xnano_core._xnano_core import Width

        assert Buffer(width=7, height=1).inspect(math.floor(Width / 2)) == 3
        ```
    """

__ceil__

__ceil__() -> NumericValue[int]

The value rounded up to a whole number, used by math.ceil.

Examples:

import math

from xnano_core._xnano_core import Width

assert Buffer(width=7, height=1).inspect(math.ceil(Width / 2)) == 4
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __ceil__(self) -> NumericValue[int]:
    """
    The value rounded up to a whole number, used by ``math.ceil``.

    Examples:
        ```python
        import math

        from xnano_core._xnano_core import Width

        assert Buffer(width=7, height=1).inspect(math.ceil(Width / 2)) == 4
        ```
    """

__round__

__round__(digits: None = None) -> NumericValue[int]

The value rounded to the nearest whole number, halves away from zero.

Unlike Python's round, a half rounds away from zero, so 2.5 becomes 3. Passing a number of digits raises TypeError.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=5, height=1).inspect(round(Width / 2)) == 3
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __round__(self, digits: None = None) -> NumericValue[int]:
    """
    The value rounded to the nearest whole number, halves away from zero.

    Unlike Python's ``round``, a half rounds away from zero, so ``2.5`` becomes ``3``.
    Passing a number of digits raises ``TypeError``.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=5, height=1).inspect(round(Width / 2)) == 3
        ```
    """

minimum

minimum(other: float | NumericValue[int] | NumericValue[float]) -> NumericValue[NumberType]

The smaller of the value and other.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=20, height=1).inspect(Width.minimum(8)) == 8
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def minimum(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[NumberType]:
    """
    The smaller of the value and ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=20, height=1).inspect(Width.minimum(8)) == 8
        ```
    """

maximum

maximum(other: float | NumericValue[int] | NumericValue[float]) -> NumericValue[NumberType]

The larger of the value and other.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=2, height=1).inspect(Width.maximum(8)) == 8
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def maximum(
    self, other: float | NumericValue[int] | NumericValue[float]
) -> NumericValue[NumberType]:
    """
    The larger of the value and ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=2, height=1).inspect(Width.maximum(8)) == 8
        ```
    """

clamp

clamp(lowest: float | NumericValue[int] | NumericValue[float], highest: float | NumericValue[int] | NumericValue[float]) -> NumericValue[NumberType]

The value limited to the range from lowest to highest.

Raises TypeError when evaluated if lowest is greater than highest.

Examples:

from xnano_core._xnano_core import Width

assert Buffer(width=50, height=1).inspect(Width.clamp(0, 10)) == 10
assert Buffer(width=5, height=1).inspect(Width.clamp(8, 10)) == 8
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def clamp(
    self,
    lowest: float | NumericValue[int] | NumericValue[float],
    highest: float | NumericValue[int] | NumericValue[float],
) -> NumericValue[NumberType]:
    """
    The value limited to the range from ``lowest`` to ``highest``.

    Raises ``TypeError`` when evaluated if ``lowest`` is greater than ``highest``.

    Examples:
        ```python
        from xnano_core._xnano_core import Width

        assert Buffer(width=50, height=1).inspect(Width.clamp(0, 10)) == 10
        assert Buffer(width=5, height=1).inspect(Width.clamp(8, 10)) == 8
        ```
    """

ParentExpressionScope

Bases: ExpressionScope

The geometry of the enclosing buffer, as expressions.

Attributes:

Examples:

assert ParentScope.width is ParentWidth

width property

width: NumericValue[int]

The parent's width in cells.

height property

height: NumericValue[int]

The parent's height in cells.

size property

size: Value[Size]

The parent's size in cells.

RootExpressionScope

Bases: ExpressionScope

The geometry and clock of the root terminal, as expressions.

Attributes:

Examples:

assert RootScope.time is TimeElapsed

width property

The terminal's width in cells.

height property

height: NumericValue[int | float]

The terminal's height in cells.

time property

Seconds elapsed on the terminal's clock.

delta property

Seconds since the previous frame.

fps property

Measured frames per second.

ScrollExpressionScope

Bases: ExpressionScope

The mouse wheel steps of the current step, as expressions.

Each member holds the steps of the latest wheel report of the step; reports of one step do not add up, and a key, paste, button or motion report that follows a wheel report in the same step clears them. Both are 0 when no wheel report arrived in the step. Scrolling up gives a negative y and scrolling left a negative x.

Attributes:

Examples:

with Runtime.offscreen(width=10, height=3) as runtime:
    buffer = Buffer(width=10, height=3)
    runtime.mount(buffer)
    seen = []
    buffer.add_hook(
        lambda context: seen.append('up'),
        events=('mouse',),
        condition=ScrollScope.y < 0,
    )
    runtime.inject(Event.mouse_event(column=1, row=1, kind='scroll', scroll_y=-1))
    runtime.step()
    assert seen == ['up'] and buffer.inspect(ScrollScope.y) == -1

x property

Horizontal steps of the latest wheel report of the current step.

y property

Vertical steps of the latest wheel report of the current step.

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.

Value

Bases: Expression[ValueType]

An expression that evaluates to a value.

== and != build conditions, as equals and not_equals do. A comparison with a value that has not been seen yet, such as the key before any key was pressed, is false (!= included) and never an error. Comparing with the literal None raises TypeError because that condition could never hold; to test that a key arrived, compare with the key itself or register the hook for the 'key' event. A value is hashed by identity, so expressions stay usable in sets and dictionaries. Because == builds a condition, in and .index() on a list or tuple of expressions raise TypeError as soon as they compare an expression with a different item; look an expression up by identity (any(item is value for item in items)) or keep expressions in a set or dictionary. Comparing an expression with an object that cannot be an expression, such as object(), gives False for == and True for !=.

Attributes:

Examples:

from xnano_core._xnano_core import _reference

bounds = _reference('Bounds', 'value')
assert isinstance(bounds, Value)
buffer = Buffer(width=4, height=1)
assert buffer.inspect(bounds.equals(Rectangle(0, 0, 4, 1)))
assert buffer.inspect(bounds == Rectangle(0, 0, 4, 1))

Methods:

  • evaluate –

    Compute the expression's value.

  • equals –

    A predicate that is true when the value equals other.

  • not_equals –

    A predicate that is true when the value differs from other.

  • contains –

    A condition that is true while this rectangle contains the point other.

  • __eq__ –

    A predicate that is true when the value equals other; the same as equals.

  • __ne__ –

    A predicate that is true when the value differs from other; the same as not_equals.

  • __hash__ –

    The identity hash, which keeps expressions usable in sets and dictionaries.

x property

The column of a Point or the left edge of a Rectangle.

Examples:

from xnano_core._xnano_core import Position

assert Buffer(width=4, height=1, x=3, y=2).inspect(Position.x) == 3

y property

The row of a Point or the top edge of a Rectangle.

Examples:

from xnano_core._xnano_core import Position

assert Buffer(width=4, height=1, x=3, y=2).inspect(Position.y) == 2

width property

width: NumericValue[int]

The width of a Size or a Rectangle.

Examples:

from xnano_core._xnano_core import BufferSize

assert Buffer(width=6, height=2).inspect(BufferSize.width) == 6

height property

height: NumericValue[int]

The height of a Size or a Rectangle.

Examples:

from xnano_core._xnano_core import BufferSize

assert Buffer(width=6, height=2).inspect(BufferSize.height) == 2

right property

right: NumericValue[int]

The first column after a Rectangle.

Examples:

from xnano_core._xnano_core import Bounds

assert Buffer(width=6, height=2, x=1).inspect(Bounds.right) == 7

bottom property

bottom: NumericValue[int]

The first row after a Rectangle.

Examples:

from xnano_core._xnano_core import Bounds

assert Buffer(width=6, height=2, y=1).inspect(Bounds.bottom) == 3

evaluate

evaluate(context: EvaluationContext) -> ValueType_co

Compute the expression's value.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
context = EvaluationContext(buffer=Buffer(width=5, height=1))
assert (width * 2).evaluate(context) == 10
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def evaluate(self, context: EvaluationContext) -> ValueType_co:
    """
    Compute the expression's value.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        context = EvaluationContext(buffer=Buffer(width=5, height=1))
        assert (width * 2).evaluate(context) == 10
        ```
    """

equals

equals(other: ValueType | Value[ValueType]) -> Predicate

A predicate that is true when the value equals other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width.equals(20))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def equals(self, other: ValueType | Value[ValueType]) -> Predicate:
    """
    A predicate that is true when the value equals ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width.equals(20))
        ```
    """

not_equals

not_equals(other: ValueType | Value[ValueType]) -> Predicate

A predicate that is true when the value differs from other.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width.not_equals(5))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def not_equals(self, other: ValueType | Value[ValueType]) -> Predicate:
    """
    A predicate that is true when the value differs from ``other``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width.not_equals(5))
        ```
    """

contains

contains(other: Point | Value[Point] | Value[Point | None]) -> Predicate

A condition that is true while this rectangle contains the point other.

It is false, never an error, while either side has no value yet, such as a mouse position before any mouse event.

Parameters:

Examples:

from xnano_core._xnano_core import Bounds, Point

buffer = Buffer(width=4, height=2)
assert buffer.inspect(Bounds.contains(Point(3, 1)))
assert not buffer.inspect(Bounds.contains(Point(4, 1)))
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def contains(
    self: Value[Rectangle],
    other: Point | Value[Point] | Value[Point | None],
) -> Predicate:
    """
    A condition that is true while this rectangle contains the point ``other``.

    It is false, never an error, while either side has no value yet, such as a mouse
    position before any mouse event.

    Args:
        other: The ``Point`` (or an expression that produces one) to look for.

    Examples:
        ```python
        from xnano_core._xnano_core import Bounds, Point

        buffer = Buffer(width=4, height=2)
        assert buffer.inspect(Bounds.contains(Point(3, 1)))
        assert not buffer.inspect(Bounds.contains(Point(4, 1)))
        ```
    """

__eq__

__eq__(other: object) -> Predicate

A predicate that is true when the value equals other; the same as equals.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width == 20)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __eq__(self, other: object) -> Predicate:  # type: ignore[override]  # pyright: ignore[reportIncompatibleMethodOverride]
    """
    A predicate that is true when the value equals ``other``; the same as ``equals``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width == 20)
        ```
    """

__ne__

__ne__(other: object) -> Predicate

A predicate that is true when the value differs from other; the same as not_equals.

Examples:

from xnano_core._xnano_core import _reference

width = _reference('Width', 'numeric_property')
assert Buffer(width=20, height=1).inspect(width != 5)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __ne__(self, other: object) -> Predicate:  # type: ignore[override]  # pyright: ignore[reportIncompatibleMethodOverride]
    """
    A predicate that is true when the value differs from ``other``; the same as ``not_equals``.

    Examples:
        ```python
        from xnano_core._xnano_core import _reference

        width = _reference('Width', 'numeric_property')
        assert Buffer(width=20, height=1).inspect(width != 5)
        ```
    """

__hash__

__hash__() -> int

The identity hash, which keeps expressions usable in sets and dictionaries.

Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def __hash__(self) -> int:
    """The identity hash, which keeps expressions usable in sets and dictionaries."""