Skip to content

xnano.components.component

xnano.components.component

xnano.components.component


Create custom components and compose them from public content primitives.

Classes:

  • Bars

    Render grouped bars.

  • Canvas

    Render geometric shapes in numeric coordinate space.

  • CellCanvas

    A rectangular sequence of styled cell rows.

  • Clear

    Clear the assigned render area.

  • Gauge

    Render a filled progress gauge.

  • Items

    Render a selectable list of text items.

  • LineGauge

    Render progress as a single horizontal line.

  • Panel

    Decorate child content with a frame and padding.

  • Plot

    Render one or more numeric data series.

  • Run

    One styled text run.

  • Sparkline

    Render a compact sequence of vertical samples.

  • Stack

    Lay out child content in one direction.

  • TableGrid

    Render tabular rows with optional selection.

  • TextBlock

    Wrapped plain text or lines of styled runs.

  • ScrollbarContent

    Render scroll position and viewport proportion.

  • ComponentRenderContext

    Render-time scope passed into component paint hooks.

  • Component

    Base class for custom xnano components.

Attributes:

Content module-attribute

Any content primitive.

Bars dataclass

Bars(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    groups: tuple[BarGroup, ...] = (),
    bar_width: int = 1,
    bar_gap: int = 1,
    group_gap: int = 0,
    max_value: int | None = None,
    direction: Direction = "vertical",
    color: ColorLike | None = None,
    value_color: ColorLike | None = None,
    label_color: ColorLike | None = None
)

Bases: ContentBase

Render grouped bars.

Example

Bars(groups=(BarGroup(bars=(Bar(value=8, label="A"),)),))

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

groups class-attribute instance-attribute

groups: tuple[BarGroup, ...] = ()

Bar groups.

bar_width class-attribute instance-attribute

bar_width: int = 1

Width of each bar in cells.

bar_gap class-attribute instance-attribute

bar_gap: int = 1

Cells between bars.

group_gap class-attribute instance-attribute

group_gap: int = 0

Cells between groups.

max_value class-attribute instance-attribute

max_value: int | None = None

Explicit value ceiling.

direction class-attribute instance-attribute

direction: Direction = 'vertical'

Bar growth direction.

color class-attribute instance-attribute

color: ColorLike | None = None

Default bar color.

value_color class-attribute instance-attribute

value_color: ColorLike | None = None

Default value-label color.

label_color class-attribute instance-attribute

label_color: ColorLike | None = None

Bar-label color.

Canvas dataclass

Canvas(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    shapes: tuple[CanvasShape, ...] = (),
    x_bounds: tuple[float, float] = (0.0, 1.0),
    y_bounds: tuple[float, float] = (0.0, 1.0),
    marker: CanvasMarkerLike = "braille",
    background: ColorLike | None = None
)

Bases: ContentBase

Render geometric shapes in numeric coordinate space.

Example

Canvas(shapes=(CanvasCircle(x=0.5, y=0.5, radius=0.25),))

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

shapes class-attribute instance-attribute

shapes: tuple[CanvasShape, ...] = ()

Shapes painted in declaration order.

x_bounds class-attribute instance-attribute

x_bounds: tuple[float, float] = (0.0, 1.0)

Horizontal coordinate bounds.

y_bounds class-attribute instance-attribute

y_bounds: tuple[float, float] = (0.0, 1.0)

Vertical coordinate bounds.

marker class-attribute instance-attribute

marker: CanvasMarkerLike = 'braille'

Canvas point marker.

background class-attribute instance-attribute

background: ColorLike | None = None

Canvas background.

CellCanvas dataclass

CellCanvas(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    rows: tuple[tuple[CellSpan, ...], ...] = (),
    width: int = 0,
    height: int = 0
)

Bases: ContentBase

A rectangular sequence of styled cell rows.

Example

CellCanvas.from_rows(((CellSpan("OK", color="green"),),))

Attributes:

Methods:

  • from_rows

    Create a cell canvas from styled span rows.

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

rows class-attribute instance-attribute

rows: tuple[tuple[CellSpan, ...], ...] = ()

Rows of styled spans.

width class-attribute instance-attribute

width: int = 0

Canvas width in cells.

height class-attribute instance-attribute

height: int = 0

Canvas height in cells.

from_rows classmethod

from_rows(
    rows: Sequence[Sequence[CellSpan | str]],
    *,
    width: int | None = None,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True
) -> "CellCanvas"

Create a cell canvas from styled span rows.

Source code in xnano/core/content.py
@classmethod
def from_rows(
    cls,
    rows: Sequence[Sequence[CellSpan | str]],
    *,
    width: int | None = None,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
) -> "CellCanvas":
    """Create a cell canvas from styled span rows."""
    normalized = tuple(
        tuple(
            span if isinstance(span, CellSpan) else CellSpan(text=span)
            for span in row
        )
        for row in rows
    )
    measured_width = max(
        (sum(len(span.text) for span in row) for row in normalized),
        default=0,
    )
    return cls(
        rows=normalized,
        width=measured_width if width is None else width,
        height=len(normalized),
        style=style,
        z=z,
        visible=visible,
    )

Clear dataclass

Clear(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True
)

Bases: ContentBase

Clear the assigned render area.

Attributes:

  • style (Style | None) –

    Optional shared style.

  • z (int) –

    Sibling-local paint order.

  • visible (bool) –

    Whether this content paints.

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

Gauge dataclass

Gauge(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    progress: float = 0.0,
    label: str | None = None,
    foreground: ColorLike = "green",
    background: ColorLike | None = None
)

Bases: ContentBase

Render a filled progress gauge.

Example

Gauge(progress=0.75, label="75%")

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

progress class-attribute instance-attribute

progress: float = 0.0

Completion ratio from zero to one.

label class-attribute instance-attribute

label: str | None = None

Text displayed inside the gauge.

foreground class-attribute instance-attribute

foreground: ColorLike = 'green'

Filled-region color.

background class-attribute instance-attribute

background: ColorLike | None = None

Gauge background color.

Items dataclass

Items(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    items: tuple[str | Run | TextBlock, ...] = (),
    selected: int | None = None,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    highlight_color: ColorLike = "black",
    highlight_background: ColorLike = "white",
    highlight_symbol: str = "> "
)

Bases: ContentBase

Render a selectable list of text items.

Example

Items(items=("One", "Two"), selected=0)

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

items class-attribute instance-attribute

items: tuple[str | Run | TextBlock, ...] = ()

List entries.

selected class-attribute instance-attribute

selected: int | None = None

Selected entry index.

foreground class-attribute instance-attribute

foreground: ColorLike | None = None

Default foreground.

background class-attribute instance-attribute

background: ColorLike | None = None

Default background.

highlight_color class-attribute instance-attribute

highlight_color: ColorLike = 'black'

Selected-entry foreground.

highlight_background class-attribute instance-attribute

highlight_background: ColorLike = 'white'

Selected-entry background.

highlight_symbol class-attribute instance-attribute

highlight_symbol: str = '> '

Symbol prefixed to the selected entry.

LineGauge dataclass

LineGauge(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    progress: float = 0.0,
    label: str | None = None,
    foreground: ColorLike | None = None,
    filled_color: ColorLike | None = None,
    unfilled_color: ColorLike | None = None,
    background: ColorLike | None = None
)

Bases: ContentBase

Render progress as a single horizontal line.

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

progress class-attribute instance-attribute

progress: float = 0.0

Completion ratio from zero to one.

label class-attribute instance-attribute

label: str | None = None

Text displayed with the line.

foreground class-attribute instance-attribute

foreground: ColorLike | None = None

Default foreground color.

filled_color class-attribute instance-attribute

filled_color: ColorLike | None = None

Completed-region color.

unfilled_color class-attribute instance-attribute

unfilled_color: ColorLike | None = None

Remaining-region color.

background class-attribute instance-attribute

background: ColorLike | None = None

Line background color.

Panel dataclass

Panel(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    child: ContentBase,
    title: str | None = None,
    title_position: FrameTitlePosition | None = None,
    border: Border | None = None,
    border_color: ColorLike | None = None,
    border_sides: tuple[Side, ...] | None = None,
    background: ColorLike | None = None,
    padding: PaddingLike | None = None
)

Bases: ContentBase

Decorate child content with a frame and padding.

Example

Panel(child=TextBlock(text="Status"), title="Service")

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

child instance-attribute

child: ContentBase

Content inside the panel.

title class-attribute instance-attribute

title: str | None = None

Optional frame title.

title_position class-attribute instance-attribute

title_position: FrameTitlePosition | None = None

Frame title alignment.

border class-attribute instance-attribute

border: Border | None = None

Border style.

border_color class-attribute instance-attribute

border_color: ColorLike | None = None

Border foreground color.

border_sides class-attribute instance-attribute

border_sides: tuple[Side, ...] | None = None

Visible border sides.

background class-attribute instance-attribute

background: ColorLike | None = None

Panel background color.

padding class-attribute instance-attribute

padding: PaddingLike | None = None

Space between border and child.

Plot dataclass

Plot(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    datasets: tuple[PlotDataset, ...] = (),
    x_axis: PlotAxis | None = None,
    y_axis: PlotAxis | None = None,
    color: ColorLike | None = None,
    legend: bool = True,
    legend_position: LegendPositionLike | None = "top_right"
)

Bases: ContentBase

Render one or more numeric data series.

Example

Plot(datasets=(PlotDataset(data=((0, 1), (1, 3))),))

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

datasets class-attribute instance-attribute

datasets: tuple[PlotDataset, ...] = ()

Data series.

x_axis class-attribute instance-attribute

x_axis: PlotAxis | None = None

Horizontal axis.

y_axis class-attribute instance-attribute

y_axis: PlotAxis | None = None

Vertical axis.

color class-attribute instance-attribute

color: ColorLike | None = None

Default plot color.

legend class-attribute instance-attribute

legend: bool = True

Whether to show the legend.

legend_position class-attribute instance-attribute

legend_position: LegendPositionLike | None = 'top_right'

Legend placement.

Run dataclass

Run(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    text: str,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: tuple[CharacterModifier, ...] = (),
    color: InitVar[Any] = _ALIAS_UNSET
)

Bases: ContentBase

One styled text run.

Example

Run(text="Ready", foreground="green", modifiers=("bold",))

Attributes:

Methods:

  • __post_init__

    Apply the deprecated constructor alias.

  • plain

    Create a plain styled run.

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

text instance-attribute

text: str

Text displayed by the run.

foreground class-attribute instance-attribute

foreground: ColorLike | None = None

Foreground color.

background class-attribute instance-attribute

background: ColorLike | None = None

Background color.

modifiers class-attribute instance-attribute

modifiers: tuple[CharacterModifier, ...] = ()

Character modifiers.

__post_init__

__post_init__(color: Any) -> None

Apply the deprecated constructor alias.

Source code in xnano/core/content.py
def __post_init__(self, color: Any) -> None:
    """Apply the deprecated constructor alias."""
    resolve_init_alias(self, color, old="color", new="foreground")

plain classmethod

plain(
    text: str,
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None
) -> "Run"

Create a plain styled run.

Parameters:

  • text (str) –

    Text displayed by the run.

  • foreground (ColorLike | None, default: None ) –

    Foreground color.

  • background (ColorLike | None, default: None ) –

    Background color.

  • modifiers (Sequence[CharacterModifier] | None, default: None ) –

    Character modifiers.

  • style (Style | None, default: None ) –

    Optional shared style.

  • z (int, default: 0 ) –

    Sibling-local paint order.

  • visible (bool, default: True ) –

    Whether the run paints.

  • color (ColorLike | None, default: None ) –

    Deprecated alias for foreground.

Returns:

  • 'Run'

    A run containing text.

Source code in xnano/core/content.py
@classmethod
def plain(
    cls,
    text: str,
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None,
) -> "Run":
    """Create a plain styled run.

    Args:
        text: Text displayed by the run.
        foreground: Foreground color.
        background: Background color.
        modifiers: Character modifiers.
        style: Optional shared style.
        z: Sibling-local paint order.
        visible: Whether the run paints.
        color: Deprecated alias for ``foreground``.

    Returns:
        A run containing ``text``.
    """
    foreground = resolve_color_alias(foreground, color, stacklevel=3)
    return cls(
        text=text,
        foreground=foreground,
        background=background,
        modifiers=tuple(modifiers or ()),
        style=style,
        z=z,
        visible=visible,
    )

Sparkline dataclass

Sparkline(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    data: tuple[int, ...] = (),
    bars: tuple[SparklineBar, ...] | None = None,
    max_value: int | None = None,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    absent_value_color: ColorLike | None = None,
    absent_value_symbol: str | None = None
)

Bases: ContentBase

Render a compact sequence of vertical samples.

Example

Sparkline(data=(2, 5, 3, 8), foreground="cyan")

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

data class-attribute instance-attribute

data: tuple[int, ...] = ()

Sample values.

bars class-attribute instance-attribute

bars: tuple[SparklineBar, ...] | None = None

Individually styled samples.

max_value class-attribute instance-attribute

max_value: int | None = None

Explicit sample ceiling.

foreground class-attribute instance-attribute

foreground: ColorLike | None = None

Default sample color.

background class-attribute instance-attribute

background: ColorLike | None = None

Sparkline background.

absent_value_color class-attribute instance-attribute

absent_value_color: ColorLike | None = None

Color for missing or zero samples.

absent_value_symbol class-attribute instance-attribute

absent_value_symbol: str | None = None

Symbol for missing or zero samples.

Stack dataclass

Stack(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    children: tuple[ContentBase, ...] = (),
    direction: Direction = "vertical",
    gap: int = 0
)

Bases: ContentBase

Lay out child content in one direction.

Example

Stack(children=(TextBlock(text="One"), TextBlock(text="Two")))

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

children class-attribute instance-attribute

children: tuple[ContentBase, ...] = ()

Child content.

direction class-attribute instance-attribute

direction: Direction = 'vertical'

Layout direction.

gap class-attribute instance-attribute

gap: int = 0

Cells between children.

TableGrid dataclass

TableGrid(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    rows: tuple[TableRow, ...] = (),
    header: TableRow | None = None,
    footer: TableRow | None = None,
    column_widths: tuple[int | float, ...] | None = None,
    column_spacing: int = 1,
    selected_row: int | None = None,
    selected_column: int | None = None,
    highlight_color: ColorLike | None = None,
    highlight_background: ColorLike | None = None,
    highlight_symbol: str | None = None
)

Bases: ContentBase

Render tabular rows with optional selection.

Example

TableGrid(rows=(TableRow(cells=("Ada", "Engineer")),))

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

rows class-attribute instance-attribute

rows: tuple[TableRow, ...] = ()

Body rows.

header class-attribute instance-attribute

header: TableRow | None = None

Optional header row.

footer class-attribute instance-attribute

footer: TableRow | None = None

Optional footer row.

column_widths class-attribute instance-attribute

column_widths: tuple[int | float, ...] | None = None

Fixed cell widths or fractional widths.

column_spacing class-attribute instance-attribute

column_spacing: int = 1

Cells between columns.

selected_row class-attribute instance-attribute

selected_row: int | None = None

Selected body row.

selected_column class-attribute instance-attribute

selected_column: int | None = None

Selected column.

highlight_color class-attribute instance-attribute

highlight_color: ColorLike | None = None

Selection foreground.

highlight_background class-attribute instance-attribute

highlight_background: ColorLike | None = None

Selection background.

highlight_symbol class-attribute instance-attribute

highlight_symbol: str | None = None

Symbol prefixed to the selected row.

TextBlock dataclass

TextBlock(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    text: str = "",
    lines: tuple[tuple[Run, ...], ...] = (),
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: tuple[CharacterModifier, ...] = (),
    horizontal_align: Alignment | None = None,
    vertical_align: VerticalAlignment | None = None,
    wrap: bool = True,
    color: InitVar[Any] = _ALIAS_UNSET,
    align: InitVar[Any] = _ALIAS_UNSET
)

Bases: ContentBase

Wrapped plain text or lines of styled runs.

Example

TextBlock(text="Hello", horizontal_align="center")

Attributes:

Methods:

  • __post_init__

    Apply deprecated constructor aliases.

  • from_plain

    Create a block from plain text and explicit style values.

  • from_lines

    Create a block from styled line sequences.

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

text class-attribute instance-attribute

text: str = ''

Plain text content.

lines class-attribute instance-attribute

lines: tuple[tuple[Run, ...], ...] = ()

Styled text lines.

foreground class-attribute instance-attribute

foreground: ColorLike | None = None

Default foreground color.

background class-attribute instance-attribute

background: ColorLike | None = None

Default background color.

modifiers class-attribute instance-attribute

modifiers: tuple[CharacterModifier, ...] = ()

Default character modifiers.

horizontal_align class-attribute instance-attribute

horizontal_align: Alignment | None = None

Horizontal alignment.

vertical_align class-attribute instance-attribute

vertical_align: VerticalAlignment | None = None

Vertical alignment within the painted area.

wrap class-attribute instance-attribute

wrap: bool = True

Whether long lines wrap.

__post_init__

__post_init__(color: Any, align: Any) -> None

Apply deprecated constructor aliases.

Source code in xnano/core/content.py
def __post_init__(self, color: Any, align: Any) -> None:
    """Apply deprecated constructor aliases."""
    resolve_init_alias(self, color, old="color", new="foreground")
    resolve_init_alias(self, align, old="align", new="horizontal_align")

from_plain classmethod

from_plain(
    text: str,
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    horizontal_align: Alignment | None = None,
    vertical_align: VerticalAlignment | None = None,
    wrap: bool = True,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None
) -> "TextBlock"

Create a block from plain text and explicit style values.

color is a deprecated alias for foreground.

Source code in xnano/core/content.py
@classmethod
def from_plain(
    cls,
    text: str,
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    horizontal_align: Alignment | None = None,
    vertical_align: VerticalAlignment | None = None,
    wrap: bool = True,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None,
) -> "TextBlock":
    """Create a block from plain text and explicit style values.

    ``color`` is a deprecated alias for ``foreground``.
    """
    foreground = resolve_color_alias(foreground, color, stacklevel=3)
    return cls(
        text=text,
        foreground=foreground,
        background=background,
        modifiers=tuple(modifiers or ()),
        horizontal_align=horizontal_align,
        vertical_align=vertical_align,
        wrap=wrap,
        style=style,
        z=z,
        visible=visible,
    )

from_lines classmethod

from_lines(
    lines: Sequence[Sequence[Run] | Run | str],
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    horizontal_align: Alignment | None = None,
    vertical_align: VerticalAlignment | None = None,
    wrap: bool = True,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None
) -> "TextBlock"

Create a block from styled line sequences.

color is a deprecated alias for foreground.

Source code in xnano/core/content.py
@classmethod
def from_lines(
    cls,
    lines: Sequence[Sequence[Run] | Run | str],
    *,
    foreground: ColorLike | None = None,
    background: ColorLike | None = None,
    modifiers: Sequence[CharacterModifier] | None = None,
    horizontal_align: Alignment | None = None,
    vertical_align: VerticalAlignment | None = None,
    wrap: bool = True,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    color: ColorLike | None = None,
) -> "TextBlock":
    """Create a block from styled line sequences.

    ``color`` is a deprecated alias for ``foreground``.
    """
    foreground = resolve_color_alias(foreground, color, stacklevel=3)
    normalized: list[tuple[Run, ...]] = []
    for line in lines:
        if isinstance(line, str):
            normalized.append((Run(text=line),))
        elif isinstance(line, Run):
            normalized.append((line,))
        else:
            normalized.append(tuple(line))
    return cls(
        lines=tuple(normalized),
        foreground=foreground,
        background=background,
        modifiers=tuple(modifiers or ()),
        horizontal_align=horizontal_align,
        vertical_align=vertical_align,
        wrap=wrap,
        style=style,
        z=z,
        visible=visible,
    )

ScrollbarContent dataclass

ScrollbarContent(
    *,
    style: Style | None = None,
    z: int = 0,
    visible: bool = True,
    content_length: int = 0,
    position: int = 0,
    viewport_length: int = 0,
    orientation: ScrollbarOrientationLike = "vertical_right",
    thumb_symbol: str | None = None,
    track_symbol: str | None = None,
    begin_symbol: str | None = None,
    end_symbol: str | None = None,
    color: ColorLike | None = None,
    thumb_color: ColorLike | None = None,
    track_color: ColorLike | None = None
)

Bases: ContentBase

Render scroll position and viewport proportion.

Example

Scrollbar(content_length=100, viewport_length=20, position=10)

Attributes:

style class-attribute instance-attribute

style: Style | None = None

Optional shared style.

z class-attribute instance-attribute

z: int = 0

Sibling-local paint order.

visible class-attribute instance-attribute

visible: bool = True

Whether this content paints.

content_length class-attribute instance-attribute

content_length: int = 0

Total scrollable length.

position class-attribute instance-attribute

position: int = 0

Current scroll offset.

viewport_length class-attribute instance-attribute

viewport_length: int = 0

Visible length.

orientation class-attribute instance-attribute

orientation: ScrollbarOrientationLike = 'vertical_right'

Scrollbar edge and direction.

thumb_symbol class-attribute instance-attribute

thumb_symbol: str | None = None

Custom thumb symbol.

track_symbol class-attribute instance-attribute

track_symbol: str | None = None

Custom track symbol.

begin_symbol class-attribute instance-attribute

begin_symbol: str | None = None

Symbol at the beginning.

end_symbol class-attribute instance-attribute

end_symbol: str | None = None

Symbol at the end.

color class-attribute instance-attribute

color: ColorLike | None = None

Default scrollbar color.

thumb_color class-attribute instance-attribute

thumb_color: ColorLike | None = None

Thumb color.

track_color class-attribute instance-attribute

track_color: ColorLike | None = None

Track color.

ComponentRenderContext dataclass

ComponentRenderContext(
    area: "Area",
    terminal: "Terminal[StateT] | None" = None,
    state: StateT | None = None,
    component: "Component | None" = None,
)

Bases: Generic[StateT]

Render-time scope passed into component paint hooks.

Attributes:

  • area ('Area') –

    Target area for this paint.

  • terminal ('Terminal[StateT] | None') –

    Terminal handling the render, when available.

  • state (StateT | None) –

    Application state for this paint.

  • component ('Component | None') –

    Component being rendered, when known.

area instance-attribute

area: 'Area'

Area assigned to the component.

terminal class-attribute instance-attribute

terminal: 'Terminal[StateT] | None' = None

Terminal handling the render.

state class-attribute instance-attribute

state: StateT | None = None

Application state for this paint.

component class-attribute instance-attribute

component: 'Component | None' = None

Component being rendered.

Component dataclass

Component(
    *,
    visible: bool = True,
    z: int = 0,
    fit_content: bool = True
)

Base class for custom xnano components.

Component attributes are live state: changing an attribute changes the next rendered frame. Implement compose() to return content. Interactive components can also implement handle_keyboard() or handle_paste().

The framework updates the read-only focused property when the component gains or loses field focus.

Example

Text(content="Ready", color="green")

Attributes:

  • visible (bool) –

    Whether the component is rendered.

  • z (int) –

    Paint order relative to sibling components.

  • fit_content (bool) –

    Whether layout should prefer the natural content size.

Methods:

  • component_post_init

    Initialize subclass state after dataclass fields are assigned.

  • get_frame

    Optional frame/panel chrome around composed content.

  • get_size

    Return the preferred cell size of this component.

  • before_render

    Called before rendering; returns the effective render area.

  • after_render

    Called after rendering for optional post-paint work.

  • compose

    Compose interface-neutral content for this component.

  • compose_extra_small

    Compose content when the viewport is extra small (< 40 cols).

  • compose_small

    Compose content when the viewport is small (40–79 cols).

  • compose_medium

    Compose content when the viewport is medium (80–119 cols).

  • compose_large

    Compose content when the viewport is large (120–159 cols).

  • compose_extra_large

    Compose content when the viewport is extra large (>= 160 cols).

  • handle_keyboard

    Optional keyboard handler while focused.

  • handle_paste

    Optional paste handler while focused.

visible class-attribute instance-attribute

visible: bool = dataclasses.field(
    default=True, kw_only=True
)

Whether this component paints at all.

z class-attribute instance-attribute

z: int = dataclasses.field(default=0, kw_only=True)

Stacking order among sibling content.

fit_content class-attribute instance-attribute

fit_content: bool = dataclasses.field(
    default=True, kw_only=True
)

When True, paint at natural size inside a larger slot.

focused property

focused: bool

Whether this component currently holds field focus.

component_post_init

component_post_init() -> None

Initialize subclass state after dataclass fields are assigned.

Override this method instead of __post_init__.

Source code in xnano/components/component.py
def component_post_init(self) -> None:
    """Initialize subclass state after dataclass fields are assigned.

    Override this method instead of ``__post_init__``.
    """
    return None

get_frame

get_frame() -> Any | None

Optional frame/panel chrome around composed content.

Source code in xnano/components/component.py
def get_frame(self) -> Any | None:
    """Optional frame/panel chrome around composed content."""
    return None

get_size

get_size(ctx: ComponentRenderContext[StateT]) -> Size

Return the preferred cell size of this component.

Source code in xnano/components/component.py
def get_size(self, ctx: ComponentRenderContext[StateT]) -> Size:
    """Return the preferred cell size of this component."""
    return Size(width=0, height=0)

before_render

before_render(
    ctx: ComponentRenderContext[StateT], area: "Area"
) -> "Area"

Called before rendering; returns the effective render area.

Source code in xnano/components/component.py
def before_render(
    self,
    ctx: ComponentRenderContext[StateT],
    area: "Area",
) -> "Area":
    """Called before rendering; returns the effective render area."""
    return area

after_render

after_render(
    ctx: ComponentRenderContext[StateT], area: "Area"
) -> None

Called after rendering for optional post-paint work.

Source code in xnano/components/component.py
def after_render(
    self,
    ctx: ComponentRenderContext[StateT],
    area: "Area",
) -> None:
    """Called after rendering for optional post-paint work."""
    return None

compose

compose(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose interface-neutral content for this component.

Returns:

  • Content | None

    A Content tree, or None when nothing should paint.

Source code in xnano/components/component.py
def compose(self, ctx: ComponentRenderContext[StateT]) -> Content | None:
    """Compose interface-neutral content for this component.

    Returns:
        A ``Content`` tree, or ``None`` when nothing should paint.
    """
    return None

compose_extra_small

compose_extra_small(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose content when the viewport is extra small (< 40 cols).

Optional responsive counterpart to :meth:compose. When overridden, it is used instead of compose while the window is in this size tier. Overriding any compose_* variant opts the component into breakpoint dispatch; a component that overrides none pays no per-frame cost.

Source code in xnano/components/component.py
@responsive_noop
def compose_extra_small(
    self, ctx: ComponentRenderContext[StateT]
) -> Content | None:
    """Compose content when the viewport is extra small (< 40 cols).

    Optional responsive counterpart to :meth:`compose`. When
    overridden, it is used *instead of* ``compose`` while the window
    is in this size tier. Overriding any ``compose_*`` variant opts the
    component into breakpoint dispatch; a component that overrides none
    pays no per-frame cost.
    """
    return None

compose_small

compose_small(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose content when the viewport is small (40–79 cols).

See :meth:compose_extra_small.

Source code in xnano/components/component.py
@responsive_noop
def compose_small(
    self, ctx: ComponentRenderContext[StateT]
) -> Content | None:
    """Compose content when the viewport is small (40–79 cols).

    See :meth:`compose_extra_small`.
    """
    return None

compose_medium

compose_medium(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose content when the viewport is medium (80–119 cols).

See :meth:compose_extra_small.

Source code in xnano/components/component.py
@responsive_noop
def compose_medium(
    self, ctx: ComponentRenderContext[StateT]
) -> Content | None:
    """Compose content when the viewport is medium (80–119 cols).

    See :meth:`compose_extra_small`.
    """
    return None

compose_large

compose_large(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose content when the viewport is large (120–159 cols).

See :meth:compose_extra_small.

Source code in xnano/components/component.py
@responsive_noop
def compose_large(
    self, ctx: ComponentRenderContext[StateT]
) -> Content | None:
    """Compose content when the viewport is large (120–159 cols).

    See :meth:`compose_extra_small`.
    """
    return None

compose_extra_large

compose_extra_large(
    ctx: ComponentRenderContext[StateT],
) -> Content | None

Compose content when the viewport is extra large (>= 160 cols).

See :meth:compose_extra_small.

Source code in xnano/components/component.py
@responsive_noop
def compose_extra_large(
    self, ctx: ComponentRenderContext[StateT]
) -> Content | None:
    """Compose content when the viewport is extra large (>= 160 cols).

    See :meth:`compose_extra_small`.
    """
    return None

handle_keyboard

handle_keyboard(keyboard: 'KeyboardEventData') -> bool

Optional keyboard handler while focused.

Returns:

  • bool

    True when the event was consumed.

Source code in xnano/components/component.py
def handle_keyboard(self, keyboard: "KeyboardEventData") -> bool:
    """Optional keyboard handler while focused.

    Returns:
        ``True`` when the event was consumed.
    """
    return False

handle_paste

handle_paste(text: str) -> bool

Optional paste handler while focused.

Returns:

  • bool

    True when the paste was consumed.

Source code in xnano/components/component.py
def handle_paste(self, text: str) -> bool:
    """Optional paste handler while focused.

    Returns:
        ``True`` when the paste was consumed.
    """
    return False