feat: imgage feature

This commit is contained in:
2026-04-01 12:31:23 +02:00
parent 175de893aa
commit 528d2d8519
11 changed files with 431 additions and 7 deletions

View File

@@ -19,11 +19,12 @@ from uframe.ir import (
Heading, Text, Label, Divider, Link, ListNode, ListItem,
Gauge, Sparkline, Status, Table,
Form, Field, Password, Radio, Checkbox, FormButton,
BigTitle,
BigTitle, ImageNode,
HeadingLevel, DividerStyle, ListStyle, Align, BorderWeight,
)
from uframe.themes import ThemeDef, THEME_DEFAULT
from uframe.fonts import render_big_text, get_text_width, FONT_HEIGHTS
from uframe.imaging import convert_image
def _align_text(text: str, width: int, align: Align) -> str:
@@ -222,6 +223,31 @@ def paint(node: IRNode, grid: CharGrid, theme: ThemeDef | None = None) -> None:
grid.put(x, y, char, style=CellStyle(fg=color))
grid.put_text(x + 2, y, node.label)
elif isinstance(node, ImageNode):
style = CellStyle(fg=node.style.fg or th.palette.accent)
try:
lines = convert_image(node.path, node.mode, node.img_width,
node.dither, node.invert, node.use_color)
# Center if aligned
offset = 0
actual_w = len(lines[0]) if lines else 0
if node.style.align == Align.CENTER:
offset = max(0, (w - actual_w) // 2)
elif node.style.align == Align.RIGHT:
offset = max(0, w - actual_w)
for row_i, line in enumerate(lines):
if y + row_i < grid.height:
grid.put_text(x + offset, y + row_i, line, style=style)
# Caption
if node.caption and y + len(lines) < grid.height:
cap_style = CellStyle(fg=th.palette.label, italic=True)
grid.put_text(x + offset, y + len(lines), node.caption, style=cap_style)
except (ImportError, FileNotFoundError) as e:
# Render placeholder if image can't be loaded
grid.put_text(x, y, f"[image: {node.path}]", style=CellStyle(fg=th.palette.muted))
elif isinstance(node, BigTitle):
style = CellStyle(fg=node.style.fg or th.palette.accent, bold=True)