[IPython 5] [Docs] Custom Terminal Prompts

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
Just been trying to set up a custom prompt with the new prompt_toolkit features, and couldn't figure it out from the IPython docs. Perhaps adding an example class would make things clear.

Thanks,
-- Carl Smith

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Thomas Kluyver-2
On 9 July 2016 at 15:59, Carl Smith <[hidden email]> wrote:
Just been trying to set up a custom prompt with the new prompt_toolkit features, and couldn't figure it out from the IPython docs. Perhaps adding an example class would make things clear.

In the absence of examples in the docs, the prompt classes in the code might be worth a look:

https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith

Thanks Thomas. Appreciated.


On Sat, 9 Jul 2016 17:07 Thomas Kluyver, <[hidden email]> wrote:
On 9 July 2016 at 15:59, Carl Smith <[hidden email]> wrote:
Just been trying to set up a custom prompt with the new prompt_toolkit features, and couldn't figure it out from the IPython docs. Perhaps adding an example class would make things clear.

In the absence of examples in the docs, the prompt classes in the code might be worth a look:

https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py
_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Fernando Perez
BTW, Carl, if you come up with a nice example that works for you, a small PR contribution to the docs would be greatly appreciated, so it helps others in the future!

Cheers

f

On Sat, Jul 9, 2016 at 12:30 PM, Carl Smith <[hidden email]> wrote:

Thanks Thomas. Appreciated.


On Sat, 9 Jul 2016 17:07 Thomas Kluyver, <[hidden email]> wrote:
On 9 July 2016 at 15:59, Carl Smith <[hidden email]> wrote:
Just been trying to set up a custom prompt with the new prompt_toolkit features, and couldn't figure it out from the IPython docs. Perhaps adding an example class would make things clear.

In the absence of examples in the docs, the prompt classes in the code might be worth a look:

https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py
_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev




--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
No problem at all, Fernando.

I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

I'll have another go at it soon, and am happy to add something to the docs once I figure it out, but just switched to using tmux and the ne editor this week too, so have a ton of little things to figure out and configure at the moment.

Best,

-- Carl Smith

On 11 July 2016 at 00:01, Fernando Perez <[hidden email]> wrote:
BTW, Carl, if you come up with a nice example that works for you, a small PR contribution to the docs would be greatly appreciated, so it helps others in the future!

Cheers

f

On Sat, Jul 9, 2016 at 12:30 PM, Carl Smith <[hidden email]> wrote:

Thanks Thomas. Appreciated.


On Sat, 9 Jul 2016 17:07 Thomas Kluyver, <[hidden email]> wrote:
On 9 July 2016 at 15:59, Carl Smith <[hidden email]> wrote:
Just been trying to set up a custom prompt with the new prompt_toolkit features, and couldn't figure it out from the IPython docs. Perhaps adding an example class would make things clear.

In the absence of examples in the docs, the prompt classes in the code might be worth a look:

https://github.com/ipython/ipython/blob/master/IPython/terminal/prompts.py
_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev




--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Fernando Perez
On Sun, Jul 10, 2016 at 4:11 PM, Carl Smith <[hidden email]> wrote:
No problem at all, Fernando.

Great, thanks!
 
I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

Mmh, I'm afraid I haven't really done any significant prompt customizations in years, way before we made the system traitlets-based... I don't have a quick solution handy.  But let's see if someone else can pitch in and we get a solution, otherwise I'll try to dig in later...

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
Thanks Fernando, but please don't put yourself out on my account. Obviously, it's something that needs figuring out, but there's no urgency here.

Best,

-- Carl Smith

On 11 July 2016 at 00:15, Fernando Perez <[hidden email]> wrote:
On Sun, Jul 10, 2016 at 4:11 PM, Carl Smith <[hidden email]> wrote:
No problem at all, Fernando.

Great, thanks!
 
I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

Mmh, I'm afraid I haven't really done any significant prompt customizations in years, way before we made the system traitlets-based... I don't have a quick solution handy.  But let's see if someone else can pitch in and we get a solution, otherwise I'll try to dig in later...

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
Just to help move things along, this is what I have. It works, except for the input number always being `1`. I don't know much about some of the parts, so have no idea why the count doesn't increment.

    import IPython
    from pygments.token import Token

    Prompts = IPython.terminal.prompts.Prompts
    Shell = IPython.terminal.interactiveshell.TerminalInteractiveShell

    class CustomPrompts(Prompts):

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

    get_ipython().prompts = CustomPrompts(Shell())

This code creates an input prompt like `CustomIn[1]: `, but the number is always `1`. Interestingly, the output prompt, which is inherited unmodified, is also stuck at `1` now. Everything works correctly; you can input code and so on, but the execution count never updates.

If anyone has any ideas...

Best,

-- Carl Smith

On 11 July 2016 at 00:20, Carl Smith <[hidden email]> wrote:
Thanks Fernando, but please don't put yourself out on my account. Obviously, it's something that needs figuring out, but there's no urgency here.

Best,

-- Carl Smith

On 11 July 2016 at 00:15, Fernando Perez <[hidden email]> wrote:
On Sun, Jul 10, 2016 at 4:11 PM, Carl Smith <[hidden email]> wrote:
No problem at all, Fernando.

Great, thanks!
 
I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

Mmh, I'm afraid I haven't really done any significant prompt customizations in years, way before we made the system traitlets-based... I don't have a quick solution handy.  But let's see if someone else can pitch in and we get a solution, otherwise I'll try to dig in later...

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev




_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
It seems to be unrelated to the prompt customisation stuff. I removed the code from my last email, so now have the standard prompt again, but the execution count still fails to update, even though the prompt is working properly:

​    ​
In [1]: import IPython

​    ​
In [2]: Shell = IPython.terminal.interactiveshell.TerminalInteractiveShell

​    ​
In [3]: shell = Shell()

​    ​
In [4]: shell.execution_count

​    ​
Out[4]: 1

​    ​
In [5]: shell.execution_count

    Out[5]: 1

Maybe the prompt class is supposed to increment the count whenever the `in_prompt_tokens` method is called, but I need to get to bed. It's 3am here.

G'night,

-- Carl Smith

On 11 July 2016 at 02:27, Carl Smith <[hidden email]> wrote:
Just to help move things along, this is what I have. It works, except for the input number always being `1`. I don't know much about some of the parts, so have no idea why the count doesn't increment.

    import IPython
    from pygments.token import Token

    Prompts = IPython.terminal.prompts.Prompts
    Shell = IPython.terminal.interactiveshell.TerminalInteractiveShell

    class CustomPrompts(Prompts):

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

    get_ipython().prompts = CustomPrompts(Shell())

This code creates an input prompt like `CustomIn[1]: `, but the number is always `1`. Interestingly, the output prompt, which is inherited unmodified, is also stuck at `1` now. Everything works correctly; you can input code and so on, but the execution count never updates.

If anyone has any ideas...

Best,

-- Carl Smith

On 11 July 2016 at 00:20, Carl Smith <[hidden email]> wrote:
Thanks Fernando, but please don't put yourself out on my account. Obviously, it's something that needs figuring out, but there's no urgency here.

Best,

-- Carl Smith

On 11 July 2016 at 00:15, Fernando Perez <[hidden email]> wrote:
On Sun, Jul 10, 2016 at 4:11 PM, Carl Smith <[hidden email]> wrote:
No problem at all, Fernando.

Great, thanks!
 
I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

Mmh, I'm afraid I haven't really done any significant prompt customizations in years, way before we made the system traitlets-based... I don't have a quick solution handy.  But let's see if someone else can pitch in and we get a solution, otherwise I'll try to dig in later...

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev





_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Matthias Bussonnier

https://github.com/asmeurer/iterm2-tools. Might be a good resource if you looks at (unfinished) PR.

This week is SciPy, so I'll be low bandwidth, but if you issue a draft example PR, I can an try to review.
--
M

On Jul 10, 2016 19:04, "Carl Smith" <[hidden email]> wrote:
It seems to be unrelated to the prompt customisation stuff. I removed the code from my last email, so now have the standard prompt again, but the execution count still fails to update, even though the prompt is working properly:

​    ​
In [1]: import IPython

​    ​
In [2]: Shell = IPython.terminal.interactiveshell.TerminalInteractiveShell

​    ​
In [3]: shell = Shell()

​    ​
In [4]: shell.execution_count

​    ​
Out[4]: 1

​    ​
In [5]: shell.execution_count

    Out[5]: 1

Maybe the prompt class is supposed to increment the count whenever the `in_prompt_tokens` method is called, but I need to get to bed. It's 3am here.

G'night,

-- Carl Smith

On 11 July 2016 at 02:27, Carl Smith <[hidden email]> wrote:
Just to help move things along, this is what I have. It works, except for the input number always being `1`. I don't know much about some of the parts, so have no idea why the count doesn't increment.

    import IPython
    from pygments.token import Token

    Prompts = IPython.terminal.prompts.Prompts
    Shell = IPython.terminal.interactiveshell.TerminalInteractiveShell

    class CustomPrompts(Prompts):

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

    get_ipython().prompts = CustomPrompts(Shell())

This code creates an input prompt like `CustomIn[1]: `, but the number is always `1`. Interestingly, the output prompt, which is inherited unmodified, is also stuck at `1` now. Everything works correctly; you can input code and so on, but the execution count never updates.

If anyone has any ideas...

Best,

-- Carl Smith

On 11 July 2016 at 00:20, Carl Smith <[hidden email]> wrote:
Thanks Fernando, but please don't put yourself out on my account. Obviously, it's something that needs figuring out, but there's no urgency here.

Best,

-- Carl Smith

On 11 July 2016 at 00:15, Fernando Perez <[hidden email]> wrote:
On Sun, Jul 10, 2016 at 4:11 PM, Carl Smith <[hidden email]> wrote:
No problem at all, Fernando.

Great, thanks!
 
I did have a go at it based on the code Thomas pointed to, but couldn't figure out how to use the reference to `self.shell.execution_count`. When it's used directly, as it is in the IPython source (passed through `str`), you end up with something like this:

    In [<traitlets.traitlets.Int at 0x104800b70>]:

Using the `default_value` and `default_value_repr` methods, gives you the number, but it was always `1`, no matter what the actual input number was.

Mmh, I'm afraid I haven't really done any significant prompt customizations in years, way before we made the system traitlets-based... I don't have a quick solution handy.  But let's see if someone else can pitch in and we get a solution, otherwise I'll try to dig in later...

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev





_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Thomas Kluyver-2
In reply to this post by Carl Smith
On 11 July 2016 at 02:27, Carl Smith <[hidden email]> wrote:
get_ipython().prompts = CustomPrompts(Shell())

Don't instantiate a new shell - the value you get from get_ipython() is the active shell. So do this:

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

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith

I think the problem stems from what Thomas said: There's one shell executing code, and another just sat around doing nothing. It's the later one that the CustomPrompt class has a reference to.

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.

Cheers,


On Mon, 11 Jul 2016 13:01 Thomas Kluyver, <[hidden email]> wrote:
On 11 July 2016 at 02:27, Carl Smith <[hidden email]> wrote:
get_ipython().prompts = CustomPrompts(Shell())

Don't instantiate a new shell - the value you get from get_ipython() is the active shell. So do this:

ip = get_ipython()
ip.prompts = CustomPrompts(ip)
_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Fernando Perez

On Mon, Jul 11, 2016 at 1:12 PM, Carl Smith <[hidden email]> 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.


Best to put it in the docs themselves... Wikis tend to go stale more easily without active curation, and ours is no exception :)


--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Thomas Kluyver-2
In reply to this post by Carl Smith
On 11 July 2016 at 21:12, Carl Smith <[hidden email]> 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
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
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.

Best,


-- Carl Smith

On 11 July 2016 at 21:20, Thomas Kluyver <[hidden email]> wrote:
On 11 July 2016 at 21:12, Carl Smith <[hidden email]> 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
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Yuri Numerov
Thanks a ton for this Carl, I spent some time trying to figure this out myself when ipython 5 came out without luck :)

On 13/07/2016 at 2:34 AM, "Carl Smith" <[hidden email]> wrote:

>
>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:
>https://github.com/ipython/ipython/commit/95ed0855ae23e55c10b46903d
>b911265aa1cdd58#diff-c5d5abe11d1c298906cb121c45eb12e2
>
>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.
>
>Best,
>
>
>-- Carl Smith
>[hidden email]
>
>On 11 July 2016 at 21:20, Thomas Kluyver <[hidden email]> wrote:
>
>> On 11 July 2016 at 21:12, Carl Smith <[hidden email]>
>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
>> [hidden email]
>> https://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Thomas Kluyver-2
In reply to this post by Carl Smith
Thanks Carl!

On 13 July 2016 at 01:34, Carl Smith <[hidden email]> wrote:
The commit that adds the examples:

Sigh, of course we have docs on customising prompts in two places. I'll try to unify them...
 
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.

Yes, I think the prompt is conceptually a single line. Does the information in the lines above where you type need to change while you're typing? If not, I'd recommend printing it before the prompt is displayed with the post_run_cell event (http://ipython.readthedocs.io/en/stable/config/callbacks.html).

Thomas


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
No problem, Yuri - mostly just followed Thomas' pointers.

Thanks again, Thomas. Yep - printing the first line of the prompt from a callback should work fine. I didn't think about the event API.

The current API for defining custom prompts is powerful, but pretty low level, and it becomes more complicated if you need custom token classes to use different colours. It'd be nice if there was also a wrapper API that would take strings, like bash prompts, and parse them into prompt_toolkit token arrays and plumb them in automatically.

The actual format used in bash prompts makes it awkward to define styles, so I was thinking about reusing Python's string formatting mini-language, and just defining a standard signature that will pass a bunch of helpful stuff to `format` when the prompt is compiled.

No promises, but I'm going to play around with the new prompt API a bit more and see if there's a light way to wrap the current API.

Cheers,


-- Carl Smith

On 13 July 2016 at 11:38, Thomas Kluyver <[hidden email]> wrote:
Thanks Carl!

On 13 July 2016 at 01:34, Carl Smith <[hidden email]> wrote:
The commit that adds the examples:

Sigh, of course we have docs on customising prompts in two places. I'll try to unify them...
 
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.

Yes, I think the prompt is conceptually a single line. Does the information in the lines above where you type need to change while you're typing? If not, I'd recommend printing it before the prompt is displayed with the post_run_cell event (http://ipython.readthedocs.io/en/stable/config/callbacks.html).

Thomas


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Fernando Perez
In reply to this post by Thomas Kluyver-2

On Wed, Jul 13, 2016 at 3:38 AM, Thomas Kluyver <[hidden email]> wrote:
Yes, I think the prompt is conceptually a single line. Does the information in the lines above where you type need to change while you're typing? If not, I'd recommend printing it before the prompt is displayed with the post_run_cell event (http://ipython.readthedocs.io/en/stable/config/callbacks.html).

Mmh, should we update the rewrite logic to basically work only on the last line of the prompt? It could compute the length to write on prompt_string.split('\n')[-1] rather than the full prompt_string. It seems to me that would give us the correct logic in all cases (including single line prompts), no?

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: [IPython 5] [Docs] Custom Terminal Prompts

Carl Smith
On 13 July 2016 at 19:45, Fernando Perez <[hidden email]> wrote:

On Wed, Jul 13, 2016 at 3:38 AM, Thomas Kluyver <[hidden email]> wrote:
Yes, I think the prompt is conceptually a single line. Does the information in the lines above where you type need to change while you're typing? If not, I'd recommend printing it before the prompt is displayed with the post_run_cell event (http://ipython.readthedocs.io/en/stable/config/callbacks.html).

Mmh, should we update the rewrite logic to basically work only on the last line of the prompt? It could compute the length to write on prompt_string.split('\n')[-1] rather than the full prompt_string. It seems to me that would give us the correct logic in all cases (including single line prompts), no?

​+1 Yep. Basically, it's always going to be last line you want to align to, whether there's one or more.

Obviously, users should be able to just define their prompt using the API, instead of defining half the prompt as a print call that happens once per input on a callback, and the other half as an arrays of tokens that are generated by a method every time you edit the input.

Not many people will need this functionality, but it should work correctly if they ever do.

-- Carl Smith

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
12