prompt-toolkit / prompt-toolkit/python-prompt-toolkit
Catching keyboard interrupt for colored prompts using ANSI() causes prompt to turn grey
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
I encountered this issue when attempting to creating a ctrl-c resistant CLI that uses colored prompts. Below is some simplified source code which demonstrates the issue. Essentially, when a colored prompt is set using ANSI() the prompt color turns grey when ctrl-c keyboard interrupts are caught in a try except block.
import prompt_toolkit
while True:
try:
text = prompt_toolkit.prompt(prompt_toolkit.formatted_text.ANSI('\x1b[1m\x1b[31mTEST : \x1b[0m')).strip()
print(f'you said : {text}')
except KeyboardInterrupt:
print('Caught keyboard interrupt')
For any user input the code works as expected. However, when keyboard interrupts are caught from ctrl-c the originally colored prompt turns grey. The next prompt, the one waiting for user input takes on the actual color as seen in the below image where the latest prompt at the bottom that is waiting for input is red.
From the image above the greyed out prompts occur whenever ctrl-c is caught. Im running this on windows 10, using python 3.8.10. This is done with prompt-toolkit version 3.0.21. Ive tried running this code on command prompt, powershell and even cmder. All 3 consoles have this exact same behaviour. Is this a bug? If not, how can I prevent this from occurring?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python reproduction with prompt_toolkit.prompt, formatted_text.ANSI, and a caught KeyboardInterrupt on Windows. Trace the prompt rendering and interrupt-handling entry points, then verify that prompts printed after Ctrl-C retain their configured ANSI color while the next prompt remains interactive.
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