or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/textual@6.1.x
tile.json

tessl/pypi-textual

tessl install tessl/pypi-textual@6.1.0

Modern Text User Interface framework for building cross-platform terminal and web applications with Python

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.18x

Baseline

Agent success rate without this tile

79%

task.mdevals/scenario-6/

Text Formatter Widget

Build a custom Textual widget that displays formatted text with automatic wrapping, overflow handling, and alignment options.

Requirements

Create a TextFormatter widget that:

  1. Text Content Management:

    • Accepts plain text or text with markup styling
    • Stores and manages the text content internally
    • Supports updating the text content after widget creation
  2. Text Wrapping:

    • Automatically wraps text to fit the widget's width
    • Handles long words appropriately without breaking the layout
  3. Alignment Options:

    • Supports left, center, and right text alignment
    • Allows alignment to be configured when creating the widget
    • Provides a method to change alignment dynamically
  4. Overflow Handling:

    • Implements ellipsis mode: truncates text with "..." when content exceeds available space
    • Implements fold mode: wraps text at word boundaries when content is too wide
    • Allows overflow mode to be specified when creating the widget

Implementation Details

Your widget should inherit from Widget and override the render() method to return the properly formatted content. The widget should accept these parameters during initialization:

  • text: The text content to display (string)
  • alignment: Text alignment ("left", "center", or "right"), default "left"
  • overflow: Overflow handling mode ("ellipsis" or "fold"), default "fold"
  • markup: Whether the text contains markup (boolean), default True

Provide a set_text(text: str) method to update the text content, and a set_alignment(alignment: str) method to change the alignment.

Dependencies { .dependencies }

textual { .dependency }

Modern Text User Interface framework for building terminal applications.

Test Cases

Create a file named test_text_formatter.py with the following test cases:

Test 1: Basic Text Display { .test-case @test }

from textual.app import App
from text_formatter import TextFormatter

class TestApp(App):
    def compose(self):
        yield TextFormatter(
            "Hello, World!",
            alignment="left",
            markup=False
        )

app = TestApp()
# The widget should display "Hello, World!" aligned to the left

Test 2: Text with Markup { .test-case @test }

from textual.app import App
from text_formatter import TextFormatter

class TestApp(App):
    def compose(self):
        yield TextFormatter(
            "[bold]Bold text[/bold] and [italic]italic text[/italic]",
            alignment="center",
            markup=True
        )

app = TestApp()
# The widget should display styled text centered

Test 3: Ellipsis Overflow { .test-case @test }

from textual.app import App
from text_formatter import TextFormatter

class TestApp(App):
    def compose(self):
        widget = TextFormatter(
            "This is a very long text that should be truncated with ellipsis when it exceeds the available width",
            alignment="left",
            overflow="ellipsis",
            markup=False
        )
        widget.styles.width = 20  # Constrain width
        yield widget

app = TestApp()
# The widget should show truncated text with "..." at the end

Test 4: Dynamic Text Update { .test-case @test }

from textual.app import App
from text_formatter import TextFormatter

class TestApp(App):
    def compose(self):
        self.formatter = TextFormatter("Initial text", markup=False)
        yield self.formatter

    def on_mount(self):
        self.formatter.set_text("Updated text")
        self.formatter.set_alignment("right")

app = TestApp()
# The widget should display "Updated text" aligned to the right

Expected Output

The widget should:

  • Render text correctly with the specified alignment
  • Handle markup when enabled
  • Apply overflow handling as configured
  • Support dynamic updates to text and alignment
  • Integrate seamlessly with Textual's rendering system