prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Can't scroll TextArea using FormattedTextControl for ANSI colors

Open
#1,903 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

# Repro case for prompt_toolkit issue with FormattedTextControl and scrolling in TextArea

from prompt_toolkit import Application
from prompt_toolkit.layout import Layout, HSplit, VSplit
from prompt_toolkit.widgets import TextArea
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit import ANSI
from prompt_toolkit.key_binding import KeyBindings

def create_repro_app():
    # Create a TextArea with regular text
    regular_text_area = TextArea(
        text="Regular TextArea:\n" + "\n".join([f"Line {i}" for i in range(100)]),
        scrollbar=True,
        wrap_lines=True,
    )

    # Create a TextArea with FormattedTextControl for ANSI text
    ansi_text = ANSI("\n".join([
        f"\033[1;3{i%8}mANSI Formatted Line {i}\033[0m" for i in range(100)
    ]))
    formatted_text_area = TextArea(
        scrollbar=True,
        wrap_lines=True,
    )
    formatted_text_area.window.content = FormattedTextControl(ansi_text)

    # Create the layout
    layout = Layout(
        HSplit([
            VSplit([
                regular_text_area,
                formatted_text_area,
            ]),
        ])
    )

    # Create key bindings
    kb = KeyBindings()

    @kb.add('tab')
    def _(event):
        # Toggle focus between text areas
        if event.app.layout.has_focus(regular_text_area):
            event.app.layout.focus(formatted_text_area)
        else:
            event.app.layout.focus(regular_text_area)

    @kb.add('escape', 'q')
    def _(event):
        """Quit the application when 'escape' followed by 'q' is pressed."""
        event.app.exit()


    # Create and return the application
    return Application(layout=layout, key_bindings=kb, full_screen=True)

def run_repro():
    app = create_repro_app()
    app.run()

# Run the repro case
run_repro()

# Note: This repro case demonstrates that while the regular TextArea
# scrolls normally, the TextArea with FormattedTextControl for ANSI
# text does not scroll properly for multiline content.
# Use Tab key to switch focus between the two text areas.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproduction and compare scrolling in the regular TextArea with the TextArea whose window content is a FormattedTextControl containing ANSI text. Trace the TextArea, window, and FormattedTextControl scrolling behavior; done means multiline ANSI content scrolls normally like the regular TextArea.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.