Skip to content

TextualOutputWriter

Base classes

Name Children Inherits
OutputWriter
slashed.base
Interface for command output.

â‹” Inheritance diagram

graph TD
  236713483["app.TextualOutputWriter"]
  1594020653["base.OutputWriter"]
  1073422405["typing.Protocol"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  1594020653 --> 236713483
  1073422405 --> 1594020653
  3481597255 --> 1073422405
  2751680898 --> 3481597255

🛈 DocStrings

Bases: OutputWriter

Output writer that routes messages to bound widgets.

Supports different widget types: - Labels: Direct update of content - VerticalScroll/Container: Mount new Label with content - Other widgets: Try update() method

Source code in src/slashed/textual_adapter/app.py
class TextualOutputWriter(OutputWriter):
    """Output writer that routes messages to bound widgets.

    Supports different widget types:
    - Labels: Direct update of content
    - VerticalScroll/Container: Mount new Label with content
    - Other widgets: Try update() method
    """

    def __init__(self, app: App[Any]) -> None:
        self.app = app
        self._bindings: dict[str, str] = {}
        self._default_binding: str | None = None

    def bind(self, output_id: str, widget_query: str, default: bool = False) -> None:
        """Bind an output to a widget.

        Args:
            output_id: ID for this output stream
            widget_query: CSS query to find the target widget
            default: Whether this is the default output for unspecified streams
        """
        self._bindings[output_id] = widget_query
        if default:
            self._default_binding = output_id

    async def print(self, message: str, output_id: str | None = None) -> None:
        """Print message to bound widget.

        Args:
            message: Message to display
            output_id: Optional output stream ID. Uses default if not specified.

        The message is handled differently based on widget type:
        - For containers (VerticalScroll/Container): Mount new Label
        - For Label widgets: Update content
        - For other widgets: Try update() method

        Raises:
            ValueError: If no default binding is configured or binding not found
            TypeError: If widget type is not supported
        """
        if output_id is None and self._default_binding is None:
            msg = "No default output binding configured"
            raise ValueError(msg)

        binding = self._bindings.get(
            output_id or self._default_binding,
        )
        if not binding:
            msg = f"No binding found for output: {output_id}"
            raise ValueError(msg)

        widget = self.app.query_one(binding)
        if isinstance(widget, VerticalScroll | Container):
            widget.mount(Label(message))
        elif isinstance(widget, Label):
            widget.update(message)
        else:
            try:
                widget.update(message)  # type: ignore
            except AttributeError as e:
                msg = f"Widget {type(widget).__name__} does not support update()"
                raise TypeError(msg) from e

bind

bind(output_id: str, widget_query: str, default: bool = False) -> None

Bind an output to a widget.

Parameters:

Name Type Description Default
output_id str

ID for this output stream

required
widget_query str

CSS query to find the target widget

required
default bool

Whether this is the default output for unspecified streams

False
Source code in src/slashed/textual_adapter/app.py
def bind(self, output_id: str, widget_query: str, default: bool = False) -> None:
    """Bind an output to a widget.

    Args:
        output_id: ID for this output stream
        widget_query: CSS query to find the target widget
        default: Whether this is the default output for unspecified streams
    """
    self._bindings[output_id] = widget_query
    if default:
        self._default_binding = output_id

print async

print(message: str, output_id: str | None = None) -> None

Print message to bound widget.

Parameters:

Name Type Description Default
message str

Message to display

required
output_id str | None

Optional output stream ID. Uses default if not specified.

None

The message is handled differently based on widget type: - For containers (VerticalScroll/Container): Mount new Label - For Label widgets: Update content - For other widgets: Try update() method

Raises:

Type Description
ValueError

If no default binding is configured or binding not found

TypeError

If widget type is not supported

Source code in src/slashed/textual_adapter/app.py
async def print(self, message: str, output_id: str | None = None) -> None:
    """Print message to bound widget.

    Args:
        message: Message to display
        output_id: Optional output stream ID. Uses default if not specified.

    The message is handled differently based on widget type:
    - For containers (VerticalScroll/Container): Mount new Label
    - For Label widgets: Update content
    - For other widgets: Try update() method

    Raises:
        ValueError: If no default binding is configured or binding not found
        TypeError: If widget type is not supported
    """
    if output_id is None and self._default_binding is None:
        msg = "No default output binding configured"
        raise ValueError(msg)

    binding = self._bindings.get(
        output_id or self._default_binding,
    )
    if not binding:
        msg = f"No binding found for output: {output_id}"
        raise ValueError(msg)

    widget = self.app.query_one(binding)
    if isinstance(widget, VerticalScroll | Container):
        widget.mount(Label(message))
    elif isinstance(widget, Label):
        widget.update(message)
    else:
        try:
            widget.update(message)  # type: ignore
        except AttributeError as e:
            msg = f"Widget {type(widget).__name__} does not support update()"
            raise TypeError(msg) from e

:fa-brands-github: Show source on GitHub