75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
"""µFrame — A DSL for rich terminal UIs rendered as ASCII and Micron.
|
|
|
|
Public API:
|
|
compile(source, width=64) → CompileResult
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from uframe.errors import CompileWarning, UFrameError
|
|
from uframe.parser import parse
|
|
from uframe.measure import measure
|
|
from uframe.layout import layout
|
|
from uframe.paint import paint
|
|
from uframe.borders import merge_borders
|
|
from uframe.grid import CharGrid
|
|
from uframe.emit_ascii import emit_ascii
|
|
from uframe.emit_micron import emit_micron
|
|
|
|
|
|
@dataclass
|
|
class CompileResult:
|
|
"""Result of compiling a .uf source."""
|
|
ascii: str = ""
|
|
micron: str = ""
|
|
warnings: list[CompileWarning] = field(default_factory=list)
|
|
|
|
|
|
def compile(source: str, width: int = 64) -> CompileResult:
|
|
"""Compile a µFrame .uf source string into ASCII and Micron output.
|
|
|
|
Args:
|
|
source: the .uf DSL source text
|
|
width: page width in characters (default 64)
|
|
|
|
Returns:
|
|
CompileResult with ascii, micron, and any warnings
|
|
|
|
Raises:
|
|
ParseError: if the source cannot be parsed
|
|
LayoutError: if layout constraints fail
|
|
"""
|
|
warnings: list[CompileWarning] = []
|
|
|
|
# 1. Parse
|
|
page = parse(source)
|
|
if page.width == 64 and width != 64:
|
|
page.width = width
|
|
|
|
w = page.width
|
|
|
|
# 2. Measure
|
|
measure(page, w)
|
|
|
|
# 3. Layout
|
|
total_h = layout(page, 0, 0, w, page.pref_height + 100)
|
|
|
|
# 4. Create grid and paint
|
|
grid = CharGrid(w, max(total_h, 1))
|
|
paint(page, grid)
|
|
|
|
# 5. Merge borders
|
|
merge_borders(grid)
|
|
|
|
# 6. Emit
|
|
ascii_out = emit_ascii(grid)
|
|
micron_out = emit_micron(grid, page_title=page.title)
|
|
|
|
return CompileResult(
|
|
ascii=ascii_out,
|
|
micron=micron_out,
|
|
warnings=warnings,
|
|
)
|