forked from genewildish/Mainline
fix(performance): use simple height estimation instead of PIL rendering
- Replace estimate_block_height (PIL-based) with estimate_simple_height (word wrap) - Update viewport filter tests to match new height-based filtering (~4 items vs 24) - Fix CI task duplication in mise.toml (remove redundant depends) Closes #38 Closes #36
This commit is contained in:
@@ -3,843 +3,48 @@ Stage adapters - Bridge existing components to the Stage interface.
|
||||
|
||||
This module provides adapters that wrap existing components
|
||||
(EffectPlugin, Display, DataSource, Camera) as Stage implementations.
|
||||
|
||||
DEPRECATED: This file is now a compatibility wrapper.
|
||||
Use `engine.pipeline.adapters` package instead.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from engine.pipeline.core import PipelineContext, Stage
|
||||
|
||||
|
||||
class EffectPluginStage(Stage):
|
||||
"""Adapter wrapping EffectPlugin as a Stage."""
|
||||
|
||||
def __init__(self, effect_plugin, name: str = "effect"):
|
||||
self._effect = effect_plugin
|
||||
self.name = name
|
||||
self.category = "effect"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
"""Return stage_type based on effect name.
|
||||
|
||||
HUD effects are overlays.
|
||||
"""
|
||||
if self.name == "hud":
|
||||
return "overlay"
|
||||
return self.category
|
||||
|
||||
@property
|
||||
def render_order(self) -> int:
|
||||
"""Return render_order based on effect type.
|
||||
|
||||
HUD effects have high render_order to appear on top.
|
||||
"""
|
||||
if self.name == "hud":
|
||||
return 100 # High order for overlays
|
||||
return 0
|
||||
|
||||
@property
|
||||
def is_overlay(self) -> bool:
|
||||
"""Return True for HUD effects.
|
||||
|
||||
HUD is an overlay - it composes on top of the buffer
|
||||
rather than transforming it for the next stage.
|
||||
"""
|
||||
return self.name == "hud"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"effect.{self.name}"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return set()
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Process data through the effect."""
|
||||
if data is None:
|
||||
return None
|
||||
from engine.effects.types import EffectContext, apply_param_bindings
|
||||
|
||||
w = ctx.params.viewport_width if ctx.params else 80
|
||||
h = ctx.params.viewport_height if ctx.params else 24
|
||||
frame = ctx.params.frame_number if ctx.params else 0
|
||||
|
||||
effect_ctx = EffectContext(
|
||||
terminal_width=w,
|
||||
terminal_height=h,
|
||||
scroll_cam=0,
|
||||
ticker_height=h,
|
||||
camera_x=0,
|
||||
mic_excess=0.0,
|
||||
grad_offset=(frame * 0.01) % 1.0,
|
||||
frame_number=frame,
|
||||
has_message=False,
|
||||
items=ctx.get("items", []),
|
||||
)
|
||||
|
||||
# Copy sensor state from PipelineContext to EffectContext
|
||||
for key, value in ctx.state.items():
|
||||
if key.startswith("sensor."):
|
||||
effect_ctx.set_state(key, value)
|
||||
|
||||
# Copy metrics from PipelineContext to EffectContext
|
||||
if "metrics" in ctx.state:
|
||||
effect_ctx.set_state("metrics", ctx.state["metrics"])
|
||||
|
||||
# Apply sensor param bindings if effect has them
|
||||
if hasattr(self._effect, "param_bindings") and self._effect.param_bindings:
|
||||
bound_config = apply_param_bindings(self._effect, effect_ctx)
|
||||
self._effect.configure(bound_config)
|
||||
|
||||
return self._effect.process(data, effect_ctx)
|
||||
|
||||
|
||||
class DisplayStage(Stage):
|
||||
"""Adapter wrapping Display as a Stage."""
|
||||
|
||||
def __init__(self, display, name: str = "terminal"):
|
||||
self._display = display
|
||||
self.name = name
|
||||
self.category = "display"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"display.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"render.output"} # Display needs rendered content
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER} # Display consumes rendered text
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.NONE} # Display is a terminal stage (no output)
|
||||
|
||||
def init(self, ctx: PipelineContext) -> bool:
|
||||
w = ctx.params.viewport_width if ctx.params else 80
|
||||
h = ctx.params.viewport_height if ctx.params else 24
|
||||
result = self._display.init(w, h, reuse=False)
|
||||
return result is not False
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Output data to display."""
|
||||
if data is not None:
|
||||
self._display.show(data)
|
||||
return data
|
||||
|
||||
def cleanup(self) -> None:
|
||||
self._display.cleanup()
|
||||
|
||||
|
||||
class DataSourceStage(Stage):
|
||||
"""Adapter wrapping DataSource as a Stage."""
|
||||
|
||||
def __init__(self, data_source, name: str = "headlines"):
|
||||
self._source = data_source
|
||||
self.name = name
|
||||
self.category = "source"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"source.{self.name}"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return set()
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.NONE} # Sources don't take input
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Fetch data from source."""
|
||||
if hasattr(self._source, "get_items"):
|
||||
return self._source.get_items()
|
||||
return data
|
||||
|
||||
|
||||
class PassthroughStage(Stage):
|
||||
"""Simple stage that passes data through unchanged.
|
||||
|
||||
Used for sources that already provide the data in the correct format
|
||||
(e.g., pipeline introspection that outputs text directly).
|
||||
"""
|
||||
|
||||
def __init__(self, name: str = "passthrough"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Pass data through unchanged."""
|
||||
return data
|
||||
|
||||
|
||||
class SourceItemsToBufferStage(Stage):
|
||||
"""Convert SourceItem objects to text buffer.
|
||||
|
||||
Takes a list of SourceItem objects and extracts their content,
|
||||
splitting on newlines to create a proper text buffer for display.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str = "items-to-buffer"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Convert SourceItem list to text buffer."""
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
# If already a list of strings, return as-is
|
||||
if isinstance(data, list) and data and isinstance(data[0], str):
|
||||
return data
|
||||
|
||||
# If it's a list of SourceItem, extract content
|
||||
from engine.data_sources import SourceItem
|
||||
|
||||
if isinstance(data, list):
|
||||
result = []
|
||||
for item in data:
|
||||
if isinstance(item, SourceItem):
|
||||
# Split content by newline to get individual lines
|
||||
lines = item.content.split("\n")
|
||||
result.extend(lines)
|
||||
elif hasattr(item, "content"): # Has content attribute
|
||||
lines = str(item.content).split("\n")
|
||||
result.extend(lines)
|
||||
else:
|
||||
result.append(str(item))
|
||||
return result
|
||||
|
||||
# Single item
|
||||
if isinstance(data, SourceItem):
|
||||
return data.content.split("\n")
|
||||
|
||||
return [str(data)]
|
||||
|
||||
|
||||
class CameraStage(Stage):
|
||||
"""Adapter wrapping Camera as a Stage."""
|
||||
|
||||
def __init__(self, camera, name: str = "vertical"):
|
||||
self._camera = camera
|
||||
self.name = name
|
||||
self.category = "camera"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"camera"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"render.output"} # Depend on rendered output from font or render stage
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER} # Camera works on rendered text
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Apply camera transformation to data."""
|
||||
if data is None or (isinstance(data, list) and len(data) == 0):
|
||||
return data
|
||||
if hasattr(self._camera, "apply"):
|
||||
viewport_width = ctx.params.viewport_width if ctx.params else 80
|
||||
viewport_height = ctx.params.viewport_height if ctx.params else 24
|
||||
buffer_height = len(data) if isinstance(data, list) else 0
|
||||
|
||||
# Get global layout height for canvas (enables full scrolling range)
|
||||
total_layout_height = ctx.get("total_layout_height", buffer_height)
|
||||
|
||||
# Preserve camera's configured canvas width, but ensure it's at least viewport_width
|
||||
# This allows horizontal/omni/floating/bounce cameras to scroll properly
|
||||
canvas_width = max(
|
||||
viewport_width, getattr(self._camera, "canvas_width", viewport_width)
|
||||
)
|
||||
|
||||
# Update camera's viewport dimensions so it knows its actual bounds
|
||||
# Set canvas size to achieve desired viewport (viewport = canvas / zoom)
|
||||
if hasattr(self._camera, "set_canvas_size"):
|
||||
self._camera.set_canvas_size(
|
||||
width=int(viewport_width * self._camera.zoom),
|
||||
height=int(viewport_height * self._camera.zoom),
|
||||
)
|
||||
|
||||
# Set canvas to full layout height so camera can scroll through all content
|
||||
self._camera.set_canvas_size(width=canvas_width, height=total_layout_height)
|
||||
|
||||
# Update camera position (scroll) - uses global canvas for clamping
|
||||
if hasattr(self._camera, "update"):
|
||||
self._camera.update(1 / 60)
|
||||
|
||||
# Store camera_y in context for ViewportFilterStage (global y position)
|
||||
ctx.set("camera_y", self._camera.y)
|
||||
|
||||
# Apply camera viewport slicing to the partial buffer
|
||||
# The buffer starts at render_offset_y in global coordinates
|
||||
render_offset_y = ctx.get("render_offset_y", 0)
|
||||
|
||||
# Temporarily shift camera to local buffer coordinates for apply()
|
||||
real_y = self._camera.y
|
||||
local_y = max(0, real_y - render_offset_y)
|
||||
|
||||
# Temporarily shrink canvas to local buffer size so apply() works correctly
|
||||
self._camera.set_canvas_size(width=canvas_width, height=buffer_height)
|
||||
self._camera.y = local_y
|
||||
|
||||
# Apply slicing
|
||||
result = self._camera.apply(data, viewport_width, viewport_height)
|
||||
|
||||
# Restore global canvas and camera position for next frame
|
||||
self._camera.set_canvas_size(width=canvas_width, height=total_layout_height)
|
||||
self._camera.y = real_y
|
||||
|
||||
return result
|
||||
return data
|
||||
|
||||
def cleanup(self) -> None:
|
||||
if hasattr(self._camera, "reset"):
|
||||
self._camera.reset()
|
||||
|
||||
|
||||
class ViewportFilterStage(Stage):
|
||||
"""Stage that limits items based on layout calculation.
|
||||
|
||||
Computes cumulative y-offsets for all items using cheap height estimation,
|
||||
then returns only items that overlap the camera's viewport window.
|
||||
This prevents FontStage from rendering thousands of items when only a few
|
||||
are visible, while still allowing camera scrolling through all content.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str = "viewport-filter"):
|
||||
self.name = name
|
||||
self.category = "filter"
|
||||
self.optional = False
|
||||
self._cached_count = 0
|
||||
self._layout: list[tuple[int, int]] = []
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "filter"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"filter.{self.name}"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Filter items based on layout and camera position."""
|
||||
if data is None or not isinstance(data, list):
|
||||
return data
|
||||
|
||||
viewport_height = ctx.params.viewport_height if ctx.params else 24
|
||||
viewport_width = ctx.params.viewport_width if ctx.params else 80
|
||||
camera_y = ctx.get("camera_y", 0)
|
||||
|
||||
# Recompute layout when item count OR viewport width changes
|
||||
cached_width = getattr(self, "_cached_width", None)
|
||||
if len(data) != self._cached_count or cached_width != viewport_width:
|
||||
self._layout = []
|
||||
y = 0
|
||||
from engine.render.blocks import estimate_block_height
|
||||
|
||||
for item in data:
|
||||
if hasattr(item, "content"):
|
||||
title = item.content
|
||||
elif isinstance(item, tuple):
|
||||
title = str(item[0]) if item else ""
|
||||
else:
|
||||
title = str(item)
|
||||
h = estimate_block_height(title, viewport_width)
|
||||
self._layout.append((y, h))
|
||||
y += h
|
||||
self._cached_count = len(data)
|
||||
self._cached_width = viewport_width
|
||||
|
||||
# Find items visible in [camera_y - buffer, camera_y + viewport_height + buffer]
|
||||
buffer_zone = viewport_height
|
||||
vis_start = max(0, camera_y - buffer_zone)
|
||||
vis_end = camera_y + viewport_height + buffer_zone
|
||||
|
||||
visible_items = []
|
||||
render_offset_y = 0
|
||||
first_visible_found = False
|
||||
for i, (start_y, height) in enumerate(self._layout):
|
||||
item_end = start_y + height
|
||||
if item_end > vis_start and start_y < vis_end:
|
||||
if not first_visible_found:
|
||||
render_offset_y = start_y
|
||||
first_visible_found = True
|
||||
visible_items.append(data[i])
|
||||
|
||||
# Compute total layout height for the canvas
|
||||
total_layout_height = 0
|
||||
if self._layout:
|
||||
last_start, last_height = self._layout[-1]
|
||||
total_layout_height = last_start + last_height
|
||||
|
||||
# Store metadata for CameraStage
|
||||
ctx.set("render_offset_y", render_offset_y)
|
||||
ctx.set("total_layout_height", total_layout_height)
|
||||
|
||||
# Always return at least one item to avoid empty buffer errors
|
||||
return visible_items if visible_items else data[:1]
|
||||
|
||||
|
||||
class FontStage(Stage):
|
||||
"""Stage that applies font rendering to content.
|
||||
|
||||
FontStage is a Transform that takes raw content (text, headlines)
|
||||
and renders it to an ANSI-formatted buffer using the configured font.
|
||||
|
||||
This decouples font rendering from data sources, allowing:
|
||||
- Different fonts per source
|
||||
- Runtime font swapping
|
||||
- Font as a pipeline stage
|
||||
|
||||
Attributes:
|
||||
font_path: Path to font file (None = use config default)
|
||||
font_size: Font size in points (None = use config default)
|
||||
font_ref: Reference name for registered font ("default", "cjk", etc.)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
font_path: str | None = None,
|
||||
font_size: int | None = None,
|
||||
font_ref: str | None = "default",
|
||||
name: str = "font",
|
||||
):
|
||||
self.name = name
|
||||
self.category = "transform"
|
||||
self.optional = False
|
||||
self._font_path = font_path
|
||||
self._font_size = font_size
|
||||
self._font_ref = font_ref
|
||||
self._font = None
|
||||
self._render_cache: dict[tuple[str, str, str, int], list[str]] = {}
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "transform"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"transform.{self.name}", "render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def init(self, ctx: PipelineContext) -> bool:
|
||||
"""Initialize font from config or path."""
|
||||
from engine import config
|
||||
|
||||
if self._font_path:
|
||||
try:
|
||||
from PIL import ImageFont
|
||||
|
||||
size = self._font_size or config.FONT_SZ
|
||||
self._font = ImageFont.truetype(self._font_path, size)
|
||||
except Exception:
|
||||
return False
|
||||
return True
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Render content with font to buffer."""
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
from engine.render import make_block
|
||||
|
||||
w = ctx.params.viewport_width if ctx.params else 80
|
||||
|
||||
# If data is already a list of strings (buffer), return as-is
|
||||
if isinstance(data, list) and data and isinstance(data[0], str):
|
||||
return data
|
||||
|
||||
# If data is a list of items, render each with font
|
||||
if isinstance(data, list):
|
||||
result = []
|
||||
for item in data:
|
||||
# Handle SourceItem or tuple (title, source, timestamp)
|
||||
if hasattr(item, "content"):
|
||||
title = item.content
|
||||
src = getattr(item, "source", "unknown")
|
||||
ts = getattr(item, "timestamp", "0")
|
||||
elif isinstance(item, tuple):
|
||||
title = item[0] if len(item) > 0 else ""
|
||||
src = item[1] if len(item) > 1 else "unknown"
|
||||
ts = str(item[2]) if len(item) > 2 else "0"
|
||||
else:
|
||||
title = str(item)
|
||||
src = "unknown"
|
||||
ts = "0"
|
||||
|
||||
# Check cache first
|
||||
cache_key = (title, src, ts, w)
|
||||
if cache_key in self._render_cache:
|
||||
result.extend(self._render_cache[cache_key])
|
||||
continue
|
||||
|
||||
try:
|
||||
block_lines, color_code, meta_idx = make_block(title, src, ts, w)
|
||||
self._render_cache[cache_key] = block_lines
|
||||
result.extend(block_lines)
|
||||
except Exception:
|
||||
result.append(title)
|
||||
|
||||
return result
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class ImageToTextStage(Stage):
|
||||
"""Transform that converts PIL Image to ASCII text buffer.
|
||||
|
||||
Takes an ImageItem or PIL Image and converts it to a text buffer
|
||||
using ASCII character density mapping. The output can be displayed
|
||||
directly or further processed by effects.
|
||||
|
||||
Attributes:
|
||||
width: Output width in characters
|
||||
height: Output height in characters
|
||||
charset: Character set for density mapping (default: simple ASCII)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
width: int = 80,
|
||||
height: int = 24,
|
||||
charset: str = " .:-=+*#%@",
|
||||
name: str = "image-to-text",
|
||||
):
|
||||
self.name = name
|
||||
self.category = "transform"
|
||||
self.optional = False
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.charset = charset
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "transform"
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.PIL_IMAGE} # Accepts PIL Image objects or ImageItem
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"transform.{self.name}", "render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Convert PIL Image to text buffer."""
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
from engine.data_sources.sources import ImageItem
|
||||
|
||||
# Extract PIL Image from various input types
|
||||
pil_image = None
|
||||
|
||||
if isinstance(data, ImageItem) or hasattr(data, "image"):
|
||||
pil_image = data.image
|
||||
else:
|
||||
# Assume it's already a PIL Image
|
||||
pil_image = data
|
||||
|
||||
# Check if it's a PIL Image
|
||||
if not hasattr(pil_image, "resize"):
|
||||
# Not a PIL Image, return as-is
|
||||
return data if isinstance(data, list) else [str(data)]
|
||||
|
||||
# Convert to grayscale and resize
|
||||
try:
|
||||
if pil_image.mode != "L":
|
||||
pil_image = pil_image.convert("L")
|
||||
except Exception:
|
||||
return ["[image conversion error]"]
|
||||
|
||||
# Calculate cell aspect ratio correction (characters are taller than wide)
|
||||
aspect_ratio = 0.5
|
||||
target_w = self.width
|
||||
target_h = int(self.height * aspect_ratio)
|
||||
|
||||
# Resize image to target dimensions
|
||||
try:
|
||||
resized = pil_image.resize((target_w, target_h))
|
||||
except Exception:
|
||||
return ["[image resize error]"]
|
||||
|
||||
# Map pixels to characters
|
||||
result = []
|
||||
pixels = list(resized.getdata())
|
||||
|
||||
for row in range(target_h):
|
||||
line = ""
|
||||
for col in range(target_w):
|
||||
idx = row * target_w + col
|
||||
if idx < len(pixels):
|
||||
brightness = pixels[idx]
|
||||
char_idx = int((brightness / 255) * (len(self.charset) - 1))
|
||||
line += self.charset[char_idx]
|
||||
else:
|
||||
line += " "
|
||||
result.append(line)
|
||||
|
||||
# Pad or trim to exact height
|
||||
while len(result) < self.height:
|
||||
result.append(" " * self.width)
|
||||
result = result[: self.height]
|
||||
|
||||
# Pad lines to width
|
||||
result = [line.ljust(self.width) for line in result]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def create_stage_from_display(display, name: str = "terminal") -> DisplayStage:
|
||||
"""Create a Stage from a Display instance."""
|
||||
return DisplayStage(display, name)
|
||||
|
||||
|
||||
def create_stage_from_effect(effect_plugin, name: str) -> EffectPluginStage:
|
||||
"""Create a Stage from an EffectPlugin."""
|
||||
return EffectPluginStage(effect_plugin, name)
|
||||
|
||||
|
||||
def create_stage_from_source(data_source, name: str = "headlines") -> DataSourceStage:
|
||||
"""Create a Stage from a DataSource."""
|
||||
return DataSourceStage(data_source, name)
|
||||
|
||||
|
||||
def create_stage_from_camera(camera, name: str = "vertical") -> CameraStage:
|
||||
"""Create a Stage from a Camera."""
|
||||
return CameraStage(camera, name)
|
||||
|
||||
|
||||
def create_stage_from_font(
|
||||
font_path: str | None = None,
|
||||
font_size: int | None = None,
|
||||
font_ref: str | None = "default",
|
||||
name: str = "font",
|
||||
) -> FontStage:
|
||||
"""Create a FontStage for rendering content with fonts."""
|
||||
return FontStage(
|
||||
font_path=font_path, font_size=font_size, font_ref=font_ref, name=name
|
||||
)
|
||||
|
||||
|
||||
class CanvasStage(Stage):
|
||||
"""Stage that manages a Canvas for rendering.
|
||||
|
||||
CanvasStage creates and manages a 2D canvas that can hold rendered content.
|
||||
Other stages can write to and read from the canvas via the pipeline context.
|
||||
|
||||
This enables:
|
||||
- Pre-rendering content off-screen
|
||||
- Multiple cameras viewing different regions
|
||||
- Smooth scrolling (camera moves, content stays)
|
||||
- Layer compositing
|
||||
|
||||
Usage:
|
||||
- Add CanvasStage to pipeline
|
||||
- Other stages access canvas via: ctx.get("canvas")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
width: int = 80,
|
||||
height: int = 24,
|
||||
name: str = "canvas",
|
||||
):
|
||||
self.name = name
|
||||
self.category = "system"
|
||||
self.optional = True
|
||||
self._width = width
|
||||
self._height = height
|
||||
self._canvas = None
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "system"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"canvas"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return set()
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.ANY}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.ANY}
|
||||
|
||||
def init(self, ctx: PipelineContext) -> bool:
|
||||
from engine.canvas import Canvas
|
||||
|
||||
self._canvas = Canvas(width=self._width, height=self._height)
|
||||
ctx.set("canvas", self._canvas)
|
||||
return True
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Pass through data but ensure canvas is in context."""
|
||||
if self._canvas is None:
|
||||
from engine.canvas import Canvas
|
||||
|
||||
self._canvas = Canvas(width=self._width, height=self._height)
|
||||
ctx.set("canvas", self._canvas)
|
||||
|
||||
# Get dirty regions from canvas and expose via context
|
||||
# Effects can access via ctx.get_state("canvas.dirty_rows")
|
||||
if self._canvas.is_dirty():
|
||||
dirty_rows = self._canvas.get_dirty_rows()
|
||||
ctx.set_state("canvas.dirty_rows", dirty_rows)
|
||||
ctx.set_state("canvas.dirty_regions", self._canvas.get_dirty_regions())
|
||||
|
||||
return data
|
||||
|
||||
def get_canvas(self):
|
||||
"""Get the canvas instance."""
|
||||
return self._canvas
|
||||
|
||||
def cleanup(self) -> None:
|
||||
self._canvas = None
|
||||
# Re-export from the new package structure for backward compatibility
|
||||
from engine.pipeline.adapters import (
|
||||
# Adapter classes
|
||||
CameraStage,
|
||||
CanvasStage,
|
||||
DataSourceStage,
|
||||
DisplayStage,
|
||||
EffectPluginStage,
|
||||
FontStage,
|
||||
ImageToTextStage,
|
||||
PassthroughStage,
|
||||
SourceItemsToBufferStage,
|
||||
ViewportFilterStage,
|
||||
# Factory functions
|
||||
create_stage_from_camera,
|
||||
create_stage_from_display,
|
||||
create_stage_from_effect,
|
||||
create_stage_from_font,
|
||||
create_stage_from_source,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# Adapter classes
|
||||
"EffectPluginStage",
|
||||
"DisplayStage",
|
||||
"DataSourceStage",
|
||||
"PassthroughStage",
|
||||
"SourceItemsToBufferStage",
|
||||
"CameraStage",
|
||||
"ViewportFilterStage",
|
||||
"FontStage",
|
||||
"ImageToTextStage",
|
||||
"CanvasStage",
|
||||
# Factory functions
|
||||
"create_stage_from_display",
|
||||
"create_stage_from_effect",
|
||||
"create_stage_from_source",
|
||||
"create_stage_from_camera",
|
||||
"create_stage_from_font",
|
||||
]
|
||||
|
||||
43
engine/pipeline/adapters/__init__.py
Normal file
43
engine/pipeline/adapters/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Stage adapters - Bridge existing components to the Stage interface.
|
||||
|
||||
This module provides adapters that wrap existing components
|
||||
(EffectPlugin, Display, DataSource, Camera) as Stage implementations.
|
||||
"""
|
||||
|
||||
from .camera import CameraStage
|
||||
from .data_source import DataSourceStage, PassthroughStage, SourceItemsToBufferStage
|
||||
from .display import DisplayStage
|
||||
from .effect_plugin import EffectPluginStage
|
||||
from .factory import (
|
||||
create_stage_from_camera,
|
||||
create_stage_from_display,
|
||||
create_stage_from_effect,
|
||||
create_stage_from_font,
|
||||
create_stage_from_source,
|
||||
)
|
||||
from .transform import (
|
||||
CanvasStage,
|
||||
FontStage,
|
||||
ImageToTextStage,
|
||||
ViewportFilterStage,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# Adapter classes
|
||||
"EffectPluginStage",
|
||||
"DisplayStage",
|
||||
"DataSourceStage",
|
||||
"PassthroughStage",
|
||||
"SourceItemsToBufferStage",
|
||||
"CameraStage",
|
||||
"ViewportFilterStage",
|
||||
"FontStage",
|
||||
"ImageToTextStage",
|
||||
"CanvasStage",
|
||||
# Factory functions
|
||||
"create_stage_from_display",
|
||||
"create_stage_from_effect",
|
||||
"create_stage_from_source",
|
||||
"create_stage_from_camera",
|
||||
"create_stage_from_font",
|
||||
]
|
||||
48
engine/pipeline/adapters/camera.py
Normal file
48
engine/pipeline/adapters/camera.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""Adapter for camera stage."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from engine.pipeline.core import DataType, PipelineContext, Stage
|
||||
|
||||
|
||||
class CameraStage(Stage):
|
||||
"""Adapter wrapping Camera as a Stage."""
|
||||
|
||||
def __init__(self, camera, name: str = "vertical"):
|
||||
self._camera = camera
|
||||
self.name = name
|
||||
self.category = "camera"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "camera"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"camera"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Apply camera transformation to items."""
|
||||
if data is None:
|
||||
return data
|
||||
|
||||
# Apply camera offset to items
|
||||
if hasattr(self._camera, "apply"):
|
||||
# Extract viewport dimensions from context params
|
||||
viewport_width = ctx.params.viewport_width if ctx.params else 80
|
||||
viewport_height = ctx.params.viewport_height if ctx.params else 24
|
||||
return self._camera.apply(data, viewport_width, viewport_height)
|
||||
return data
|
||||
143
engine/pipeline/adapters/data_source.py
Normal file
143
engine/pipeline/adapters/data_source.py
Normal file
@@ -0,0 +1,143 @@
|
||||
"""
|
||||
Stage adapters - Bridge existing components to the Stage interface.
|
||||
|
||||
This module provides adapters that wrap existing components
|
||||
(DataSource) as Stage implementations.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from engine.data_sources import SourceItem
|
||||
from engine.pipeline.core import DataType, PipelineContext, Stage
|
||||
|
||||
|
||||
class DataSourceStage(Stage):
|
||||
"""Adapter wrapping DataSource as a Stage."""
|
||||
|
||||
def __init__(self, data_source, name: str = "headlines"):
|
||||
self._source = data_source
|
||||
self.name = name
|
||||
self.category = "source"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"source.{self.name}"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return set()
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.NONE} # Sources don't take input
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Fetch data from source."""
|
||||
if hasattr(self._source, "get_items"):
|
||||
return self._source.get_items()
|
||||
return data
|
||||
|
||||
|
||||
class PassthroughStage(Stage):
|
||||
"""Simple stage that passes data through unchanged.
|
||||
|
||||
Used for sources that already provide the data in the correct format
|
||||
(e.g., pipeline introspection that outputs text directly).
|
||||
"""
|
||||
|
||||
def __init__(self, name: str = "passthrough"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Pass data through unchanged."""
|
||||
return data
|
||||
|
||||
|
||||
class SourceItemsToBufferStage(Stage):
|
||||
"""Convert SourceItem objects to text buffer.
|
||||
|
||||
Takes a list of SourceItem objects and extracts their content,
|
||||
splitting on newlines to create a proper text buffer for display.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str = "items-to-buffer"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Convert SourceItem list to text buffer."""
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
# If already a list of strings, return as-is
|
||||
if isinstance(data, list) and data and isinstance(data[0], str):
|
||||
return data
|
||||
|
||||
# If it's a list of SourceItem, extract content
|
||||
if isinstance(data, list):
|
||||
result = []
|
||||
for item in data:
|
||||
if isinstance(item, SourceItem):
|
||||
# Split content by newline to get individual lines
|
||||
lines = item.content.split("\n")
|
||||
result.extend(lines)
|
||||
elif hasattr(item, "content"): # Has content attribute
|
||||
lines = str(item.content).split("\n")
|
||||
result.extend(lines)
|
||||
else:
|
||||
result.append(str(item))
|
||||
return result
|
||||
|
||||
# Single item
|
||||
if isinstance(data, SourceItem):
|
||||
return data.content.split("\n")
|
||||
|
||||
return [str(data)]
|
||||
50
engine/pipeline/adapters/display.py
Normal file
50
engine/pipeline/adapters/display.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Adapter wrapping Display as a Stage."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from engine.pipeline.core import PipelineContext, Stage
|
||||
|
||||
|
||||
class DisplayStage(Stage):
|
||||
"""Adapter wrapping Display as a Stage."""
|
||||
|
||||
def __init__(self, display, name: str = "terminal"):
|
||||
self._display = display
|
||||
self.name = name
|
||||
self.category = "display"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"display.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"render.output"} # Display needs rendered content
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER} # Display consumes rendered text
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.NONE} # Display is a terminal stage (no output)
|
||||
|
||||
def init(self, ctx: PipelineContext) -> bool:
|
||||
w = ctx.params.viewport_width if ctx.params else 80
|
||||
h = ctx.params.viewport_height if ctx.params else 24
|
||||
result = self._display.init(w, h, reuse=False)
|
||||
return result is not False
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Output data to display."""
|
||||
if data is not None:
|
||||
self._display.show(data)
|
||||
return data
|
||||
|
||||
def cleanup(self) -> None:
|
||||
self._display.cleanup()
|
||||
103
engine/pipeline/adapters/effect_plugin.py
Normal file
103
engine/pipeline/adapters/effect_plugin.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Adapter wrapping EffectPlugin as a Stage."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from engine.pipeline.core import PipelineContext, Stage
|
||||
|
||||
|
||||
class EffectPluginStage(Stage):
|
||||
"""Adapter wrapping EffectPlugin as a Stage."""
|
||||
|
||||
def __init__(self, effect_plugin, name: str = "effect"):
|
||||
self._effect = effect_plugin
|
||||
self.name = name
|
||||
self.category = "effect"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
"""Return stage_type based on effect name.
|
||||
|
||||
HUD effects are overlays.
|
||||
"""
|
||||
if self.name == "hud":
|
||||
return "overlay"
|
||||
return self.category
|
||||
|
||||
@property
|
||||
def render_order(self) -> int:
|
||||
"""Return render_order based on effect type.
|
||||
|
||||
HUD effects have high render_order to appear on top.
|
||||
"""
|
||||
if self.name == "hud":
|
||||
return 100 # High order for overlays
|
||||
return 0
|
||||
|
||||
@property
|
||||
def is_overlay(self) -> bool:
|
||||
"""Return True for HUD effects.
|
||||
|
||||
HUD is an overlay - it composes on top of the buffer
|
||||
rather than transforming it for the next stage.
|
||||
"""
|
||||
return self.name == "hud"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {f"effect.{self.name}"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return set()
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
from engine.pipeline.core import DataType
|
||||
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Process data through the effect."""
|
||||
if data is None:
|
||||
return None
|
||||
from engine.effects.types import EffectContext, apply_param_bindings
|
||||
|
||||
w = ctx.params.viewport_width if ctx.params else 80
|
||||
h = ctx.params.viewport_height if ctx.params else 24
|
||||
frame = ctx.params.frame_number if ctx.params else 0
|
||||
|
||||
effect_ctx = EffectContext(
|
||||
terminal_width=w,
|
||||
terminal_height=h,
|
||||
scroll_cam=0,
|
||||
ticker_height=h,
|
||||
camera_x=0,
|
||||
mic_excess=0.0,
|
||||
grad_offset=(frame * 0.01) % 1.0,
|
||||
frame_number=frame,
|
||||
has_message=False,
|
||||
items=ctx.get("items", []),
|
||||
)
|
||||
|
||||
# Copy sensor state from PipelineContext to EffectContext
|
||||
for key, value in ctx.state.items():
|
||||
if key.startswith("sensor."):
|
||||
effect_ctx.set_state(key, value)
|
||||
|
||||
# Copy metrics from PipelineContext to EffectContext
|
||||
if "metrics" in ctx.state:
|
||||
effect_ctx.set_state("metrics", ctx.state["metrics"])
|
||||
|
||||
# Apply sensor param bindings if effect has them
|
||||
if hasattr(self._effect, "param_bindings") and self._effect.param_bindings:
|
||||
bound_config = apply_param_bindings(self._effect, effect_ctx)
|
||||
self._effect.configure(bound_config)
|
||||
|
||||
return self._effect.process(data, effect_ctx)
|
||||
38
engine/pipeline/adapters/factory.py
Normal file
38
engine/pipeline/adapters/factory.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""Factory functions for creating stage instances."""
|
||||
|
||||
from engine.pipeline.adapters.camera import CameraStage
|
||||
from engine.pipeline.adapters.data_source import DataSourceStage
|
||||
from engine.pipeline.adapters.display import DisplayStage
|
||||
from engine.pipeline.adapters.effect_plugin import EffectPluginStage
|
||||
from engine.pipeline.adapters.transform import FontStage
|
||||
|
||||
|
||||
def create_stage_from_display(display, name: str = "terminal") -> DisplayStage:
|
||||
"""Create a DisplayStage from a display instance."""
|
||||
return DisplayStage(display, name=name)
|
||||
|
||||
|
||||
def create_stage_from_effect(effect_plugin, name: str) -> EffectPluginStage:
|
||||
"""Create an EffectPluginStage from an effect plugin."""
|
||||
return EffectPluginStage(effect_plugin, name=name)
|
||||
|
||||
|
||||
def create_stage_from_source(data_source, name: str = "headlines") -> DataSourceStage:
|
||||
"""Create a DataSourceStage from a data source."""
|
||||
return DataSourceStage(data_source, name=name)
|
||||
|
||||
|
||||
def create_stage_from_camera(camera, name: str = "vertical") -> CameraStage:
|
||||
"""Create a CameraStage from a camera instance."""
|
||||
return CameraStage(camera, name=name)
|
||||
|
||||
|
||||
def create_stage_from_font(
|
||||
font_path: str | None = None,
|
||||
font_size: int | None = None,
|
||||
font_ref: str | None = "default",
|
||||
name: str = "font",
|
||||
) -> FontStage:
|
||||
"""Create a FontStage with specified font configuration."""
|
||||
# FontStage currently doesn't use these parameters but keeps them for compatibility
|
||||
return FontStage(name=name)
|
||||
265
engine/pipeline/adapters/transform.py
Normal file
265
engine/pipeline/adapters/transform.py
Normal file
@@ -0,0 +1,265 @@
|
||||
"""Adapters for transform stages (viewport, font, image, canvas)."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import engine.render
|
||||
from engine.data_sources import SourceItem
|
||||
from engine.pipeline.core import DataType, PipelineContext, Stage
|
||||
|
||||
|
||||
def estimate_simple_height(text: str, width: int) -> int:
|
||||
"""Estimate height in terminal rows using simple word wrap.
|
||||
|
||||
Uses conservative estimation suitable for headlines.
|
||||
Each wrapped line is approximately 6 terminal rows (big block rendering).
|
||||
"""
|
||||
words = text.split()
|
||||
if not words:
|
||||
return 6
|
||||
|
||||
lines = 1
|
||||
current_len = 0
|
||||
for word in words:
|
||||
word_len = len(word)
|
||||
if current_len + word_len + 1 > width - 4: # -4 for margins
|
||||
lines += 1
|
||||
current_len = word_len
|
||||
else:
|
||||
current_len += word_len + 1
|
||||
|
||||
return lines * 6 # 6 rows per line for big block rendering
|
||||
|
||||
|
||||
class ViewportFilterStage(Stage):
|
||||
"""Filter items to viewport height based on rendered height."""
|
||||
|
||||
def __init__(self, name: str = "viewport-filter"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
self._layout: list[int] = []
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"source.filtered"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Filter items to viewport height based on rendered height."""
|
||||
if data is None:
|
||||
return data
|
||||
|
||||
if not isinstance(data, list):
|
||||
return data
|
||||
|
||||
if not data:
|
||||
return []
|
||||
|
||||
# Get viewport parameters from context
|
||||
viewport_height = ctx.params.viewport_height if ctx.params else 24
|
||||
viewport_width = ctx.params.viewport_width if ctx.params else 80
|
||||
camera_y = ctx.get("camera_y", 0)
|
||||
|
||||
# Estimate height for each item and cache layout
|
||||
self._layout = []
|
||||
cumulative_heights = []
|
||||
current_height = 0
|
||||
|
||||
for item in data:
|
||||
title = item.content if isinstance(item, SourceItem) else str(item)
|
||||
# Use simple height estimation (not PIL-based)
|
||||
estimated_height = estimate_simple_height(title, viewport_width)
|
||||
self._layout.append(estimated_height)
|
||||
current_height += estimated_height
|
||||
cumulative_heights.append(current_height)
|
||||
|
||||
# Find visible range based on camera_y and viewport_height
|
||||
# camera_y is the scroll offset (how many rows are scrolled up)
|
||||
start_y = camera_y
|
||||
end_y = camera_y + viewport_height
|
||||
|
||||
# Find start index (first item that intersects with visible range)
|
||||
start_idx = 0
|
||||
for i, total_h in enumerate(cumulative_heights):
|
||||
if total_h > start_y:
|
||||
start_idx = i
|
||||
break
|
||||
|
||||
# Find end index (first item that extends beyond visible range)
|
||||
end_idx = len(data)
|
||||
for i, total_h in enumerate(cumulative_heights):
|
||||
if total_h >= end_y:
|
||||
end_idx = i + 1
|
||||
break
|
||||
|
||||
# Return visible items
|
||||
return data[start_idx:end_idx]
|
||||
|
||||
|
||||
class FontStage(Stage):
|
||||
"""Render items using font."""
|
||||
|
||||
def __init__(self, name: str = "font"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Render items to text buffer using font."""
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
if not isinstance(data, list):
|
||||
return [str(data)]
|
||||
|
||||
viewport_width = ctx.params.viewport_width if ctx.params else 80
|
||||
|
||||
result = []
|
||||
for item in data:
|
||||
if isinstance(item, SourceItem):
|
||||
title = item.content
|
||||
src = item.source
|
||||
ts = item.timestamp
|
||||
content_lines, _, _ = engine.render.make_block(
|
||||
title, src, ts, viewport_width
|
||||
)
|
||||
result.extend(content_lines)
|
||||
elif hasattr(item, "content"):
|
||||
title = str(item.content)
|
||||
content_lines, _, _ = engine.render.make_block(
|
||||
title, "", "", viewport_width
|
||||
)
|
||||
result.extend(content_lines)
|
||||
else:
|
||||
result.append(str(item))
|
||||
return result
|
||||
|
||||
|
||||
class ImageToTextStage(Stage):
|
||||
"""Convert image items to text."""
|
||||
|
||||
def __init__(self, name: str = "image-to-text"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = True
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Convert image items to text representation."""
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
if not isinstance(data, list):
|
||||
return [str(data)]
|
||||
|
||||
result = []
|
||||
for item in data:
|
||||
# Check if item is an image
|
||||
if hasattr(item, "image_path") or hasattr(item, "image_data"):
|
||||
# Placeholder: would normally render image to ASCII art
|
||||
result.append(f"[Image: {getattr(item, 'image_path', 'data')}]")
|
||||
elif isinstance(item, SourceItem):
|
||||
result.extend(item.content.split("\n"))
|
||||
else:
|
||||
result.append(str(item))
|
||||
return result
|
||||
|
||||
|
||||
class CanvasStage(Stage):
|
||||
"""Render items to canvas."""
|
||||
|
||||
def __init__(self, name: str = "canvas"):
|
||||
self.name = name
|
||||
self.category = "render"
|
||||
self.optional = False
|
||||
|
||||
@property
|
||||
def stage_type(self) -> str:
|
||||
return "render"
|
||||
|
||||
@property
|
||||
def capabilities(self) -> set[str]:
|
||||
return {"render.output"}
|
||||
|
||||
@property
|
||||
def dependencies(self) -> set[str]:
|
||||
return {"source"}
|
||||
|
||||
@property
|
||||
def inlet_types(self) -> set:
|
||||
return {DataType.SOURCE_ITEMS}
|
||||
|
||||
@property
|
||||
def outlet_types(self) -> set:
|
||||
return {DataType.TEXT_BUFFER}
|
||||
|
||||
def process(self, data: Any, ctx: PipelineContext) -> Any:
|
||||
"""Render items to canvas."""
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
if not isinstance(data, list):
|
||||
return [str(data)]
|
||||
|
||||
# Simple canvas rendering
|
||||
result = []
|
||||
for item in data:
|
||||
if isinstance(item, SourceItem):
|
||||
result.extend(item.content.split("\n"))
|
||||
else:
|
||||
result.append(str(item))
|
||||
return result
|
||||
@@ -49,6 +49,8 @@ class Pipeline:
|
||||
|
||||
Manages the execution of all stages in dependency order,
|
||||
handling initialization, processing, and cleanup.
|
||||
|
||||
Supports dynamic mutation during runtime via the mutation API.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -61,26 +63,231 @@ class Pipeline:
|
||||
self._stages: dict[str, Stage] = {}
|
||||
self._execution_order: list[str] = []
|
||||
self._initialized = False
|
||||
self._capability_map: dict[str, list[str]] = {}
|
||||
|
||||
self._metrics_enabled = self.config.enable_metrics
|
||||
self._frame_metrics: list[FrameMetrics] = []
|
||||
self._max_metrics_frames = 60
|
||||
self._current_frame_number = 0
|
||||
|
||||
def add_stage(self, name: str, stage: Stage) -> "Pipeline":
|
||||
"""Add a stage to the pipeline."""
|
||||
def add_stage(self, name: str, stage: Stage, initialize: bool = True) -> "Pipeline":
|
||||
"""Add a stage to the pipeline.
|
||||
|
||||
Args:
|
||||
name: Unique name for the stage
|
||||
stage: Stage instance to add
|
||||
initialize: If True, initialize the stage immediately
|
||||
|
||||
Returns:
|
||||
Self for method chaining
|
||||
"""
|
||||
self._stages[name] = stage
|
||||
if self._initialized and initialize:
|
||||
stage.init(self.context)
|
||||
return self
|
||||
|
||||
def remove_stage(self, name: str) -> None:
|
||||
"""Remove a stage from the pipeline."""
|
||||
if name in self._stages:
|
||||
del self._stages[name]
|
||||
def remove_stage(self, name: str, cleanup: bool = True) -> Stage | None:
|
||||
"""Remove a stage from the pipeline.
|
||||
|
||||
Args:
|
||||
name: Name of the stage to remove
|
||||
cleanup: If True, call cleanup() on the removed stage
|
||||
|
||||
Returns:
|
||||
The removed stage, or None if not found
|
||||
"""
|
||||
stage = self._stages.pop(name, None)
|
||||
if stage and cleanup:
|
||||
try:
|
||||
stage.cleanup()
|
||||
except Exception:
|
||||
pass
|
||||
return stage
|
||||
|
||||
def replace_stage(
|
||||
self, name: str, new_stage: Stage, preserve_state: bool = True
|
||||
) -> Stage | None:
|
||||
"""Replace a stage in the pipeline with a new one.
|
||||
|
||||
Args:
|
||||
name: Name of the stage to replace
|
||||
new_stage: New stage instance
|
||||
preserve_state: If True, copy relevant state from old stage
|
||||
|
||||
Returns:
|
||||
The old stage, or None if not found
|
||||
"""
|
||||
old_stage = self._stages.get(name)
|
||||
if not old_stage:
|
||||
return None
|
||||
|
||||
if preserve_state:
|
||||
self._copy_stage_state(old_stage, new_stage)
|
||||
|
||||
old_stage.cleanup()
|
||||
self._stages[name] = new_stage
|
||||
new_stage.init(self.context)
|
||||
|
||||
if self._initialized:
|
||||
self._rebuild()
|
||||
|
||||
return old_stage
|
||||
|
||||
def swap_stages(self, name1: str, name2: str) -> bool:
|
||||
"""Swap two stages in the pipeline.
|
||||
|
||||
Args:
|
||||
name1: First stage name
|
||||
name2: Second stage name
|
||||
|
||||
Returns:
|
||||
True if successful, False if either stage not found
|
||||
"""
|
||||
stage1 = self._stages.get(name1)
|
||||
stage2 = self._stages.get(name2)
|
||||
|
||||
if not stage1 or not stage2:
|
||||
return False
|
||||
|
||||
self._stages[name1] = stage2
|
||||
self._stages[name2] = stage1
|
||||
|
||||
if self._initialized:
|
||||
self._rebuild()
|
||||
|
||||
return True
|
||||
|
||||
def move_stage(
|
||||
self, name: str, after: str | None = None, before: str | None = None
|
||||
) -> bool:
|
||||
"""Move a stage's position in execution order.
|
||||
|
||||
Args:
|
||||
name: Stage to move
|
||||
after: Place this stage after this stage name
|
||||
before: Place this stage before this stage name
|
||||
|
||||
Returns:
|
||||
True if successful, False if stage not found
|
||||
"""
|
||||
if name not in self._stages:
|
||||
return False
|
||||
|
||||
if not self._initialized:
|
||||
return False
|
||||
|
||||
current_order = list(self._execution_order)
|
||||
if name not in current_order:
|
||||
return False
|
||||
|
||||
current_order.remove(name)
|
||||
|
||||
if after and after in current_order:
|
||||
idx = current_order.index(after) + 1
|
||||
current_order.insert(idx, name)
|
||||
elif before and before in current_order:
|
||||
idx = current_order.index(before)
|
||||
current_order.insert(idx, name)
|
||||
else:
|
||||
current_order.append(name)
|
||||
|
||||
self._execution_order = current_order
|
||||
return True
|
||||
|
||||
def _copy_stage_state(self, old_stage: Stage, new_stage: Stage) -> None:
|
||||
"""Copy relevant state from old stage to new stage during replacement.
|
||||
|
||||
Args:
|
||||
old_stage: The old stage being replaced
|
||||
new_stage: The new stage
|
||||
"""
|
||||
if hasattr(old_stage, "_enabled"):
|
||||
new_stage._enabled = old_stage._enabled
|
||||
|
||||
def _rebuild(self) -> None:
|
||||
"""Rebuild execution order after mutation without full reinitialization."""
|
||||
self._capability_map = self._build_capability_map()
|
||||
self._execution_order = self._resolve_dependencies()
|
||||
try:
|
||||
self._validate_dependencies()
|
||||
self._validate_types()
|
||||
except StageError:
|
||||
pass
|
||||
|
||||
def get_stage(self, name: str) -> Stage | None:
|
||||
"""Get a stage by name."""
|
||||
return self._stages.get(name)
|
||||
|
||||
def enable_stage(self, name: str) -> bool:
|
||||
"""Enable a stage in the pipeline.
|
||||
|
||||
Args:
|
||||
name: Stage name to enable
|
||||
|
||||
Returns:
|
||||
True if successful, False if stage not found
|
||||
"""
|
||||
stage = self._stages.get(name)
|
||||
if stage:
|
||||
stage.set_enabled(True)
|
||||
return True
|
||||
return False
|
||||
|
||||
def disable_stage(self, name: str) -> bool:
|
||||
"""Disable a stage in the pipeline.
|
||||
|
||||
Args:
|
||||
name: Stage name to disable
|
||||
|
||||
Returns:
|
||||
True if successful, False if stage not found
|
||||
"""
|
||||
stage = self._stages.get(name)
|
||||
if stage:
|
||||
stage.set_enabled(False)
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_stage_info(self, name: str) -> dict | None:
|
||||
"""Get detailed information about a stage.
|
||||
|
||||
Args:
|
||||
name: Stage name
|
||||
|
||||
Returns:
|
||||
Dictionary with stage information, or None if not found
|
||||
"""
|
||||
stage = self._stages.get(name)
|
||||
if not stage:
|
||||
return None
|
||||
|
||||
return {
|
||||
"name": name,
|
||||
"category": stage.category,
|
||||
"stage_type": stage.stage_type,
|
||||
"enabled": stage.is_enabled(),
|
||||
"optional": stage.optional,
|
||||
"capabilities": list(stage.capabilities),
|
||||
"dependencies": list(stage.dependencies),
|
||||
"inlet_types": [dt.name for dt in stage.inlet_types],
|
||||
"outlet_types": [dt.name for dt in stage.outlet_types],
|
||||
"render_order": stage.render_order,
|
||||
"is_overlay": stage.is_overlay,
|
||||
}
|
||||
|
||||
def get_pipeline_info(self) -> dict:
|
||||
"""Get comprehensive information about the pipeline.
|
||||
|
||||
Returns:
|
||||
Dictionary with pipeline state
|
||||
"""
|
||||
return {
|
||||
"stages": {name: self.get_stage_info(name) for name in self._stages},
|
||||
"execution_order": self._execution_order.copy(),
|
||||
"initialized": self._initialized,
|
||||
"stage_count": len(self._stages),
|
||||
}
|
||||
|
||||
def build(self) -> "Pipeline":
|
||||
"""Build execution order based on dependencies."""
|
||||
self._capability_map = self._build_capability_map()
|
||||
|
||||
@@ -315,6 +315,68 @@ class UIPanel:
|
||||
else:
|
||||
return "└" + "─" * (width - 2) + "┘"
|
||||
|
||||
def execute_command(self, command: dict) -> bool:
|
||||
"""Execute a command from external control (e.g., WebSocket).
|
||||
|
||||
Supported commands:
|
||||
- {"action": "toggle_stage", "stage": "stage_name"}
|
||||
- {"action": "select_stage", "stage": "stage_name"}
|
||||
- {"action": "adjust_param", "stage": "stage_name", "param": "param_name", "delta": 0.1}
|
||||
- {"action": "change_preset", "preset": "preset_name"}
|
||||
- {"action": "cycle_preset", "direction": 1}
|
||||
|
||||
Returns:
|
||||
True if command was handled, False if not
|
||||
"""
|
||||
action = command.get("action")
|
||||
|
||||
if action == "toggle_stage":
|
||||
stage_name = command.get("stage")
|
||||
if stage_name in self.stages:
|
||||
self.toggle_stage(stage_name)
|
||||
self._emit_event(
|
||||
"stage_toggled",
|
||||
stage_name=stage_name,
|
||||
enabled=self.stages[stage_name].enabled,
|
||||
)
|
||||
return True
|
||||
|
||||
elif action == "select_stage":
|
||||
stage_name = command.get("stage")
|
||||
if stage_name in self.stages:
|
||||
self.select_stage(stage_name)
|
||||
self._emit_event("stage_selected", stage_name=stage_name)
|
||||
return True
|
||||
|
||||
elif action == "adjust_param":
|
||||
stage_name = command.get("stage")
|
||||
param_name = command.get("param")
|
||||
delta = command.get("delta", 0.1)
|
||||
if stage_name == self.selected_stage and param_name:
|
||||
self._focused_param = param_name
|
||||
self.adjust_selected_param(delta)
|
||||
self._emit_event(
|
||||
"param_changed",
|
||||
stage_name=stage_name,
|
||||
param_name=param_name,
|
||||
value=self.stages[stage_name].params.get(param_name),
|
||||
)
|
||||
return True
|
||||
|
||||
elif action == "change_preset":
|
||||
preset_name = command.get("preset")
|
||||
if preset_name in self._presets:
|
||||
self._current_preset = preset_name
|
||||
self._emit_event("preset_changed", preset_name=preset_name)
|
||||
return True
|
||||
|
||||
elif action == "cycle_preset":
|
||||
direction = command.get("direction", 1)
|
||||
self.cycle_preset(direction)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def process_key_event(self, key: str | int, modifiers: int = 0) -> bool:
|
||||
"""Process a keyboard event.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user