Skip to content

Allow styling prompts with HTML-like tags#1866

Open
rolandwalker wants to merge 1 commit intomainfrom
RW/html-and-more-robust-prompt-strings
Open

Allow styling prompts with HTML-like tags#1866
rolandwalker wants to merge 1 commit intomainfrom
RW/html-and-more-robust-prompt-strings

Conversation

@rolandwalker
Copy link
Copy Markdown
Contributor

@rolandwalker rolandwalker commented Apr 25, 2026

Description

  • use FormattedText for prompt strings rather than ANSI
  • do all interpretation and formatting within render_prompt_string() ( renamed from get_prompt() )
  • make formatting more robust: don't depend on a special string to be substituted for backslashes
  • inline an error message if the user gives an unrecognized backslash prompt format string
  • replace backslashes with forward slashes in socket names, in case they would be further substituted as format strings

Motivation:

It is much easier to figure out how to apply colors and styles using this method.

Bugs and limitations:

Since the substitutions are still done serially on a string (or list of strings), in the rare case that say a DSN-name substitution value contained a backslash, it might again be substituted, or fail. A better way could be to use re.split() with a capture group, replace known prompt strings with callables, then have a substitution pass, then join at the end. But the edge case of a double-substitution is very unlikely, and the edge case of HTML characters is handled.

Documentation: https://python-prompt-toolkit.readthedocs.io/en/stable/pages/printing_text.html#html

Example:

prompt = '\<html><red><u>root</u></red>@localhost:\d&gt; '
last image

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@rolandwalker rolandwalker self-assigned this Apr 25, 2026
 * use FormattedText for prompt strings rather than ANSI
 * do all interpretation and formatting within render_prompt_string()
   ( renamed from get_prompt() )
 * make formatting more robust: don't depend on a special string to be
   substituted for backslashes
 * inline an error message if the user gives an unrecognized backslash
   prompt format string
 * replace backslashes with forward slashes in socket names, in case
   they would be further substituted as format strings

Motivation:

It is much easier to figure out how to apply colors and styles using
this method.

Bugs and limitations:

Since the substitutions are still done serially on a string (or list of
strings), in the rare case that say a DSN-name substitution value
contained a backslash, it might again be substituted, or fail.  A better
way could be to use "re.split()" with a capture group, replace known
prompt strings with callables, then have a substitution pass, then
join at the end.  But the edge case of a double-substitution is very
unlikely, and the edge case of HTML characters is handled.
@rolandwalker rolandwalker force-pushed the RW/html-and-more-robust-prompt-strings branch from ff2b14e to a3aae2e Compare April 25, 2026 21:57
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