[IPython-dev] [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith carl.input at gmail.com
Tue Jul 12 20:34:39 EDT 2016

It's working now. Thanks for your help with that. It was what Thomas said.

Looking through the IPython repo, it turns out there's already a commit
that added a couple of examples of how to do this, so maybe just need to
move some existing stuff around. If I'd found those examples, and the link
Thomas suggested (to the default class definition), it would have been easy
to figure out from there.

The commit that adds the examples:

I did put a simple example together before I saw those, and you're welcome
to use it too. It just creates more minimal versions of the standard
prompts to save some space:

[*1*]* $ def* *inc*(x):
    *$*     *return* x + 1

[*2*] *$* inc 1
*---->* inc(1)
[*2] :* 2

The code is pretty compact too:

*from IPython.terminal.prompts import Prompts, Token*

*class CustomPrompts(Prompts):*

*    def in_prompt_tokens(self, cli=None): return [*
*        (Token.Prompt, "["),*
*        (Token.PromptNum, str(self.shell.execution_count)),*
*        (Token.Prompt, "] $ ")*
*        ]*

*    def out_prompt_tokens(self): return [*
*        (Token.OutPrompt, "["),*
*        (Token.OutPromptNum, str(self.shell.execution_count)),*
*        (Token.OutPrompt, "] : ")*
*        ]*

*    def continuation_prompt_tokens(self, cli=None, width=None):*
*        if width is None: width = self._width()*
*        return [(Token.Prompt, " " * (width - 5) + "   $ ")]*

*ip = get_ipython()*
*ip.prompts = CustomPrompts(ip)*

One thing I found buggy was the rewrite prompt. If you inherit from
`Prompt` and redefine some of the prompt methods in the derived class, as
you're meant to do, any prompts that you inherit from `Prompt` will pad
themselves out to keep everything aligned. This works well except when you
have a multiline prompt.

Prompts can get pretty lengthy, and you don't really want multiline inputs
starting at column 50, so two-line prompts with a really short second line
will be fairly common.

If you create a multiline Input prompt, the rewrite prompt will end up
being too long. The math in the token assumes the Input prompt is all on
one line.

def rewrite_prompt_tokens(self): width = self._width() return [
(Token.Prompt, ('-' * (width - 2)) + '> '), ]

I'm happy to do some work on the docs, but not sure what should be done
exactly. It's getting late here again (1:30am), so it'll have to be
tomorrow now anyway.


-- Carl Smith
carl.input at gmail.com

On 11 July 2016 at 21:20, Thomas Kluyver <takowl at gmail.com> wrote:

> On 11 July 2016 at 21:12, Carl Smith <carl.input at gmail.com> wrote:
>> On the PR, should I put the example code right there in the docs, where
>> the API is explained, or is there a wiki or something? Happy either way.
>> Just unsure what's correct these days.
> In the docs themselves would be great - thanks!
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> https://mail.scipy.org/mailman/listinfo/ipython-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20160713/4c2fa9fd/attachment.html>

More information about the IPython-dev mailing list