Skip to content

ANSI dim text styling #1999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mattmorgis
Copy link

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant