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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…