Skip to content

Catching keyboard interrupt for colored prompts using ANSI() causes prompt to turn grey #1521

@not-sekiun

Description

@not-sekiun

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.

Capture

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions