将颜色添加到新样式的ipython(v5)提示中 [英] Adding Color to new style ipython (v5) prompt

查看:57
本文介绍了将颜色添加到新样式的ipython(v5)提示中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天更新到新发行的ipython5.启动交互式提示并收到:

Update to the newly release ipython5 today. Started up the interactive prompt and received:

/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'

拨出我的旧配置设置以自定义提示并为其着色,然后寻找自定义提示的新方法,发现它非常酷.使用了示例代码中的新类样式::

Yanked out my old config settings to customize and colorize the prompt and went looking for the new way to customize the prompt and found it, very cool. Used the new class style from the example code:

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

将其放入启动脚本中,它可以很好地工作,但默认情况下它不会使Token行变色,Token.Prompt变为浅绿色.

Put this into a startup script and it works great, except it by default doesn't colorize the Token line, the Token.Prompt is made light green.

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

Attempted to use the old config method colors, (r'{color.Green}') but that doesn't work here. Any pointers in the correct direction would be great.

谢谢!

推荐答案

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)
"""

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

I experimented with various prompts with this script. It includes the default in_prompt_tokens method, the example customization and a couple of alternatives. The last imitates my bash prompt

73:~/mypy$ 

In看起来像元组(Token..., str)根据token_type设置字符串的颜色. TokenToken.PromptToken.PromptNum是可能的类型.查看Token.<tab>了解更多信息(例如OutPrompt(Num)).

In looks like the tuple (Token..., str) sets the color of the string according to the token_type. Token, Token.Prompt, Token.PromptNum are possible types. Look at Token.<tab> for more (such as OutPrompt(Num)).

IPython/terminal/prompts.py

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

I probably won't use any of these because I like the default matching In /Out pairs. Besides I can use --term-title to show the directory in the tab title.

这篇关于将颜色添加到新样式的ipython(v5)提示中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆