xnano.operations
xnano.operations
¶
xnano.operations
Operations that change or animate a buffer, placement or canvas.
Pass operations to update or pipe them with |. Create a reusable hook from a function
with hook, or build typed style, border, block and effect
customizations with fx.
Classes:
-
Customization–Base class for all customization dataclasses returned by
fx. -
ApplyStyle–Patch a
Styleonto every cell of the target. -
Background–Set the background color of every cell of the target.
-
Border–Draw a border around the edge of the target.
-
Clear–Blank cells that hide whatever is behind them.
-
Fill–Fill every cell of the target with one cell.
-
Foreground–Set the foreground color of every cell of the target.
-
Layout–Arrange attached children within the parent's content area.
-
Margin–Horizontal and vertical inset distances.
-
Move–Move the target buffer relative to its current position in its parent.
-
Operation–An action that changes a buffer, applied with
Buffer.update. -
Paste–Copy the cells of another buffer or region into the target.
-
Place–Move the target buffer to an absolute position in its parent.
-
Resize–Resize the target buffer to
widthbyheightcells. -
Write–Write text into the target at a cell position.
-
ContextualOperation–A reusable hook that can be piped onto any canvas or placement.
Functions:
-
fx–Create a typed dataclass customization for a fill, layout,
-
hook–Describe a hook once, to attach to any canvas or placement later.
Attributes:
-
OperationResult(TypeAlias) –What a function decorated with
hookmay return.
OperationResult
module-attribute
¶
OperationResult: TypeAlias = '_core.Operation | _core.Style | Customization | None'
What a function decorated with hook may return.
Customization
¶
Bases: Customization
Base class for all customization dataclasses returned by fx.
Every customization is both a dataclass holding its typed parameters and a native
Customization operation that can be piped with | or
passed to add and update. The parameters are read when it is applied, so editing a
field changes what it does.
Attributes:
-
customization_kind(str) –Identifier of the customization kind (such as
'fill'or'effect.dissolve').
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
kinds–The name of every kind of customization.
-
defaults–The parameter names of a kind with their defaults, in order.
-
build–The core object this customization stands for: a
Style,Border,Effect, -
to_operation–The core operation that applies this customization.
-
to_audio_effect–The audio effect behind an
'audio.*'customization. -
apply_to_surface–Apply this customization to a buffer, widget or region.
-
apply_to–Apply this customization to a canvas, placement or surface.
-
cancel–Stop this effect on the target it was applied to.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
customization_kind
instance-attribute
¶
customization_kind: str
The kind, such as 'style' or 'effect.fade_to'.
parameters
property
¶
The parameters by name, with defaults filled in.
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
kinds
staticmethod
¶
The name of every kind of customization.
Examples:
defaults
staticmethod
¶
The parameter names of a kind with their defaults, in order.
Raises:
-
ValueError–kindis not a customization kind.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
build
¶
build() -> object
The core object this customization stands for: a Style, Border, Effect,
Block, Margin, audio effect and so on, with all of its own methods.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_operation
¶
to_operation() -> Operation
The core operation that applies this customization.
Raises:
-
TypeError–The kind is not applied as an operation, such as
'block'.
to_audio_effect
¶
to_audio_effect() -> object
The audio effect behind an 'audio.*' customization.
Raises:
-
TypeError–The kind is not an audio effect.
apply_to
¶
Apply this customization to a canvas, placement or surface.
It stays attached to the target: after this, assigning one of its fields applies it
again, so a customization from fx is a live handle on what it changed. A running
effect is stopped and started again with the new values.
Parameters:
-
target(Canvas[object, object] | Placement[object, object] | Surface) –The target to apply the customization to.
Source code in xnano/_customize.py
cancel
¶
Stop this effect on the target it was applied to.
Does nothing for an effect that was never applied or has already finished, and for a customization that is not an effect.
Examples:
import xnano
from xnano.operations import fx
with xnano.new('hi', width=4, height=1) as canvas:
fade = fx('effect.fade_to', foreground='red', duration_milliseconds=5000)
fade.apply_to(canvas)
fade.cancel()
fade.cancel()
Source code in xnano/_customize.py
ApplyStyle
¶
ApplyStyle(style: Style)
Bases: Operation
Patch a Style onto every cell of the target.
Colors and modifiers the style leaves unset are kept.
Parameters:
-
style(Style) –The style to patch onto each cell.
Examples:
buffer = Buffer(width=3, height=1)
buffer.update(Write('abc'), ApplyStyle(Style('green', bold=True)))
assert 'bold' in buffer[0, 0].style.modifiers
Create a ApplyStyle.
Parameters:
-
style(Style) –Style.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Background
¶
Background(color: ColorValue)
Bases: Operation
Set the background color of every cell of the target.
Parameters:
-
color(ColorValue) –A
Color, a color name, a hex string or an(red, green, blue)tuple.
Examples:
buffer = Buffer(width=3, height=1)
buffer.update(Background((0, 0, 255)))
assert buffer[0, 0].background.to_rgb() == (0, 0, 255)
Create a Background.
Parameters:
-
color(ColorValue) –ColorValue.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Border
¶
Border(kind: BorderKind = 'plain', *, style: Style | None = None)
Bases: Operation
Draw a border around the edge of the target.
Maps to ratatui::widgets::Block
with BorderType.
Parameters:
-
kind(BorderKind, default:'plain') –'plain','rounded','double'or'thick'. -
style(Style | None, default:None) –The style of the border cells.
Examples:
buffer = Buffer(width=6, height=3)
buffer.update(Border('rounded'))
assert buffer.render(format='text').split('\n')[0] == '╭────╮'
Create a Border.
Parameters:
-
kind(BorderKind, default:'plain') –BorderKind. Defaults to'plain'. -
style(Style | None, default:None) –Style | None. Defaults toNone.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Clear
¶
Clear(*, symbol: str | Expression[str] | None = None, style: Style | DynamicStyle | None = None, width: int | None = None, height: int | None = None, x: int = 0, y: int = 0)
Bases: Widget
Blank cells that hide whatever is behind them.
Maps to ratatui::widgets::Clear.
Parameters:
-
symbol(str | Expression[str] | None, default:None) –The one-column symbol every cell shows. Defaults to a space.
-
style(Style | DynamicStyle | None, default:None) –The style of every cell.
-
width(int | None, default:None) –The widget's width in cells. Defaults to 10.
-
height(int | None, default:None) –The widget's height in cells. Defaults to 3.
-
x(int, default:0) –The column in the parent.
-
y(int, default:0) –The row in the parent.
Raises:
-
ValueError–symbolis not exactly one column wide.
Attributes:
-
symbol(str | Expression[str]) –The symbol used to erase each cell; a space leaves the area blank.
-
style(Style | DynamicStyle) –The colors and text modifiers applied to the cleared cells.
Examples:
clear = Clear(symbol='.', width=4, height=1)
assert clear.render(format='text') == '....'
clear.symbol = '#'
assert clear.render(format='text') == '####'
Create a Clear.
Parameters:
-
symbol(str | Expression[str] | None, default:None) –str | Expression[str] | None. Defaults toNone. -
style(Style | DynamicStyle | None, default:None) –Style | DynamicStyle | None. Defaults toNone. -
width(int | None, default:None) –int | None. Defaults toNone. -
height(int | None, default:None) –int | None. Defaults toNone. -
x(int, default:0) –int. Defaults to0. -
y(int, default:0) –int. Defaults to0.
Methods:
-
__setitem__–Replace one cell with a
Cellor a single grapheme. -
write–Write text starting at a cell.
-
fill–Set every cell of the surface.
-
set_background_colors–Set the background color of a block of cells in one pass.
-
set_foreground_colors–Set the foreground color of a block of cells in one pass.
-
pixels–Return a pixel view that draws into this surface.
-
update–Apply operations in argument order.
-
inspect–Evaluate an expression against this surface and return its value.
-
supports–Return whether the buffer's registry can apply an operation type.
-
add_hook–Run a callback when the runtime reports matching events for this surface.
-
invalidate–Ask the runtime to redraw an area, or the whole surface.
-
to_cell_grid–Pack the surface into the cell grid byte format.
-
__or__–Apply
operationto this surface and return the surface. -
with_text–Create a buffer sized to fit some text.
-
with_size–Create a blank buffer of a given size.
-
with_cells–Create a buffer from cells in row-major order.
-
from_cell_grid–Create a buffer from the cell grid byte format.
-
from_cell_animation–Create a buffer showing one frame of a packed cell animation.
-
region–Return a region covering a rectangle of the buffer.
-
resize–Change the size, keeping the cells that still fit.
-
paste–Copy the cells of another buffer or region into this one.
-
attach–Attach a child buffer as a retained layer of this one.
-
detach–Remove an attached child.
-
__call__–Return a lazy render of the buffer.
-
render–Compose the visible children and serialize the buffer.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
position
property
writable
¶
position: Point
The Point of the buffer in its parent. Assigning moves the buffer.
is_visible
property
writable
¶
is_visible: bool
Whether the buffer is drawn. Hidden buffers still keep their content.
content
property
writable
¶
content: Region
The whole buffer as a region. Assigning text replaces the content.
revision
property
¶
revision: int
A number that increases every time the buffer's cells or geometry change.
registry
property
¶
registry: Registry
The registry that decides which operations this buffer can apply.
symbol
property
writable
¶
symbol: str | Expression[str]
The symbol used to erase each cell; a space leaves the area blank.
style
property
writable
¶
style: Style | DynamicStyle
The colors and text modifiers applied to the cleared cells.
__setitem__
¶
__setitem__(key: Coordinate, value: Cell | str) -> None
write
¶
write(text: TextContent, *, x: int = 0, y: int = 0, style: Style | None = None, mode: WriteMode = 'replace') -> None
Write text starting at a cell.
Text is clipped by display columns, whole graphemes are kept together and the continuation cells of wide characters are repaired.
Parameters:
-
text(TextContent) –A string or styled text.
-
x(int, default:0) –The starting column.
-
y(int, default:0) –The starting row.
-
style(Style | None, default:None) –A style applied to the written cells.
-
mode(WriteMode, default:'replace') –'replace'overwrites the cell;'preserve_background'keeps the background already there.
Examples:
buffer = Buffer(width=6, height=2)
buffer.write('hi', x=1, y=0, style=Style(foreground='red'))
assert buffer[1, 0].foreground == Color('red')
assert buffer.text.split('\n')[0] == ' hi'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
fill
¶
Set every cell of the surface.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
set_background_colors
¶
set_background_colors(colors: Sequence[ColorValue | None], *, width: int, x: int = 0, y: int = 0) -> None
Set the background color of a block of cells in one pass.
Symbols and foreground colors are kept, and the whole block counts as one change (the revision grows by one).
Parameters:
-
colors(Sequence[ColorValue | None]) –A flat, row-major sequence of colors (names,
(r, g, b)tuples orColorvalues).Noneleaves a cell alone. -
width(int) –The number of colors in one row of
colors. -
x(int, default:0) –The column of the block's top-left cell in this surface.
-
y(int, default:0) –The row of the block's top-left cell in this surface.
Raises:
-
ValueError–widthis below 1,len(colors)is not a multiple ofwidth, or the block does not fit inside the surface, or an entry is not a color. -
TypeError–colorsis a single string instead of a sequence of colors, or an entry has no color meaning.
Examples:
buffer = Buffer(width=3, height=2)
buffer.write('abc')
buffer.set_background_colors(['red', None, (0, 0, 255), 'green'], width=2, x=1)
assert buffer[1, 0].background == Color('red')
assert buffer[2, 0].background == Color.reset
assert buffer[1, 1].background == Color('#0000ff')
assert buffer[2, 1].background == Color('green')
assert buffer[1, 0].symbol == 'b'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
set_foreground_colors
¶
set_foreground_colors(colors: Sequence[ColorValue | None], *, width: int, x: int = 0, y: int = 0) -> None
Set the foreground color of a block of cells in one pass.
Symbols and background colors are kept, and the whole block counts as one change (the revision grows by one).
Parameters:
-
colors(Sequence[ColorValue | None]) –A flat, row-major sequence of colors (names,
(r, g, b)tuples orColorvalues).Noneleaves a cell alone. -
width(int) –The number of colors in one row of
colors. -
x(int, default:0) –The column of the block's top-left cell in this surface.
-
y(int, default:0) –The row of the block's top-left cell in this surface.
Raises:
-
ValueError–widthis below 1,len(colors)is not a multiple ofwidth, or the block does not fit inside the surface, or an entry is not a color. -
TypeError–colorsis a single string instead of a sequence of colors, or an entry has no color meaning.
Examples:
buffer = Buffer(width=3, height=2)
buffer.write('abc')
buffer.set_foreground_colors(['red', None, (0, 0, 255), 'green'], width=2, x=1)
assert buffer[1, 0].foreground == Color('red')
assert buffer[2, 0].foreground == Color.reset
assert buffer[1, 1].foreground == Color('#0000ff')
assert buffer[2, 1].foreground == Color('green')
assert buffer[1, 0].symbol == 'b'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
pixels
¶
pixels(*, encoding: PixelEncoding = 'half_blocks') -> PixelView
Return a pixel view that draws into this surface.
Parameters:
-
encoding(PixelEncoding, default:'half_blocks') –'half_blocks'for one column and two rows per cell or'braille'for two columns and four rows per cell.
Examples:
buffer = Buffer(width=3, height=2)
assert (buffer.pixels().width, buffer.pixels().height) == (3, 4)
assert (buffer.pixels(encoding='braille').width) == 6
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
update
¶
update(*operations: Operation) -> None
Apply operations in argument order.
Raises:
-
UnsupportedOperationError–The surface's registry cannot apply an operation.
-
RuntimeRequiredError–An operation needs a mounted buffer on a runtime.
Examples:
buffer = Buffer(width=8, height=3)
buffer.update(Border('rounded'), Write('hi', x=1, y=1))
assert buffer[0, 0].symbol == '╭'
assert buffer[1, 1].symbol == 'h'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
inspect
¶
inspect(expression: Expression[ValueType]) -> ValueType
Evaluate an expression against this surface and return its value.
Examples:
width = _reference('Width', 'numeric_property')
height = _reference('Height', 'numeric_property')
buffer = Buffer(width=8, height=3)
assert buffer.inspect(width) == 8
assert buffer.inspect(width * height) == 24
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
supports
¶
Return whether the buffer's registry can apply an operation type.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
add_hook
¶
add_hook(callback: HookCallback, *, condition: Predicate | None = None, events: Sequence[HookEventKind] = ('tick',), priority: int = 0, once: bool = False) -> Hook
Run a callback when the runtime reports matching events for this surface.
Parameters:
-
callback(HookCallback) –Called with a context for the event.
-
condition(Predicate | None, default:None) –An expression that must be true for the callback to run.
-
events(Sequence[HookEventKind], default:('tick',)) –The event kinds to listen to.
-
priority(int, default:0) –Hooks with higher priority run first.
-
once(bool, default:False) –Whether to remove the hook after its first run.
Returns:
-
Hook–The registered hook, which can be cancelled.
Examples:
keys = []
with Runtime.offscreen(width=10, height=2) as runtime:
buffer = Buffer()
runtime.mount(buffer, geometry='inherit')
buffer.add_hook(lambda context: keys.append(context.event.key.key), events=['key'])
runtime.dispatch(Event.key_press('a'))
runtime.step()
assert keys == ['a']
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
invalidate
¶
invalidate(area: Rectangle | None = None) -> None
Ask the runtime to redraw an area, or the whole surface.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
to_cell_grid
¶
to_cell_grid() -> bytes
Pack the surface into the cell grid byte format.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Apply operation to this surface and return the surface.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
with_text
classmethod
¶
with_text(text: TextContent, *, style: Style | None = None) -> Buffer
Create a buffer sized to fit some text.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
with_size
classmethod
¶
Create a blank buffer of a given size.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
with_cells
classmethod
¶
Create a buffer from cells in row-major order.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
from_cell_grid
classmethod
¶
Create a buffer from the cell grid byte format.
Raises:
-
CellGridError–The bytes are not a valid cell grid.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
from_cell_animation
classmethod
¶
Create a buffer showing one frame of a packed cell animation.
Parameters:
-
packed(bytes) –The cell animation bytes.
-
start_milliseconds(int, default:0) –The point in the animation to show.
Raises:
-
CellGridError–The bytes are not a valid cell animation.
Examples:
frames = [Buffer.with_text('a').to_cell_grid(), Buffer.with_text('b').to_cell_grid()]
packed = pack_cell_animation(frames, frame_durations_milliseconds=[50, 50])
assert Buffer.from_cell_animation(packed, start_milliseconds=60).text == 'b'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
region
¶
resize
¶
Change the size, keeping the cells that still fit.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
paste
¶
Copy the cells of another buffer or region into this one.
Parameters:
-
source(Buffer | Region) –The cells to copy.
-
x(int, default:0) –The destination column of the source's left edge.
-
y(int, default:0) –The destination row of the source's top edge.
-
mode(PasteMode, default:'replace') –'replace'copies every cell,'preserve_background'keeps the destination background and'transparent'skips cells that are marked transparent.
Examples:
sprite = Buffer.with_text('ab')
canvas = Buffer(width=5, height=1)
canvas.paste(sprite, x=2)
assert canvas.text == ' ab'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
attach
¶
attach(child: Buffer, *, geometry: GeometryMode = 'explicit', mode: PasteMode = 'replace') -> None
Attach a child buffer as a retained layer of this one.
The child keeps its own cells and is composited when this buffer is drawn,
ordered by layer and then by attachment order. A child with
geometry='inherit' always fills the parent's content rectangle. The
'transparent' mode skips cells explicitly marked transparent, not spaces.
Raises:
-
ValueError–The child already has a parent.
Examples:
parent = Buffer(width=8, height=3)
child = Buffer('hi', x=2, y=1)
parent.attach(child)
assert parent.children == (child,)
assert parent.render(format='text').split('\n')[1] == ' hi'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
detach
¶
detach(child: Buffer) -> None
__call__
¶
Return a lazy render of the buffer.
str(rendering) serializes the buffer without touching a terminal. Using
the result as a context manager mounts the buffer on runtime (or opens a
live runtime) and unmounts it on exit.
Examples:
buffer = Buffer.with_text('hi')
assert 'hi' in str(buffer())
with Runtime.offscreen(width=8, height=2) as runtime:
with buffer(runtime=runtime) as context:
assert context.runtime is runtime
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
render
¶
render(*, format: RenderFormat = 'ansi') -> str
Compose the visible children and serialize the buffer.
No terminal or device is opened.
Parameters:
-
format(RenderFormat, default:'ansi') –'text'for plain rows or'ansi'for text with styling escape sequences.
Examples:
buffer = Buffer.with_text('hi', style=Style(foreground='red'))
assert buffer.render(format='text') == 'hi'
assert '\x1b[' in buffer.render(format='ansi')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
Fill
¶
Bases: Operation
Fill every cell of the target with one cell.
Parameters:
Examples:
Create a Fill.
Parameters:
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Foreground
¶
Foreground(color: ColorValue)
Bases: Operation
Set the foreground color of every cell of the target.
Parameters:
-
color(ColorValue) –A
Color, a color name, a hex string or an(red, green, blue)tuple.
Examples:
buffer = Buffer(width=3, height=1)
buffer.update(Write('abc'), Foreground('red'))
assert str(buffer[0, 0].foreground) == "Color('red')"
Create a Foreground.
Parameters:
-
color(ColorValue) –ColorValue.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Layout
¶
Layout(arrangement: Literal['row', 'column', 'overlay'] = 'column', *, sizes: Sequence[SizeRule] = (), spacing: int = 0, padding: int = 0)
Bases: Operation
Arrange attached children within the parent's content area.
Overlay children inherit that area unless explicitly positioned. Row and column children default to equal fill; missing size rules never mean zero-sized children. All output is clipped to the authoritative frame.
Maps to ratatui::layout::Layout
for 'row' and 'column'.
Parameters:
-
arrangement(Literal['row', 'column', 'overlay'], default:'column') –'row','column'or'overlay'. -
sizes(Sequence[SizeRule], default:()) –One
SizeRuleper child along the main axis. -
spacing(int, default:0) –Cells between children.
-
padding(int, default:0) –Cells between the parent's edge and its children.
Examples:
parent = Buffer(width=12, height=3)
left, right = Buffer(), Buffer()
parent.attach(left, geometry='inherit')
parent.attach(right, geometry='inherit')
parent.update(
Layout('row', sizes=[SizeRule.cells(4), SizeRule.fill()], spacing=1)
)
assert (left.width, right.x, right.width) == (4, 5, 7)
Create a Layout.
Parameters:
-
arrangement(Literal['row', 'column', 'overlay'], default:'column') –Literal['row', 'column', 'overlay']. Defaults to'column'. -
sizes(Sequence[SizeRule], default:()) –Sequence[SizeRule]. Defaults to(). -
spacing(int, default:0) –int. Defaults to0. -
padding(int, default:0) –int. Defaults to0.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Margin
¶
Margin(horizontal: int | Expression[int | float] = 0, vertical: int | Expression[int | float] = 0)
Horizontal and vertical inset distances.
Attributes:
-
vertical(int) –The number of cells inset from the top and bottom edges.
-
horizontal(int) –The number of cells inset from the left and right edges.
Examples:
Create a Margin.
Parameters:
-
horizontal(int | Expression[int | float], default:0) –int | Expression[int | float]. Defaults to0. -
vertical(int | Expression[int | float], default:0) –int | Expression[int | float]. Defaults to0.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
Move
¶
Bases: Operation
Move the target buffer relative to its current position in its parent.
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:
buffer = Buffer(width=4, height=1, x=2, y=1)
buffer.update(Move(1, 1))
assert (buffer.x, buffer.y) == (3, 2)
buffer.update(Move(x=-2, y=-1))
assert (buffer.x, buffer.y) == (1, 1)
Create a Move.
Parameters:
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Operation
¶
Bases: Expression[None]
An action that changes a buffer, applied with Buffer.update.
Operations apply in argument order. update accepts operations only, not query
expressions. Combine operations with then and when.
Examples:
buffer = Buffer(width=8, height=1)
buffer.update(Write('hi'), Foreground('red'))
assert buffer.text.startswith('hi')
-
API Reference
- xnano
-
xnano_core
-
xnano_core-
AnimateDrawing -
ApplyStatePatch -
ApplyStyle -
Background -
Border -
CancelEffect -
Conditional -
Customization -
Dissolve -
Draw -
Editor -
Effect -
Fade -
Fill -
Foreground -
Fuzz -
Glitch -
Layout -
Liveoperations -
Move -
Operation -
OperationChain -
Paste -
Pixels -
Place -
Raster -
Resize -
SequenceOperation -
SetState -
SetValue -
Write -
WriteCellText
-
-
xnano_core._xnano_core
_xnano_core-
AnimateDrawing -
ApplyStatePatch -
ApplyStyle -
Background -
Border -
CancelEffect -
Conditional -
Customization -
Dissolve -
Draw -
Editor -
Effect -
Fade -
Fill -
Foreground -
Fuzz -
Glitch -
Layout -
Liveoperations -
Move -
Operation -
OperationChain -
Paste -
Pixels -
Place -
Raster -
Resize -
SequenceOperation -
SetState -
SetValue -
Write -
WriteCellText
-
-
-
API Reference
-
xnano
-
xnano -
xnano.buffers
buffersBuffer -
xnano.canvas
canvasCanvas -
xnano.effects
effects -
xnano.operations
operations -
xnano.placement
placementPlacement -
xnano.terminal
terminalTerminal -
xnano.widgets
widgets
-
-
xnano_core
-
xnano_core-
AnimateDrawing -
ApplyStatePatch -
ApplyStyle -
Background -
BarChart -
Block -
Border -
Buffer -
Calendar -
CancelEffect -
Canvas -
Chart -
Clear -
Conditional -
CoreContextupdate -
CustomWidget -
Customizationthen -
Dissolve -
Draw -
Editor -
Effect -
EvaluationContextupdate -
EventContextupdate -
Fade -
Fill -
Foreground -
FrameContextupdate -
Fuzz -
Gauge -
Glitch -
ItemList -
Layout -
LineGauge -
Live -
Move -
Operation -
OperationChain -
Paragraph -
Paste -
Pixels -
Place -
Raster -
Region -
Resize -
Scrollbar -
SequenceOperation -
SetState -
SetValue -
Sparkline -
Surface -
Table -
Tabs -
UpdateContextupdate -
Widget -
Write -
WriteCellText
-
-
xnano_core._xnano_core
_xnano_core-
AnimateDrawing -
ApplyStatePatch -
ApplyStyle -
Background -
BarChart -
Block -
Border -
Buffer -
Calendar -
CancelEffect -
Canvas -
Chart -
Clear -
Conditional -
CustomWidget -
Customizationthen -
Dissolve -
Draw -
Editor -
Effect -
Fade -
Fill -
Foreground -
Fuzz -
Gauge -
Glitch -
ItemList -
Layout -
LineGauge -
Live -
Move -
Operation -
OperationChain -
Paragraph -
Paste -
Pixels -
Place -
Raster -
Region -
Resize -
Scrollbar -
SequenceOperation -
SetState -
SetValue -
Sparkline -
Surface -
Table -
Tabs -
Widget -
Write -
WriteCellText
-
-
xnano_core._xnano_core._core_types
_core_types
-
-
xnano
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Paste
¶
Bases: Operation
Copy the cells of another buffer or region into the target.
The source is snapshotted when the operation is created, so the operation can be applied repeatedly and always copies the same cells.
Parameters:
-
source(Buffer | Region) –The buffer or region to copy from.
-
x(int, default:0) –The destination column of the source's top-left cell.
-
y(int, default:0) –The destination row of the source's top-left cell.
-
mode(WriteMode, default:'replace') –How copied cells combine with the destination.
Examples:
source = Buffer(width=2, height=1)
source.update(Write('ab'))
target = Buffer(width=4, height=1)
target.update(Fill('.'), Paste(source, x=1))
assert target.text == '.ab.'
Create a Paste.
Parameters:
-
source(Buffer | Region) –Buffer | Region. -
x(int, default:0) –int. Defaults to0. -
y(int, default:0) –int. Defaults to0. -
mode(WriteMode, default:'replace') –WriteMode. Defaults to'replace'.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Place
¶
Bases: Operation
Move the target buffer to an absolute position in its parent.
Parameters:
Examples:
Create a Place.
Parameters:
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Resize
¶
Bases: Operation
Resize the target buffer to width by height cells.
Existing content is kept where it still fits and cleared where the buffer grows.
Parameters:
Examples:
buffer = Buffer(width=10, height=3)
buffer.update(Resize(6, 2))
assert (buffer.width, buffer.height) == (6, 2)
Create a Resize.
Parameters:
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
Write
¶
Write(text: TextContent, *, x: int = 0, y: int = 0, style: Style | None = None, mode: WriteMode = 'replace')
Bases: Operation
Write text into the target at a cell position.
Text is clipped by display columns, whole graphemes are preserved and wide characters repair their continuation cells.
Parameters:
-
text(TextContent) –A string, a
StyledTextor a sequence of both. -
x(int, default:0) –The first column to write to.
-
y(int, default:0) –The row to write to.
-
style(Style | None, default:None) –The style applied to the written cells.
-
mode(WriteMode, default:'replace') –'replace'overwrites the cell style;'preserve_background'keeps the background that is already in the cell.
Examples:
buffer = Buffer(width=8, height=2)
buffer.update(Write('hi', x=1, y=1, style=Style('red')))
assert buffer.render(format='text').split('\n')[1].strip() == 'hi'
assert buffer[1, 1].foreground.to_rgb() is None
assert str(buffer[1, 1].foreground) == "Color('red')"
painted = Buffer(width=3, height=1)
painted.update(Write('a', style=Style('red', 'blue')))
painted.update(Write('b', mode='preserve_background', style=Style('green')))
assert str(painted[0, 0].background) == "Color('blue')"
Create a Write.
Parameters:
-
text(TextContent) –TextContent. -
x(int, default:0) –int. Defaults to0. -
y(int, default:0) –int. Defaults to0. -
style(Style | None, default:None) –Style | None. Defaults toNone. -
mode(WriteMode, default:'replace') –WriteMode. Defaults to'replace'.
Methods:
-
evaluate–Compute the expression's value.
-
then–An operation that applies this operation, then
other. -
when–An operation that applies only while
conditionis true. -
__or__–Combine this operation with
otherso both run in sequence.
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
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
then
¶
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
__or__
¶
ContextualOperation
¶
ContextualOperation(callback: Callable[[Context[DepsT, StateT]], object] | None = None, *, on: HookKind | None = None, every: float | None = None, after: float | None = None, keys: tuple[str, ...] = (), when: When[DepsT, StateT] | None = None, requires_deps: bool = False, steps: Sequence[Operation] = ())
Bases: Operation, Generic[DepsT, StateT]
A reusable hook that can be piped onto any canvas or placement.
Build one with hook. Apply it with |, |= or update,
and it attaches the same hook that target.hook(function, ...) would.
Attributes:
-
on(HookKind | None) –When the hook runs, or
Nonefor every input event. -
every(float | None) –Seconds (
'tick') or beats ('beat') between runs, orNone. -
after(float | None) –Delay before the one run, or
None. -
keys(tuple[str, ...]) –Key names an
on='key'hook reacts to.
Examples:
import xnano
counted = []
@xnano.hook(on='tick', every=0.5)
def count(context: xnano.Context) -> None:
counted.append(context.time.elapsed)
with xnano.new(width=16, height=1) as canvas:
canvas | count
canvas.step(delta_seconds=1.0)
assert len(counted) == 2
Create a reusable hook. Use hook instead of calling this.
Parameters:
-
callback(Callable[[Context[DepsT, StateT]], object] | None, default:None) –Function called with a
Contextwhen the hook runs. -
on(HookKind | None, default:None) –What the hook listens to.
-
every(float | None, default:None) –Interval between runs.
-
after(float | None, default:None) –Delay before the one run.
-
keys(tuple[str, ...], default:()) –Key names to react to when
on='key'. -
when(When[DepsT, StateT] | None, default:None) –A condition that must hold.
-
requires_deps(bool, default:False) –Whether the hook needs the canvas's dependencies.
-
steps(Sequence[Operation], default:()) –Chained operations to apply in sequence.
Methods:
-
then–Return a new operation that applies this operation and then
other. -
__or__–Combine this operation with another operation or style.
-
__ror__–Apply this operation to
targetwhen piped withtarget | operation. -
apply_to_surface–Apply the chained steps of this operation to a core
Surface. -
apply_to–Attach this hook to
target, or apply its chained steps. -
evaluate–Compute the expression's value.
-
when–An operation that applies only while
conditionis true.
Source code in xnano/operations.py
then
¶
then(other: Operation) -> ContextualOperation[DepsT, StateT]
Return a new operation that applies this operation and then other.
Parameters:
-
other(Operation) –The next operation to run.
Source code in xnano/operations.py
__or__
¶
__or__(other: Operation | Style) -> ContextualOperation[DepsT, StateT]
Combine this operation with another operation or style.
Source code in xnano/operations.py
__ror__
¶
Apply this operation to target when piped with target | operation.
Source code in xnano/operations.py
apply_to_surface
¶
apply_to_surface(surface: Surface) -> None
Apply the chained steps of this operation to a core Surface.
A hook has no surface to listen on, so only its chained steps apply.
Parameters:
-
surface(Surface) –The buffer or region to modify.
Source code in xnano/operations.py
apply_to
¶
Attach this hook to target, or apply its chained steps.
Parameters:
Source code in xnano/operations.py
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
when
¶
An operation that applies only while condition is true.
Examples:
from xnano_core._xnano_core import _reference
width = _reference('Width', 'numeric_property')
buffer = Buffer(width=6, height=1)
buffer.update(Write('wide').when(width > 4))
assert buffer.text.startswith('wide')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
fx
¶
fx(kind: Literal['fill'], /, **parameters: Unpack[FillArguments]) -> FillCustomization
fx(kind: Literal['write'], /, **parameters: Unpack[WriteArguments]) -> WriteCustomization
fx(kind: Literal['layout'], /, **parameters: Unpack[LayoutArguments]) -> LayoutCustomization
fx(kind: Literal['fuzz'], /, **parameters: Unpack[FuzzArguments]) -> FuzzCustomization
fx(kind: Literal['pixels'], /, **parameters: Unpack[PixelsArguments]) -> PixelsCustomization
fx(kind: Literal['raster'], /, **parameters: Unpack[RasterArguments]) -> RasterCustomization
fx(kind: Literal['effect.fade_to'], /, **parameters: Unpack[FadeToEffectArguments]) -> FadeToEffectCustomization
fx(kind: Literal['effect.fade_from'], /, **parameters: Unpack[FadeFromEffectArguments]) -> FadeFromEffectCustomization
fx(kind: Literal['effect.paint'], /, **parameters: Unpack[PaintEffectArguments]) -> PaintEffectCustomization
fx(kind: Literal['effect.saturate'], /, **parameters: Unpack[SaturateEffectArguments]) -> SaturateEffectCustomization
fx(kind: Literal['effect.lighten'], /, **parameters: Unpack[LightenEffectArguments]) -> LightenEffectCustomization
fx(kind: Literal['effect.darken'], /, **parameters: Unpack[DarkenEffectArguments]) -> DarkenEffectCustomization
fx(kind: Literal['effect.sweep_in'], /, **parameters: Unpack[SweepInEffectArguments]) -> SweepInEffectCustomization
fx(kind: Literal['effect.sweep_out'], /, **parameters: Unpack[SweepOutEffectArguments]) -> SweepOutEffectCustomization
fx(kind: Literal['effect.slide_in'], /, **parameters: Unpack[SlideInEffectArguments]) -> SlideInEffectCustomization
fx(kind: Literal['effect.slide_out'], /, **parameters: Unpack[SlideOutEffectArguments]) -> SlideOutEffectCustomization
fx(kind: Literal['effect.hsl_shift'], /, **parameters: Unpack[HslShiftEffectArguments]) -> HslShiftEffectCustomization
fx(kind: Literal['effect.dissolve'], /, **parameters: Unpack[DissolveEffectArguments]) -> DissolveEffectCustomization
fx(kind: Literal['effect.coalesce'], /, **parameters: Unpack[CoalesceEffectArguments]) -> CoalesceEffectCustomization
fx(kind: Literal['effect.glitch'], /, **parameters: Unpack[GlitchEffectArguments]) -> GlitchEffectCustomization
fx(kind: Literal['effect.sleep'], /, **parameters: Unpack[SleepEffectArguments]) -> SleepEffectCustomization
fx(kind: Literal['effect.evolve'], /, **parameters: Unpack[EvolveEffectArguments]) -> EvolveEffectCustomization
fx(kind: Literal['effect.evolve_into'], /, **parameters: Unpack[EvolveIntoEffectArguments]) -> EvolveIntoEffectCustomization
fx(kind: Literal['effect.evolve_from'], /, **parameters: Unpack[EvolveFromEffectArguments]) -> EvolveFromEffectCustomization
fx(kind: Literal['effect.expand'], /, **parameters: Unpack[ExpandEffectArguments]) -> ExpandEffectCustomization
fx(kind: Literal['effect.stretch'], /, **parameters: Unpack[StretchEffectArguments]) -> StretchEffectCustomization
fx(kind: Literal['effect.explode'], /, **parameters: Unpack[ExplodeEffectArguments]) -> ExplodeEffectCustomization
fx(kind: Literal['audio.bit_crush'], /, **parameters: Unpack[BitCrushAudioArguments]) -> BitCrushAudioCustomization
fx(kind: Literal['audio.distortion'], /, **parameters: Unpack[DistortionAudioArguments]) -> DistortionAudioCustomization
fx(kind: Literal['audio.low_pass'], /, **parameters: Unpack[LowPassAudioArguments]) -> LowPassAudioCustomization
fx(kind: Literal['audio.high_pass'], /, **parameters: Unpack[HighPassAudioArguments]) -> HighPassAudioCustomization
fx(kind: Literal['audio.delay'], /, **parameters: Unpack[DelayAudioArguments]) -> DelayAudioCustomization
fx(kind: Literal['audio.reverb'], /, **parameters: Unpack[ReverbAudioArguments]) -> ReverbAudioCustomization
fx(kind: Literal['audio.tremolo'], /, **parameters: Unpack[TremoloAudioArguments]) -> TremoloAudioCustomization
fx(kind: CustomizationKind, /, **parameters: object) -> Customization
Create a typed dataclass customization for a fill, layout,
pixels, visual effect ('effect.<name>') or audio effect ('audio.<name>').
Each kind overloads the keyword arguments with Unpack over its corresponding
TypedDict and returns the matching Customization
dataclass. The returned dataclass can be inspected, mutated, returned from an
hook, piped with |, or passed to add and
update.
Parameters:
-
kind(CustomizationKind) –Which customization to create (such as
'fill','layout','pixels','effect.dissolve','effect.fade_to'or'audio.reverb'). -
parameters(object, default:{}) –Keyword arguments for the chosen customization kind.
Raises:
-
ValueError–kindis not a recognized customization kind. -
TypeError–A keyword is not a setting of that kind. The message lists its settings.
Examples:
import xnano
from xnano.operations import fx
sparkle = fx('fill', cell='*')
assert sparkle.cell == '*'
fade = fx('effect.fade_to', foreground='red', duration_milliseconds=200)
assert fade.duration_milliseconds == 200
with xnano.new(width=10, height=3) as canvas:
canvas | sparkle
assert '*' in canvas.render()
Source code in xnano/_customize.py
hook
¶
hook(function: Hook[DepsT, StateT]) -> ContextualOperation[DepsT, StateT]
hook(function: Hook[DepsT, StateT] | None = None, /, *, on: HookKind | None = None, when: When[DepsT, StateT] | None = None, every: float | None = None, after: float | None = None, keys: KeyName | Sequence[KeyName] | None = None, requires_deps: bool = False) -> ContextualOperation[DepsT, StateT] | Callable[[Hook[DepsT, StateT]], ContextualOperation[DepsT, StateT]]
Describe a hook once, to attach to any canvas or placement later.
It takes the same arguments as target.hook(...) and returns an operation: pipe it onto
a target with target | operation, or pass it to update. Each target gets its own
hook. The function may return an operation, a style or an effect, which is applied to the
target it runs on.
Parameters:
-
function(Hook[DepsT, StateT] | None, default:None) –The function to wrap when
@xnano.hookis used without parentheses: a function of theContext, or one with no parameters. -
on(HookKind | None, default:None) –What the hook listens to; see
HookKind. -
when(When[DepsT, StateT] | None, default:None) –A condition that must hold; see
When. -
every(float | None, default:None) –Repeat every this many seconds (
'tick') or beats ('beat'). -
after(float | None, default:None) –Run once after this many seconds (
'tick') or beats ('beat'). -
keys(KeyName | Sequence[KeyName] | None, default:None) –The key or keys an
on='key'hook reacts to. -
requires_deps(bool, default:False) –Whether the hook needs the dependencies given to
new(deps=...).
Raises:
-
TypeError–functioncannot be called with a context,keysis not a key name or a list of them, orwhenis not a condition. -
ValueError–on,every,after,keysorwhenis not valid, or they do not fit together.
Examples:
import xnano
readings = []
@xnano.hook(on='frame')
def clock_readout(context: xnano.Context) -> None:
readings.append(round(context.time.elapsed, 1))
with xnano.new(width=12, height=1) as canvas:
canvas | clock_readout
canvas.step(delta_seconds=0.5)
assert readings == [0.5]
Source code in xnano/operations.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |