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

Possible bug: zero-width-escapes at the end of a line are not rendered

Open
#1,651 0 comments 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

Hi Jonathan,

I think I've come across a potential bug in prompt_toolkit.renderer._output_screen_diff: if a line of output is updated so it ends with a zero-width-escape fragment, the ZWE at the end of the line does not get rendered.

In the example app below, pressing u appends a text fragment and a ZWE fragment (red text) to the end of the output. However, the zwe fragment is not rendered since it is at the end of a line. If another text fragment is append (u), or the app is closed (q), the ZWE at the end of the line is rendered.

I'm using red text to represent a ZWE in this example, but my use case is using ZWEs to display terminal graphics.

from prompt_toolkit.application import Application
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.containers import Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.key_binding import KeyBindings

formatted_text = [
    ("", "start-of-line"),
]

kb = KeyBindings()

@kb.add("u")
def update(event):
    formatted_text.extend(
        [
            ("", " text-fragment"),
            ("[ZeroWidthEscape]", "\x1b[0;31m zwe-fragment\x1b[0m"),
        ]
    )

@kb.add("c-q")
def quit(event):
    event.app.exit()

app = Application(
    full_screen=False,
    layout=Layout(container=Window(FormattedTextControl(lambda: formatted_text))),
    key_bindings=kb,
)

app.run()

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

Run the provided prompt_toolkit example and inspect prompt_toolkit.renderer._output_screen_diff while pressing u to reproduce the missing zero-width escape at the line end. Confirm the fix by checking that the final zero-width-escape fragment is rendered immediately after the update, without needing another text fragment or application exit.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.