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
¶
Content: TypeAlias = (
Run
| TextBlock
| Stack
| Panel
| Gauge
| LineGauge
| Bars
| Plot
| Sparkline
| TableGrid
| Items
| Canvas
| Scrollbar
| Clear
| CellCanvas
| Native
)
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:
-
groups(tuple[BarGroup, ...]) –Bar groups.
-
bar_width(int) –Width of each bar in cells.
-
bar_gap(int) –Cells between bars.
-
group_gap(int) –Cells between groups.
-
max_value(int | None) –Explicit value ceiling.
-
direction(Direction) –Bar growth direction.
-
color(ColorLike | None) –Default bar color.
-
value_color(ColorLike | None) –Default value-label color.
-
label_color(ColorLike | 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:
-
shapes(tuple[CanvasShape, ...]) –Shapes painted in declaration order.
-
x_bounds(tuple[float, float]) –Horizontal coordinate bounds.
-
y_bounds(tuple[float, float]) –Vertical coordinate bounds.
-
marker(CanvasMarkerLike) –Canvas point marker.
-
background(ColorLike | None) –Canvas background.
shapes
class-attribute
instance-attribute
¶
shapes: tuple[CanvasShape, ...] = ()
Shapes painted in declaration order.
x_bounds
class-attribute
instance-attribute
¶
Horizontal coordinate bounds.
y_bounds
class-attribute
instance-attribute
¶
Vertical coordinate bounds.
marker
class-attribute
instance-attribute
¶
marker: CanvasMarkerLike = 'braille'
Canvas point marker.
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:
-
rows(tuple[tuple[CellSpan, ...], ...]) –Rows of styled spans.
-
width(int) –Canvas width in cells.
-
height(int) –Canvas height in cells.
Methods:
-
from_rows–Create a cell canvas from styled span rows.
rows
class-attribute
instance-attribute
¶
Rows of styled spans.
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
Clear
dataclass
¶
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:
-
progress(float) –Completion ratio from zero to one.
-
label(str | None) –Text displayed inside the gauge.
-
foreground(ColorLike) –Filled-region color.
-
background(ColorLike | None) –Gauge background color.
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.
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:
-
items(tuple[str | Run | TextBlock, ...]) –List entries.
-
selected(int | None) –Selected entry index.
-
foreground(ColorLike | None) –Default foreground.
-
background(ColorLike | None) –Default background.
-
highlight_color(ColorLike) –Selected-entry foreground.
-
highlight_background(ColorLike) –Selected-entry background.
-
highlight_symbol(str) –Symbol prefixed to the selected entry.
items
class-attribute
instance-attribute
¶
List entries.
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.
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:
-
progress(float) –Completion ratio from zero to one.
-
label(str | None) –Text displayed with the line.
-
foreground(ColorLike | None) –Default foreground color.
-
filled_color(ColorLike | None) –Completed-region color.
-
unfilled_color(ColorLike | None) –Remaining-region color.
-
background(ColorLike | None) –Line background color.
progress
class-attribute
instance-attribute
¶
progress: float = 0.0
Completion ratio from zero to one.
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.
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:
-
child(ContentBase) –Content inside the panel.
-
title(str | None) –Optional frame title.
-
title_position(FrameTitlePosition | None) –Frame title alignment.
-
border(Border | None) –Border style.
-
border_color(ColorLike | None) –Border foreground color.
-
border_sides(tuple[Side, ...] | None) –Visible border sides.
-
background(ColorLike | None) –Panel background color.
-
padding(PaddingLike | None) –Space between border and child.
title_position
class-attribute
instance-attribute
¶
title_position: FrameTitlePosition | None = None
Frame title alignment.
border_color
class-attribute
instance-attribute
¶
border_color: ColorLike | None = None
Border foreground color.
border_sides
class-attribute
instance-attribute
¶
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:
-
datasets(tuple[PlotDataset, ...]) –Data series.
-
x_axis(PlotAxis | None) –Horizontal axis.
-
y_axis(PlotAxis | None) –Vertical axis.
-
color(ColorLike | None) –Default plot color.
-
legend(bool) –Whether to show the legend.
-
legend_position(LegendPositionLike | None) –Legend placement.
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:
-
text(str) –Text displayed by the run.
-
foreground(ColorLike | None) –Foreground color.
-
background(ColorLike | None) –Background color.
-
modifiers(tuple[CharacterModifier, ...]) –Character modifiers.
Methods:
-
__post_init__–Apply the deprecated constructor alias.
-
plain–Create a plain styled 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.
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
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:
-
data(tuple[int, ...]) –Sample values.
-
bars(tuple[SparklineBar, ...] | None) –Individually styled samples.
-
max_value(int | None) –Explicit sample ceiling.
-
foreground(ColorLike | None) –Default sample color.
-
background(ColorLike | None) –Sparkline background.
-
absent_value_color(ColorLike | None) –Color for missing or zero samples.
-
absent_value_symbol(str | None) –Symbol for missing or zero samples.
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.
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:
-
children(tuple[ContentBase, ...]) –Child content.
-
direction(Direction) –Layout direction.
-
gap(int) –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:
-
rows(tuple[TableRow, ...]) –Body rows.
-
header(TableRow | None) –Optional header row.
-
footer(TableRow | None) –Optional footer row.
-
column_widths(tuple[int | float, ...] | None) –Fixed cell widths or fractional widths.
-
column_spacing(int) –Cells between columns.
-
selected_row(int | None) –Selected body row.
-
selected_column(int | None) –Selected column.
-
highlight_color(ColorLike | None) –Selection foreground.
-
highlight_background(ColorLike | None) –Selection background.
-
highlight_symbol(str | None) –Symbol prefixed to the selected row.
column_widths
class-attribute
instance-attribute
¶
Fixed cell widths or fractional widths.
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.
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:
-
text(str) –Plain text content.
-
lines(tuple[tuple[Run, ...], ...]) –Styled text lines.
-
foreground(ColorLike | None) –Default foreground color.
-
background(ColorLike | None) –Default background color.
-
modifiers(tuple[CharacterModifier, ...]) –Default character modifiers.
-
horizontal_align(Alignment | None) –Horizontal alignment.
-
vertical_align(VerticalAlignment | None) –Vertical alignment within the painted area.
-
wrap(bool) –Whether long lines wrap.
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.
lines
class-attribute
instance-attribute
¶
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.
__post_init__
¶
Apply deprecated constructor aliases.
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
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
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:
-
content_length(int) –Total scrollable length.
-
position(int) –Current scroll offset.
-
viewport_length(int) –Visible length.
-
orientation(ScrollbarOrientationLike) –Scrollbar edge and direction.
-
thumb_symbol(str | None) –Custom thumb symbol.
-
track_symbol(str | None) –Custom track symbol.
-
begin_symbol(str | None) –Symbol at the beginning.
-
end_symbol(str | None) –Symbol at the end.
-
color(ColorLike | None) –Default scrollbar color.
-
thumb_color(ColorLike | None) –Thumb color.
-
track_color(ColorLike | None) –Track color.
content_length
class-attribute
instance-attribute
¶
content_length: int = 0
Total scrollable 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.
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.
Component
dataclass
¶
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.
component_post_init
¶
Initialize subclass state after dataclass fields are assigned.
Override this method instead of __post_init__.
get_size
¶
get_size(ctx: ComponentRenderContext[StateT]) -> Size
before_render
¶
before_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> "Area"
after_render
¶
after_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> None
compose
¶
compose(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose interface-neutral content for this component.
Returns:
-
Content | None–A
Contenttree, orNonewhen nothing should paint.
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
compose_small
¶
compose_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is small (40–79 cols).
See :meth:compose_extra_small.
compose_medium
¶
compose_medium(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is medium (80–119 cols).
See :meth:compose_extra_small.
compose_large
¶
compose_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is large (120–159 cols).
See :meth:compose_extra_small.
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
handle_keyboard
¶
handle_keyboard(keyboard: 'KeyboardEventData') -> bool