import codecs import io import logging import math import shutil import sys import unicodedata from collections.abc import Callable, Iterable, Mapping, Sized from datetime import datetime from functools import lru_cache from itertools import pairwise from types import ModuleType from typing import IO, Any, Literal, Optional, TextIO, cast import rich.cells from rich import box from rich._log_render import FormatTimeCallable, LogRender from rich.color import Color, blend_rgb from rich.console import Console, ConsoleOptions, ConsoleRenderable, HighlighterType, RenderableType, RenderResult from rich.emoji import EmojiVariant from rich.highlighter import Highlighter, ReprHighlighter from rich.live import Live from rich.logging import RichHandler from rich.measure import Measurement from rich.padding import Padding, PaddingDimensions from rich.panel import Panel from rich.progress import BarColumn, Task from rich.rule import Rule from rich.status import Status from rich.style import Style, StyleType from rich.table import Table from rich.text import Text, TextType from rich.theme import Theme from unshackle.core.config import config from unshackle.core.themes import DEFAULT_THEME, PALETTES, apply_help_theme, resolve_palette class ComfyLogRenderer(LogRender): def __call__( self, console: "Console", renderables: Iterable["ConsoleRenderable"], log_time: datetime | None = None, time_format: str | FormatTimeCallable | None = None, level: TextType = "", path: str | None = None, line_no: int | None = None, link_path: str | None = None, ) -> "Table": from rich.containers import Renderables output = Table.grid(padding=(0, 5), pad_edge=True) output.expand = True if self.show_time: output.add_column(style="log.time") if self.show_level: output.add_column(style="log.level", width=self.level_width) output.add_column(ratio=1, style="log.message", overflow="fold") if self.show_path and path: output.add_column(style="log.path") row: list[RenderableType] = [] if self.show_time: log_time = log_time or console.get_datetime() time_format = time_format or self.time_format if callable(time_format): log_time_display = time_format(log_time) else: log_time_display = Text(log_time.strftime(time_format)) if log_time_display == self._last_time and self.omit_repeated_times: row.append(Text(" " * len(log_time_display))) else: row.append(log_time_display) self._last_time = log_time_display if self.show_level: row.append(level) row.append(Renderables(renderables)) if self.show_path and path: path_text = Text() path_text.append(path, style=f"link file://{link_path}" if link_path else "") if line_no: path_text.append(":") path_text.append( f"{line_no}", style=f"link file://{link_path}#{line_no}" if link_path else "", ) row.append(path_text) output.add_row(*row) return output class ComfyRichHandler(RichHandler): def __init__( self, level: int | str = logging.NOTSET, console: Console | None = None, *, show_time: bool = True, omit_repeated_times: bool = True, show_level: bool = True, show_path: bool = True, enable_link_path: bool = True, highlighter: Highlighter | None = None, markup: bool = False, rich_tracebacks: bool = False, tracebacks_width: int | None = None, tracebacks_extra_lines: int = 3, tracebacks_theme: str | None = None, tracebacks_word_wrap: bool = True, tracebacks_show_locals: bool = False, tracebacks_suppress: Iterable[str | ModuleType] = (), locals_max_length: int = 10, locals_max_string: int = 80, log_time_format: str | FormatTimeCallable = "[%x %X]", keywords: list[str] | None = None, log_renderer: LogRender | None = None, ) -> None: super().__init__( level=level, console=console, show_time=show_time, omit_repeated_times=omit_repeated_times, show_level=show_level, show_path=show_path, enable_link_path=enable_link_path, highlighter=highlighter, markup=markup, rich_tracebacks=rich_tracebacks, tracebacks_width=tracebacks_width, tracebacks_extra_lines=tracebacks_extra_lines, tracebacks_theme=tracebacks_theme, tracebacks_word_wrap=tracebacks_word_wrap, tracebacks_show_locals=tracebacks_show_locals, tracebacks_suppress=tracebacks_suppress, locals_max_length=locals_max_length, locals_max_string=locals_max_string, log_time_format=log_time_format, keywords=keywords, ) if log_renderer: self._log_render = log_renderer REPR_HIGHLIGHTER = ReprHighlighter() class ComfyConsole(Console): """A comfy high level console interface. Args: color_system (str, optional): The colour system supported by your terminal, either ``"standard"``, ``"256"`` or ``"truecolor"``. Leave as ``"auto"`` to autodetect. force_terminal (Optional[bool], optional): Enable/disable terminal control codes, or None to auto-detect terminal. Defaults to None. force_jupyter (Optional[bool], optional): Enable/disable Jupyter rendering, or None to auto-detect Jupyter. Defaults to None. force_interactive (Optional[bool], optional): Enable/disable interactive mode, or None to auto-detect. Defaults to None. soft_wrap (Optional[bool], optional): Set soft wrap default on print method. Defaults to False. theme (Theme, optional): An optional style theme object, or ``None`` for default theme. stderr (bool, optional): Use stderr rather than stdout if ``file`` is not specified. Defaults to False. file (IO, optional): A file object where the console should write to. Defaults to stdout. quiet (bool, Optional): Boolean to suppress all output. Defaults to False. width (int, optional): The width of the terminal. Leave as default to auto-detect width. height (int, optional): The height of the terminal. Leave as default to auto-detect height. style (StyleType, optional): Style to apply to all output, or None for no style. Defaults to None. no_color (Optional[bool], optional): Enabled no colour mode, or None to auto-detect. Defaults to None. tab_size (int, optional): Number of spaces used to replace a tab character. Defaults to 8. record (bool, optional): Boolean to enable recording of terminal output, required to call :meth:`export_html`, :meth:`export_svg`, and :meth:`export_text`. Defaults to False. markup (bool, optional): Boolean to enable :ref:`console_markup`. Defaults to True. emoji (bool, optional): Enable emoji code. Defaults to True. emoji_variant (str, optional): Optional emoji variant, either "text" or "emoji". Defaults to None. highlight (bool, optional): Enable automatic highlighting. Defaults to True. log_time (bool, optional): Boolean to enable logging of time by :meth:`log` methods. Defaults to True. log_path (bool, optional): Boolean to enable the logging of the caller by :meth:`log`. Defaults to True. log_time_format (Union[str, TimeFormatterCallable], optional): If ``log_time`` is enabled, either string for strftime or callable that formats the time. Defaults to "[%X] ". highlighter (HighlighterType, optional): Default highlighter. legacy_windows (bool, optional): Enable legacy Windows mode, or ``None`` to auto-detect. Defaults to ``None``. safe_box (bool, optional): Restrict box options that do not render on legacy Windows. get_datetime (Callable[[], datetime], optional): Callable that gets the current time as a datetime.datetime object (used by Console.log), or None for datetime.now. get_time (Callable[[], time], optional): Callable that gets the current time in seconds, default uses time.monotonic. """ def __init__( self, *, color_system: Literal["auto", "standard", "256", "truecolor", "windows"] | None = "auto", force_terminal: bool | None = None, force_jupyter: bool | None = None, force_interactive: bool | None = None, soft_wrap: bool = False, theme: Theme | None = None, stderr: bool = False, file: IO[str] | None = None, quiet: bool = False, width: int | None = None, height: int | None = None, style: StyleType | None = None, no_color: bool | None = None, tab_size: int = 8, record: bool = False, markup: bool = True, emoji: bool = True, emoji_variant: EmojiVariant | None = None, highlight: bool = True, log_time: bool = True, log_path: bool = True, log_time_format: str | FormatTimeCallable = "[%X]", highlighter: Optional["HighlighterType"] = REPR_HIGHLIGHTER, legacy_windows: bool | None = None, safe_box: bool = True, get_datetime: Callable[[], datetime] | None = None, get_time: Callable[[], float] | None = None, _environ: Mapping[str, str] | None = None, log_renderer: LogRender | None = None, ) -> None: super().__init__( color_system=color_system, force_terminal=force_terminal, force_jupyter=force_jupyter, force_interactive=force_interactive, soft_wrap=soft_wrap, theme=theme, stderr=stderr, file=file, quiet=quiet, width=width, height=height, style=style, no_color=no_color, tab_size=tab_size, record=record, markup=markup, emoji=emoji, emoji_variant=emoji_variant, highlight=highlight, log_time=log_time, log_path=log_path, log_time_format=log_time_format, highlighter=highlighter, legacy_windows=legacy_windows, safe_box=safe_box, get_datetime=get_datetime, get_time=get_time, _environ=_environ, ) if log_renderer: self._log_render = log_renderer def status( # type: ignore[override] self, status: RenderableType, *, spinner: str = "dots", spinner_style: StyleType = "status.spinner", speed: float = 1.0, refresh_per_second: float = 12.5, pad: PaddingDimensions = (0, 5), ) -> Live | Status: """Display a comfy status and spinner. Args: status (RenderableType): A status renderable (str or Text typically). spinner (str, optional): Name of spinner animation (see python -m rich.spinner). Defaults to "dots". spinner_style (StyleType, optional): Style of spinner. Defaults to "status.spinner". speed (float, optional): Speed factor for spinner animation. Defaults to 1.0. refresh_per_second (float, optional): Number of refreshes per second. Defaults to 12.5. pad (Union[int, Tuple[int]]): Padding for top, right, bottom, and left borders. Give 1, 2, or 4 integers (CSS style). Returns: Status: A Status object you can use as a context manager. """ status_renderable = super().status( status=status, spinner=spinner, spinner_style=spinner_style, speed=speed, refresh_per_second=refresh_per_second, ) if pad: top, right, bottom, left = Padding.unpack(pad) renderable_width = len(cast(Sized, status_renderable.status)) spinner_width = len(cast(Sized, status_renderable.renderable.text)) status_width = spinner_width + renderable_width available_width = self.width - status_width if available_width > right: # fill up the available width with padding to apply bg color right = available_width - right padding = Padding(status_renderable, (top, right, bottom, left)) return SyncLive(padding, console=self, transient=True) return status_renderable def input( self, prompt: TextType = "", *, markup: bool = True, emoji: bool = True, password: bool = False, stream: TextIO | None = None, ) -> str: """Displays a prompt and waits for input from the user, temporarily pausing any active Live displays.""" # Find active Live displays to prevent overlap / disappearing prompt. # We copy the list because stop() will modify console._live_stack. active_lives = list(self._live_stack) for live in active_lives: live.stop() try: return super().input( prompt=prompt, markup=markup, emoji=emoji, password=password, stream=stream, ) finally: for live in reversed(active_lives): live.start() primary_scheme: dict[str, str] = resolve_palette(config.theme) or {} if not primary_scheme: logging.getLogger("console").warning( "Unknown theme %r, falling back to %s (available: %s)", config.theme, DEFAULT_THEME, ", ".join(PALETTES), ) primary_scheme = dict(PALETTES[DEFAULT_THEME]) apply_help_theme(primary_scheme) primary_scheme["none"] = primary_scheme["text"] primary_scheme["grey23"] = primary_scheme["black"] primary_scheme["magenta"] = primary_scheme["pink"] primary_scheme["bright_red"] = primary_scheme["red"] primary_scheme["bright_green"] = primary_scheme["green"] primary_scheme["bright_yellow"] = primary_scheme["yellow"] primary_scheme["bright_blue"] = primary_scheme["blue"] primary_scheme["bright_magenta"] = primary_scheme["pink"] primary_scheme["bright_cyan"] = primary_scheme["cyan"] if config.set_terminal_bg: primary_scheme["none"] += f" on {primary_scheme['bg']}" custom_colors = {"ascii.art": primary_scheme["pink"]} if config.set_terminal_bg: custom_colors["ascii.art"] += f" on {primary_scheme['bg']}" class SyncLive(Live): """Live that wraps each repaint in DEC 2026 begin/end-sync so the terminal draws it as one atomic frame.""" def refresh(self) -> None: if not self.console.is_terminal: super().refresh() return self.console.file.write("\x1b[?2026h") try: super().refresh() finally: self.console.file.write("\x1b[?2026l") self.console.file.flush() class GradientPulseBarColumn(BarColumn): LUT_SIZE = 32 def __init__(self, *args: Any, pulse_period: float = 40.0, pulse_speed: float = 15.0, **kwargs: Any) -> None: super().__init__(*args, **kwargs) self.pulse_period = pulse_period self.pulse_speed = pulse_speed lo = Color.parse(primary_scheme["dark_gray"]).triplet hi = Color.parse(primary_scheme["pink"]).triplet assert lo and hi self._lut = [ Style(color=Color.from_triplet(blend_rgb(lo, hi, i / (self.LUT_SIZE - 1)))) for i in range(self.LUT_SIZE) ] def render(self, task: Task) -> RenderableType: # type: ignore[override] if task.started and task.total is not None: return super().render(task) return _GradientPulse(self._lut, task.get_time() * self.pulse_speed, self.pulse_period, self.bar_width) class _GradientPulse: def __init__(self, lut: list[Style], offset: float, period: float, width: int | None) -> None: self.lut = lut self.offset = offset self.period = period self.width = width def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: width = min(self.width or options.max_width, options.max_width) bar = Text() for i in range(width): fade = (math.cos((i - self.offset) * math.tau / self.period) + 1) / 2 bar.append("━", self.lut[int(fade * (len(self.lut) - 1))]) yield bar def __rich_measure__(self, console: Console, options: ConsoleOptions) -> Measurement: return Measurement(4, self.width or options.max_width) def force_utf8_streams() -> None: """Make stdout and stderr write UTF-8. A redirected stream on Windows uses a legacy code page that cannot encode the console output.""" for stream in (sys.stdout, sys.stderr): if isinstance(stream, io.TextIOWrapper) and codecs.lookup(stream.encoding).name != "utf-8": stream.reconfigure(encoding="utf-8", errors="replace") force_utf8_streams() _rich_cell_size = rich.cells.get_character_cell_size @lru_cache(maxsize=4096) def _cell_size(character: str, *args: str) -> int: """Count a spacing combining mark (Mc, e.g. Devanagari ि) as one cell, as terminals draw it; Rich's table says 0.""" return 1 if unicodedata.category(character) == "Mc" else _rich_cell_size(character, *args) # the viramas that join the next consonant into one cluster (Unicode InCB=Linker); Tamil and Kannada do not _JOINING_VIRAMAS = frozenset("्্્୍్്") _rich_cell_len = rich.cells._cell_len def _cell_len(text: str, unicode_version: str) -> int: """Count a consonant joined by a virama (the न्द in हिन्दी) as no cell: terminals draw the conjunct in one.""" width = _rich_cell_len(text, unicode_version) if _JOINING_VIRAMAS.isdisjoint(text): return width return width - sum(prev in _JOINING_VIRAMAS and unicodedata.category(char) == "Lo" for prev, char in pairwise(text)) rich.cells.get_character_cell_size = _cell_size rich.cells._cell_len = _cell_len rich.cells.cached_cell_len.cache_clear() console = ComfyConsole( log_time=False, log_path=False, width=80, theme=Theme( { "bar.back": primary_scheme["dark_gray"], "bar.complete": primary_scheme["pink"], "bar.finished": primary_scheme["green"], "bar.pulse": primary_scheme["pink"], "black": primary_scheme["black"], "inspect.async_def": f"italic {primary_scheme['cyan']}", "progress.data.speed": "dark_orange", "repr.number": f"bold not italic {primary_scheme['cyan']}", "repr.number_complex": f"bold not italic {primary_scheme['cyan']}", "rule.line": primary_scheme["dark_gray"], "rule.text": primary_scheme["pink"], "tree.line": primary_scheme["dark_gray"], "status.spinner": primary_scheme["pink"], "progress.spinner": primary_scheme["pink"], **primary_scheme, **custom_colors, } ), log_renderer=ComfyLogRenderer(show_time=False, show_path=False), ) def listing_panel(renderable: RenderableType, title: str) -> Panel: """Box a listing in the shared panel style, so every listing restyles from one place.""" return Panel(renderable, title=title, box=box.SQUARE, border_style="bright_black") def title_rule(title: str) -> Rule: """A rule whose title is shown as given: brackets and emoji codes in it are text, not markup.""" text = console.render_str(title, markup=False, emoji=False) text.stylize("rule.text") return Rule(text) def listing_table(title: str | None = None, **kwargs: Any) -> Table: """Assemble a table in the style of the rich-click help tables, so listings and help screens match.""" kwargs.setdefault("box", box.HORIZONTALS) kwargs.setdefault("show_edge", False) # no top or bottom rule, so the title runs straight into the header kwargs.setdefault("border_style", "dark_gray") kwargs.setdefault("header_style", "text") kwargs.setdefault("pad_edge", False) return Table(title=title, title_style="rule.text", title_justify="left", **kwargs) def print_wide(renderable: RenderableType, pad: PaddingDimensions = (1, 3)) -> None: """Print a listing at the terminal width, indented like the rich-click help panels. The shared console holds 80 columns to keep logs and progress compact. Long values such as paths and URLs fold badly at that width, so this widens the console for one print and restores it after. It restores ``_width``, because on a legacy Windows console the ``width`` property reads one column less. """ fixed_width = console._width console.width = max(console.width, shutil.get_terminal_size().columns - console.legacy_windows) try: console.print(Padding(renderable, pad)) finally: console._width = fixed_width def print_listing(renderable: RenderableType) -> None: """Print a listing at the terminal width when ``wide_output`` is on, else at the shared console width.""" if config.wide_output: print_wide(renderable, (0, 5)) else: console.print(Padding(renderable, (0, 5))) _prompt_handler: Callable[[str, str | None], str] | None = None def set_prompt_handler(handler: Callable[[str, str | None], str] | None) -> None: """Route every :func:`prompt_user` call to *handler*, for a process with no terminal to prompt on.""" global _prompt_handler _prompt_handler = handler def qr_text(data: str, indent: int = 5) -> Text: """Draw *data* as a QR code in half blocks, dark on light whatever the theme, so a phone camera reads it.""" import segno rows = [list(row) for row in segno.make_qr(data).matrix_iter(border=2)] if len(rows) % 2: rows.append([False] * len(rows[0])) blocks = {(True, True): "\u2588", (True, False): "\u2580", (False, True): "\u2584", (False, False): " "} text = Text() for top, bottom in zip(rows[::2], rows[1::2], strict=True): text.append(" " * indent) text.append("".join(blocks[bool(t), bool(b)] for t, b in zip(top, bottom, strict=True)), style="black on white") text.append("\n") return text def read_qr_image(data: bytes) -> str | None: """Give the text of the QR code in a PNG image, to pass as ``qr=``. None when the image holds no code that reads.""" from unshackle.core.utils.qr_read import read_qr_image return read_qr_image(data) def show_qr(data: str) -> None: """Draw *data* as a QR code on the shared console, unless a prompt handler stands in for the terminal. Text longer than a QR code holds draws nothing: the prompt or notice beside it still shows. """ from segno import DataOverflowError if _prompt_handler is not None: return try: text = qr_text(data) except DataOverflowError: return console.print(text, no_wrap=True, overflow="ignore", crop=False) def prompt_user(prompt: str, qr: str | None = None) -> str: """Ask the user for input on the shared console, themed and indented like the rest of the output. *qr* is drawn above the prompt as a QR code, for a login the user scans on a phone. """ if _prompt_handler is not None: return _prompt_handler(prompt, qr) if qr: show_qr(qr) indent = " " * 5 body = Text(indent + prompt.rstrip("\n ").replace("\n", "\n" + indent), style="text") body.append("\n" + indent + "> ", style="rule.text") return console.input(body) _notice_handler: Callable[[str | None, str | None], None] | None = None def set_notice_handler(handler: Callable[[str | None, str | None], None] | None) -> None: """Route every :func:`show_notice` call to *handler*, for a process whose log output no user reads.""" global _notice_handler _notice_handler = handler def show_notice(message: str | None, qr: str | None = None) -> None: """Give *message* and its QR code text to the notice handler, or None to clear it. Without a handler this does nothing.""" if _notice_handler is not None: _notice_handler(message, qr) __all__ = ( "ComfyLogRenderer", "ComfyRichHandler", "ComfyConsole", "GradientPulseBarColumn", "SyncLive", "console", "listing_panel", "listing_table", "print_listing", "print_wide", "prompt_user", "read_qr_image", "set_notice_handler", "set_prompt_handler", "show_notice", "title_rule", )