将颜色添加到新样式ipython(v5)提示符

Mic*_*man 10 python ipython python-3.x

立即更新到新发布的ipython5.启动交互式提示并收到:

/usr/local/lib/python3.5/site-packages/IPython/core/interactiveshell.py:440: UserWarning: As of IPython 5.0 `PromptManager` config will have no effect and has been replaced by TerminalInteractiveShell.prompts_class
warn('As of IPython 5.0 `PromptManager` config will have no effect'
Run Code Online (Sandbox Code Playgroud)

拉出我的旧配置设置来自定义和着色提示,然后寻找新的方式来自定义提示并找到它,非常酷.使用示例代码中的新类样式:

class MyPrompt(Prompts):
    def in_prompt_tokens(self, cli=None):
        return [(Token, os.getcwd()),
                (Token.Prompt, ' >>>')]
Run Code Online (Sandbox Code Playgroud)

把它放到一个启动脚本中它运行得很好,除了它默认不着色令牌行,Token.Prompt变成浅绿色.

尝试使用旧的配置方法颜色(r'{color.Green}')但这在这里不起作用.任何正确方向的指针都会很棒.

谢谢!

hpa*_*ulj 6

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

class MyPrompt(Prompts):

    def in_prompt_tokens(self, cli=None):   # default
        return [
            (Token.Prompt, 'In ['),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: '),
        ]

    def in_prompt_tokens(self, cli=None):  # sample
        return [(Token, os.getcwd()),
                 (Token.Prompt, ' >>>')]

    def in_prompt_tokens(self, cli=None):   # custom
        path = os.path.basename(os.getcwd())
        return [
            (Token.Prompt, '<'),
            (Token.PromptNum, '~/'+path),
            (Token.Prompt, '>'),
            (Token.Prompt, '['),
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ']: '),
        ]

    def in_prompt_tokens(self, cli=None):   # custom
        path = os.path.basename(os.getcwd())
        return [
            (Token.PromptNum, str(self.shell.execution_count)),
            (Token.Prompt, ':'),
            (Token.PromptNum, '~/'+path),
            (Token.Prompt, '$ '),
        ]

"""
use:
import myprompt as MP
ip=get_ipython()
ip.prompts=MP.MyPrompt(ip)
"""
Run Code Online (Sandbox Code Playgroud)

我用这个脚本试验了各种提示.它包括默认 in_prompt_tokens方法,示例自定义和一些替代方案.最后一个模仿我的bash提示

73:~/mypy$ 
Run Code Online (Sandbox Code Playgroud)

在看起来像元(Token..., str)组根据的设置字符串的颜色token_type. Token,Token.Prompt,Token.PromptNum可能的类型.寻找Token.<tab>更多(例如OutPrompt(Num)).

IPython/terminal/prompts.py
Run Code Online (Sandbox Code Playgroud)

我可能不会使用任何这些,因为我喜欢默认匹配In /Out对.此外,我可以--term-title用来显示选项卡标题中的目录.