Configurable Pdb prompts


I’d like to float the idea of making the pdb prompts configurable.

Debuggers such as gdb and lldb have a feature to configure the
prompts that are used. When running in Bash, for example, this permits UX improvements
such as using colours in prompts and changing the terminal window title.
The Python debugger doesn’t have an option for this at present.

In the pdb code, the line_prefix variable is defined
outside the Pdb class definition, but other prompts (e.g. (Pdb) and
(com) ) are defined within the class, so can’t be easily changed.

This could be implemented by defining the other prompts outside the class
definition also, so that users could set the prompts in their PYTHONSTARTUP
file. Alternatively, a configuration command similar to the current alias
command could be added, allowing users to define prompts in a .pdbrc file.

If I understand your rationale correctly, what you want is a hook to be called when pdb displays a prompt, instead of customising the (Pdb) string? Your post mixes the two topics and it is difficult to understand what you are really looking for.

I’m sorry if my original post wasn’t clear. There are two alternative ways that I think this could be implemented. I am presenting these as two separate options:

  1. The line_prefix variable is a top-level assignment in the pdb module. The comment in the code recommends overriding it after importing pdb:
# Interaction prompt line will separate file and call info from code
# text using value of line_prefix string.  A newline and arrow may
# be to your liking.  You can set it once pdb is imported using the
# command "pdb.line_prefix = '\n% '".

However, the other prompts are not top-level assignments. If they were made top-level assignments, then they could be changed after importing pdb in the same way (For example, in a user’s PYTHONSTARTUP file):

pdb.main_prompt = "(MyFancyCamelCasePdb) "
pdb.command_prompt = "(YourCommandHere) "
pdb.line_prefix = "\n-> "
  1. Options specified in the pdbrc file are interpreted as if they had been typed at the pdb prompt. Adding a command to pdb to set the prompt would allow setting the value in the pdbrc file. This could be used to update the self.prompt attribute, for example. An entry in the pdbrc file might look like this:
set main_prompt "(MyFancyCamelCasePdb) "

Bash (and, I think, other shells) let you set colours and terminal titles using escape sequences, so it would not be necessary to use a hook to achieve this. This SO answer gives an example of setting a prompt with Bash escape sequences (in that case, for the Python interpreter):

sys.ps1='\x1b[1;49;33m>>>\x1b[0m '  # bright yellow