Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

python - Adding Color to new style ipython (v5) prompt

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, ' >>>')]

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.

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.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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)
"""

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...