Skip to content

xnano.output

xnano.output

xnano.output

What a canvas has drawn, and the text a canvas on the screen held back.

Classes:

  • Output –

    The frames a canvas has drawn, and the text printed while it was on the screen.

Output

Output(canvas: Canvas[object, object])

The frames a canvas has drawn, and the text printed while it was on the screen.

Get it from canvas.output. To print the current frame, use Canvas.render.

Attributes:

  • is_captured (bool) –

    Whether a canvas on the screen is holding back what is printed, so it cannot corrupt the screen.

Examples:

import xnano

with xnano.new(width=12, height=2) as canvas:
    canvas.write('hello', x=1, y=1)
    canvas.output.render()
    assert canvas.output.snapshot().lines()[1] == ' hello'

Create the output's view of a canvas. Use canvas.output instead of calling this.

Parameters:

  • canvas (Canvas[object, object]) –

    The canvas whose frames and held-back text this reads.

Methods:

  • render –

    Draw a frame now and return it.

  • snapshot –

    Return the last frame that was drawn, without drawing.

  • take_captured –

    Return the text printed or logged since the last call, from a canvas on the screen.

Source code in xnano/output.py
def __init__(self, canvas: Canvas[object, object]) -> None:
    """
    Create the output's view of a canvas. Use ``canvas.output`` instead of calling this.

    Args:
        canvas: The canvas whose frames and held-back text this reads.
    """
    self._canvas = canvas

is_captured property

is_captured: bool

Whether a canvas on the screen is holding back what is printed, so it cannot corrupt the screen.

render

render() -> FrameSnapshot

Draw a frame now and return it.

Source code in xnano/output.py
def render(self) -> _core.FrameSnapshot:
    """Draw a frame now and return it."""
    canvas = self._canvas
    canvas._require_open()
    return canvas._host.call(lambda: canvas._runtime.render())

snapshot

snapshot() -> FrameSnapshot

Return the last frame that was drawn, without drawing.

Source code in xnano/output.py
def snapshot(self) -> _core.FrameSnapshot:
    """Return the last frame that was drawn, without drawing."""
    canvas = self._canvas
    canvas._require_open()
    return canvas._host.call(lambda: canvas._runtime.snapshot())

take_captured

take_captured() -> str

Return the text printed or logged since the last call, from a canvas on the screen.

A canvas on the screen holds back anything written to standard output or standard error so it cannot corrupt the screen. Text you do not take is printed when the canvas closes.

Source code in xnano/output.py
def take_captured(self) -> str:
    """
    Return the text printed or logged since the last call, from a canvas on the screen.

    A canvas on the screen holds back anything written to standard output or standard
    error so it cannot corrupt the screen. Text you do not take is printed when the
    canvas closes.
    """
    canvas = self._canvas
    canvas._require_open()
    return canvas._host.call(lambda: canvas._runtime.take_captured_output())