[IPython-user] Prompt

Fernando.Perez at colorado.edu Fernando.Perez at colorado.edu
Wed Mar 16 01:53:08 CST 2005

Quoting Frédéric Mantegazza <mantegazza at ill.fr>:

> In our application, we would like to modify the prompt to provide some
> status info. We plan to have a system to switch between differents editing
> modes, and it would be great to modify the pompt according to the context.
> But I also know that our application use ipython in a very special way.
> But Ferndando, how do you modify the prompt in the pysh profile ? You do
> dynamically modify it according to the current dir, don't you ?

Well, that's a very valid comment. The pysh profile simply enables a prompt
which uses predefined special marks which get dynamically expanded at runtime. 
The actual list of available valid expansions lives in Prompts.py, it is the
prompt_specials_color dictionary.  It looks like this:

prompt_specials_color = {
    # Prompt/history count
    '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
    '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
    # Prompt/history count, with the actual digits replaced by dots.  Used
    # mainly in continuation prompts (prompt_in2)
    '\\D': '${"."*len(str(self.cache.prompt_count))}',
    # Current working directory
    '\\w': '${os.getcwd()}',
    # Current time
    '\\t' : '${time.strftime("%H:%M:%S")}',
    # Basename of current working directory.
    # (use os.sep to make this portable across OSes)
    '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
    # These X<N> are an extension to the normal bash prompts.  They return
    # N terms of the path, after replacing $HOME with '~'
    '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,


The point is that perhaps the best approach would be to expose this dict to
user-level modifications.  Having access to it, you could put in any control
character that you want in the prompt definition (or even modify the defaults),
and your user prompts would update dynamically via the existing machinery.

Does this sound like a good solution to you?  Would it give you the kind of
functionality you need?  Also Tom Locke, would this allow a cleaner prompt
management than your current hack for logix?  If this approach addresses your
concerns, I'm happy to implement it, as it's a relatively simple solution (in
terms of effort) which also feels 'clean' to me design wise.

Let me know what you all think before I commit to any coding along these lines.



More information about the IPython-user mailing list