xnano.expressions
xnano.expressions
¶
xnano.expressions
Dynamic properties and values that can be used for mathematical operations.
An Expression stands for a positional, stateful or other
runtime value that changes over time or is otherwise not possible to express in a static
context, such as the width of the parent, the elapsed time or the pointer position.
You can use expressions along with all of Python's standard arithmetic operators, and give
them to anything that takes a number: a position, a size or a range. Comparing one builds a
condition, which a hook takes as its on=. Read the current value of one with
Canvas.evaluate.
import xnano
from xnano.expressions import ParentWidth, TimeElapsed, Width
with xnano.open(width=40, height=3) as terminal:
# An expression keeps the text centered, whatever the width of the window.
terminal.screen.write('centered', x=(ParentWidth - Width) // 2)
# An expression that reads the time moves the ball on every frame.
ball = terminal.screen.write('o', y=2)
ball.x = (ParentWidth - Width) * (TimeElapsed % 2 / 2)
terminal.run(stop_on='q')
Attributes:
-
Expression–The base of every expression: a value, a condition or a change that is worked out again on every frame.
-
Value–An expression with a value that is not a number, such as a key name or a rectangle.
==and!=on it build conditions. -
NumericValue–An expression with a number for a value. Arithmetic on it builds new expressions, and
<,>and their relatives build conditions. -
TargetExpressionScope–The type of
target. -
KeyboardExpressionScope–The type of
keyboard. -
MouseExpressionScope–The type of
mouse. -
ParentExpressionScope–The type of
parent. -
RootExpressionScope–The type of
root. -
ScrollExpressionScope–The type of
scroll. -
X–The column of the buffer inside its parent, in cells.
-
Y–The row of the buffer inside its parent, in cells.
-
Width–The width of the buffer, in cells.
-
Height–The height of the buffer, in cells.
-
Position–The column and row of the buffer inside its parent, as a
Point. -
Size–The width and height of the buffer, as a
Size. -
IsVisible–Whether the buffer is drawn.
-
Focused–Whether the buffer has the keyboard focus.
-
ParentWidth–The width of the parent the buffer is written in, in cells.
-
ParentHeight–The height of the parent the buffer is written in, in cells.
-
ParentSize–The width and height of the parent the buffer is written in, as a
Size. -
TerminalWidth–The width of the terminal area the program draws on, in cells.
-
TerminalHeight–The height of the terminal area the program draws on, in cells.
-
TargetRegion–The rectangle of the screen the buffer covers, as a
Rectangle: where it is drawn after every parent is placed. -
TargetSize–The width and height of the canvas or placement at runtime, as a
Size. -
ScreenPosition–The column and row of the buffer on the screen, as a
Point. -
ScreenBounds–The rectangle of the buffer on the screen, as a
Rectangle. -
TimeElapsed–The seconds the canvas has run, on its own clock.
-
TimeElapsedMilliseconds–The milliseconds the canvas has run, on its own clock.
-
TimeDelta–The seconds between the last frame and the one before it.
-
TimeDeltaMilliseconds–The milliseconds between the last frame and the one before it.
-
FPS–The current runtime's estimated frames per second, measured over recent frames.
-
KeyboardKey–The name of the latest key, such as
'a'or'up'. -
KeyboardKind–Whether the latest key was a
'press', a'repeat'or a'release'. -
KeyboardBinding–The latest key with its modifiers, such as
'ctrl+c'. -
MousePosition–The cell of the mouse report that the canvas received last, as a
Point. -
MouseButtonValue–The button of the latest mouse report, such as
'left'. -
MouseKind–What the latest mouse report was, such as
'press','drag'or'move'. -
MouseIsDown–A condition that holds while a mouse button is held down.
-
PointerPosition–The cell that the pointer was over in the last mouse report, as a
Point. -
ScrollX–The sideways wheel steps of the current step: negative to the left.
-
ScrollY–The wheel steps of the current step: negative upward.
-
parent–Expressions for the parent buffer:
parent.width,parent.heightandparent.size. -
root–Expressions for the root canvas's geometry and clock:
root.width,root.heightandroot.time. -
target–Expressions for the current buffer:
target.width,target.heightand its rectangle on the screen,target.region. -
keyboard–Expressions for the latest key:
keyboard.key,keyboard.kindandkeyboard.binding(the key with its modifiers). -
mouse–Expressions for the latest mouse report:
mouse.position,mouse.button,mouse.kindandmouse.is_down. -
scroll–Expressions for the wheel steps of the current step:
scroll.xandscroll.y.
Expression
module-attribute
¶
Expression = _CoreExpression
The base of every expression: a value, a condition or a change that is worked out again on every frame.
import xnano
from xnano.expressions import Expression, ParentWidth, Width
centered = (ParentWidth - Width) // 2
assert isinstance(centered, Expression)
with xnano.new(width=20, height=2) as canvas:
text = canvas.screen.write('centered', x=centered)
assert text.x == 6
Value
module-attribute
¶
Value = _CoreValue
An expression with a value that is not a number, such as a key name or a rectangle. == and != on it build conditions.
NumericValue
module-attribute
¶
NumericValue = _CoreNumericValue
An expression with a number for a value. Arithmetic on it builds new expressions, and <, > and their relatives build conditions.
import xnano
from xnano.expressions import NumericValue, Width
assert isinstance(Width, NumericValue)
with xnano.new(width=20, height=2) as canvas:
assert canvas.evaluate(Width * 2) == 40
assert canvas.evaluate(Width > 10) is True
TargetExpressionScope
module-attribute
¶
TargetExpressionScope = _CoreBufferExpressionScope
The type of target.
KeyboardExpressionScope
module-attribute
¶
KeyboardExpressionScope = _CoreKeyboardExpressionScope
The type of keyboard.
import xnano
from xnano.events import Event
from xnano.expressions import keyboard, KeyboardExpressionScope
assert isinstance(keyboard, KeyboardExpressionScope)
with xnano.new(width=20, height=2) as canvas:
canvas.push(Event.key_press('a'))
canvas.clock.step(delta_seconds=0.1)
assert canvas.evaluate(keyboard.key) == 'a'
MouseExpressionScope
module-attribute
¶
MouseExpressionScope = _CoreMouseExpressionScope
The type of mouse.
import xnano
from xnano.events import Event
from xnano.expressions import mouse, MouseExpressionScope
from xnano.layout import Point
assert isinstance(mouse, MouseExpressionScope)
with xnano.new(width=20, height=2) as canvas:
canvas.push(Event.mouse_event(column=4, row=1, kind='press', button='left'))
canvas.clock.step(delta_seconds=0.1)
assert canvas.evaluate(mouse.position) == Point(4, 1)
ParentExpressionScope
module-attribute
¶
ParentExpressionScope = _CoreParentExpressionScope
The type of parent.
RootExpressionScope
module-attribute
¶
RootExpressionScope = _CoreRootExpressionScope
The type of root.
ScrollExpressionScope
module-attribute
¶
ScrollExpressionScope = _CoreScrollExpressionScope
The type of scroll.
import xnano
from xnano.events import Event
from xnano.expressions import scroll, ScrollExpressionScope
assert isinstance(scroll, ScrollExpressionScope)
with xnano.new(width=20, height=2) as canvas:
canvas.push(Event.mouse_event(column=0, row=0, kind='scroll', scroll_y=3))
canvas.clock.step(delta_seconds=0.1)
assert canvas.evaluate(scroll.y) == 3
Width
module-attribute
¶
Width = _CoreWidth
Height
module-attribute
¶
Height = _CoreHeight
Position
module-attribute
¶
Position = _CorePosition
The column and row of the buffer inside its parent, as a Point.
import xnano
from xnano.expressions import Position
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'position: {context.canvas.evaluate(Position)}'
terminal.run(stop_on='q')
IsVisible
module-attribute
¶
IsVisible = _CoreIsVisible
Whether the buffer is drawn.
import xnano
from xnano.expressions import IsVisible
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'visible: {context.canvas.evaluate(IsVisible)}'
terminal.run(stop_on='q')
ParentWidth
module-attribute
¶
ParentWidth = _CoreParentWidth
ParentHeight
module-attribute
¶
ParentHeight = _CoreParentHeight
ParentSize
module-attribute
¶
ParentSize = _CoreParentSize
The width and height of the parent the buffer is written in, as a Size.
import xnano
from xnano.expressions import ParentSize
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'parent size: {context.canvas.evaluate(ParentSize)}'
terminal.run(stop_on='q')
TerminalWidth
module-attribute
¶
TerminalWidth = _CoreDeviceWidth
The width of the terminal area the program draws on, in cells.
It is the whole window for a canvas that takes the whole window, the region below the
prompt for a canvas opened with a height, and the size of the canvas itself when the
canvas is not on the screen. Screen.window is the
size of the whole window, whatever the canvas takes of it.
Maps to xnano_core.DeviceWidth.
import xnano
from xnano.expressions import TerminalWidth
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'terminal width: {context.canvas.evaluate(TerminalWidth)}'
terminal.run(stop_on='q')
TerminalHeight
module-attribute
¶
TerminalHeight = _CoreDeviceHeight
The height of the terminal area the program draws on, in cells.
It is the whole window for a canvas that takes the whole window, the region below the
prompt for a canvas opened with a height, and the size of the canvas itself when the
canvas is not on the screen. Screen.window is the
size of the whole window, whatever the canvas takes of it.
Maps to xnano_core.DeviceHeight.
import xnano
from xnano.expressions import TerminalHeight
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'terminal height: {context.canvas.evaluate(TerminalHeight)}'
terminal.run(stop_on='q')
TargetRegion
module-attribute
¶
TargetRegion = _CoreBufferRegion
The rectangle of the screen the buffer covers, as a Rectangle: where it is drawn after every parent is placed.
import xnano
from xnano.expressions import TargetRegion
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'region: {context.canvas.evaluate(TargetRegion)}'
terminal.run(stop_on='q')
TargetSize
module-attribute
¶
TargetSize = _CoreBufferSize
The width and height of the canvas or placement at runtime, as a Size.
import xnano
from xnano.expressions import TargetSize
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'size: {context.canvas.evaluate(TargetSize)}'
terminal.run(stop_on='q')
ScreenPosition
module-attribute
¶
ScreenPosition = _CoreScreenPosition
The column and row of the buffer on the screen, as a Point.
import xnano
from xnano.expressions import ScreenPosition
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'on screen at: {context.canvas.evaluate(ScreenPosition)}'
terminal.run(stop_on='q')
ScreenBounds
module-attribute
¶
ScreenBounds = _CoreScreenBounds
The rectangle of the buffer on the screen, as a Rectangle.
import xnano
from xnano.expressions import ScreenBounds
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'bounds: {context.canvas.evaluate(ScreenBounds)}'
terminal.run(stop_on='q')
TimeElapsed
module-attribute
¶
TimeElapsed = _CoreTimeElapsed
The seconds the canvas has run, on its own clock.
import xnano
from xnano.expressions import ParentWidth, TimeElapsed, Width
with xnano.open(width=60, height=6) as terminal:
ball = terminal.screen.write('o', y=2)
ball.x = (ParentWidth - Width) * (TimeElapsed % 2 / 2)
terminal.run(stop_on='q')
TimeElapsedMilliseconds
module-attribute
¶
TimeElapsedMilliseconds = _CoreTimeElapsedMilliseconds
TimeDelta
module-attribute
¶
TimeDelta = _CoreTimeDelta
The seconds between the last frame and the one before it.
import xnano
from xnano.expressions import TimeDelta
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'last frame took: {context.canvas.evaluate(TimeDelta)}'
terminal.run(stop_on='q')
TimeDeltaMilliseconds
module-attribute
¶
TimeDeltaMilliseconds = _CoreTimeDeltaMilliseconds
The milliseconds between the last frame and the one before it.
import xnano
from xnano.expressions import TimeDeltaMilliseconds
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'last frame took: {context.canvas.evaluate(TimeDeltaMilliseconds)}'
terminal.run(stop_on='q')
FPS
module-attribute
¶
FPS = _CoreFPS
The current runtime's estimated frames per second, measured over recent frames.
KeyboardKey
module-attribute
¶
KeyboardKey = _CoreKeyboardKey
The name of the latest key, such as 'a' or 'up'.
import xnano
from xnano.expressions import KeyboardKey
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('press a key')
@terminal.hook(on='key')
def show(context: xnano.Context) -> None:
label.text = f'key: {context.canvas.evaluate(KeyboardKey)}'
terminal.run(stop_on='q')
KeyboardKind
module-attribute
¶
KeyboardKind = _CoreKeyboardKind
Whether the latest key was a 'press', a 'repeat' or a 'release'.
import xnano
from xnano.expressions import KeyboardKind
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('press a key')
@terminal.hook(on='key')
def show(context: xnano.Context) -> None:
label.text = f'kind: {context.canvas.evaluate(KeyboardKind)}'
terminal.run(stop_on='q')
KeyboardBinding
module-attribute
¶
KeyboardBinding = _CoreKeyboardBinding
The latest key with its modifiers, such as 'ctrl+c'.
import xnano
from xnano.expressions import KeyboardBinding
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('press a key')
@terminal.hook(on='key')
def show(context: xnano.Context) -> None:
label.text = f'binding: {context.canvas.evaluate(KeyboardBinding)}'
terminal.run(stop_on='q')
MousePosition
module-attribute
¶
MousePosition = _CoreMousePosition
The cell of the mouse report that the canvas received last, as a Point.
Use it in a hook for a mouse event, to know where that event happened. It has no value
before the first press, release, drag or move. A key, a paste or a wheel step removes
its value. To follow the pointer, use
PointerPosition, which keeps its value.
import xnano
from xnano.expressions import MousePosition
with xnano.open(width=60, height=6, mouse='motion') as terminal:
label = terminal.screen.write('move or click the mouse')
@terminal.hook(on='mouse')
def show(context: xnano.Context) -> None:
label.text = f'mouse at: {context.canvas.evaluate(MousePosition)}'
terminal.run(stop_on='q')
MouseButtonValue
module-attribute
¶
MouseButtonValue = _CoreMouseButtonValue
The button of the latest mouse report, such as 'left'.
import xnano
from xnano.expressions import MouseButtonValue
with xnano.open(width=60, height=6, mouse='motion') as terminal:
label = terminal.screen.write('move or click the mouse')
@terminal.hook(on='mouse')
def show(context: xnano.Context) -> None:
label.text = f'button: {context.canvas.evaluate(MouseButtonValue)}'
terminal.run(stop_on='q')
MouseKind
module-attribute
¶
MouseKind = _CoreMouseKind
What the latest mouse report was, such as 'press', 'drag' or 'move'.
import xnano
from xnano.expressions import MouseKind
with xnano.open(width=60, height=6, mouse='motion') as terminal:
label = terminal.screen.write('move or click the mouse')
@terminal.hook(on='mouse')
def show(context: xnano.Context) -> None:
label.text = f'kind: {context.canvas.evaluate(MouseKind)}'
terminal.run(stop_on='q')
MouseIsDown
module-attribute
¶
MouseIsDown = _CoreMouseIsDown
A condition that holds while a mouse button is held down.
PointerPosition
module-attribute
¶
PointerPosition = _CorePointerPosition
The cell that the pointer was over in the last mouse report, as a Point.
It has no value before the first mouse report. After that, it keeps its value, also when a key or another event comes after the report. Use it to follow the pointer. A placement whose position reads it is not drawn before the first report. Then it follows the pointer on each frame.
import xnano
from xnano.expressions import PointerPosition
with xnano.open(width=60, height=6, mouse='motion') as terminal:
# The middle of the three cells is on the pointer.
terminal.screen.write('<+>', x=PointerPosition.x - 1, y=PointerPosition.y)
label = terminal.screen.write('move or click the mouse')
@terminal.hook(on='mouse')
def show(context: xnano.Context) -> None:
label.text = f'pointer at: {context.canvas.evaluate(PointerPosition)}'
terminal.run(stop_on='q')
ScrollX
module-attribute
¶
ScrollX = _CoreScrollX
The sideways wheel steps of the current step: negative to the left.
import xnano
from xnano.expressions import ScrollX
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'sideways steps: {context.canvas.evaluate(ScrollX)}'
terminal.run(stop_on='q')
ScrollY
module-attribute
¶
ScrollY = _CoreScrollY
The wheel steps of the current step: negative upward.
parent
module-attribute
¶
parent = _CoreParentScope
root
module-attribute
¶
root = _CoreRootScope
target
module-attribute
¶
target = _CoreBufferScope
Expressions for the current buffer: target.width, target.height and its rectangle on the screen, target.region.
import xnano
from xnano.expressions import target
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'region: {context.canvas.evaluate(target.region)}'
terminal.run(stop_on='q')
keyboard
module-attribute
¶
keyboard = _CoreKeyboardScope
Expressions for the latest key: keyboard.key, keyboard.kind and keyboard.binding (the key with its modifiers).
import xnano
from xnano.expressions import keyboard
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('press a key')
@terminal.hook(on='key')
def show(context: xnano.Context) -> None:
label.text = f'binding: {context.canvas.evaluate(keyboard.binding)}'
terminal.run(stop_on='q')
mouse
module-attribute
¶
mouse = _CoreMouseScope
Expressions for the latest mouse report: mouse.position, mouse.button, mouse.kind and mouse.is_down.
import xnano
from xnano.expressions import mouse
with xnano.open(width=60, height=6, mouse='motion') as terminal:
label = terminal.screen.write('move or click the mouse')
@terminal.hook(on='mouse')
def show(context: xnano.Context) -> None:
label.text = f'mouse at: {context.canvas.evaluate(mouse.position)}'
terminal.run(stop_on='q')
scroll
module-attribute
¶
scroll = _CoreScrollScope
Expressions for the wheel steps of the current step: scroll.x and scroll.y.
import xnano
from xnano.expressions import scroll
with xnano.open(width=60, height=6) as terminal:
label = terminal.screen.write('')
@terminal.hook(on='tick', every=0.5)
def show(context: xnano.Context) -> None:
label.text = f'steps: {context.canvas.evaluate(scroll.y)}'
terminal.run(stop_on='q')
__getattr__
¶
Import an expression from xnano_core the first time it is read, then keep it.
Source code in xnano/expressions.py
def __getattr__(name: str) -> object:
"""Import an expression from ``xnano_core`` the first time it is read, then keep it."""
core_name = _CORE_NAMES.get(name)
if core_name is None:
raise AttributeError(
f'xnano.expressions has no expression called {name!r}. The expressions are '
f'{", ".join(sorted(_CORE_NAMES))}.'
)
import xnano_core
value = getattr(xnano_core, core_name)
globals()[name] = value
return value