Skip to content

Conversation

@mattmorgis
Copy link
Contributor

This PR adds complete support for ANSI dim text styling. Previously, prompt-toolkit would strip or ignore ANSI dim formatting when processing text from Rich or other libraries that use \x1b[2m escape sequences. This meant that dim text would appear normal instead of faded/dimmed as intended.


You can test the dim functionality with this script:

from prompt_toolkit.formatted_text.ansi import ANSI from prompt_toolkit import print_formatted_text # Test basic dim print_formatted_text(ANSI("\x1b[2mThis text should appear dim\x1b[0m")) # Test dim with color print_formatted_text(ANSI("\x1b[2;31mDim red text\x1b[0m")) # Test bold + dim combination print_formatted_text(ANSI("\x1b[1;2;34mBold dim blue text\x1b[0m")) # Visual comparison (side by side) print_formatted_text(ANSI("Normal vs \x1b[2mDim\x1b[0m")) # Test dim reset print_formatted_text(ANSI("\x1b[1;2mBold+dim\x1b[22mnormal\x1b[0m"))

Using Prompt-Toolkit native styles:

from prompt_toolkit import print_formatted_text from prompt_toolkit.formatted_text import FormattedText from prompt_toolkit.styles import Style style = Style.from_dict({ 'dim-text': 'dim', 'dim-red': 'dim ansired', 'bold-dim': 'bold dim', 'dim-blue': 'dim ansiblue', }) print_formatted_text(FormattedText([ ('', 'Normal text '), ('class:dim-text', 'dim text '), ('class:dim-red', 'dim red '), ('class:bold-dim', 'bold dim'), ]), style=style) # Inline styles also work print_formatted_text(FormattedText([ ('dim', 'This is dim '), ('dim ansired', 'this is dim red '), ('bold dim ansiblue', 'this is bold dim blue'), ]))

It also matches Rich's [dim] formatting exactly:

import io from rich.console import Console from rich.text import Text from prompt_toolkit.formatted_text.ansi import ANSI from prompt_toolkit import print_formatted_text console = Console(file=io.StringIO(), force_terminal=True) console.print("This is [dim]dimmed text[/dim]") rich_output = console.file.getvalue() # dim formatting is preserved print_formatted_text(ANSI(rich_output))
@jonathanslenders jonathanslenders merged commit c53e20c into prompt-toolkit:main Jul 3, 2025
@jonathanslenders
Copy link
Member

Thank you! Really appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants