-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_ipython_prompts.py
78 lines (61 loc) · 2.1 KB
/
my_ipython_prompts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from IPython.terminal.prompts import Prompts, Token
#from platform import python_version
import os
import sys
FEATURES = [
'version 0.1.0 : for IPython Prompts',
'version 0.2.0 : add parser for ``help``',
'version 0.3.0 : more versatile on ``help``',
'version 0.4.0 : make more robust on ``help``',
]
__version__ = FEATURES[-1].split(':')[0].split()[1]
#VERSION = python_version()
CONDA = os.getenv('CONDA_DEFAULT_ENV','')
#LEVEL = os.getenv('SHLVL','')
#if 'schrodinger' in sys.prefix.lower():
# title = 'level-%s: Python %s: Schrodinger: {cwd}' % (LEVEL, VERSION)
#else:
# title = 'level-%s: Python %s: {cwd}' % (LEVEL, VERSION)
class MyPrompt(Prompts):
def in_prompt_tokens(self):
return [
(Token.Prompt, self.vi_mode() ),
(Token.Prompt, '('+CONDA+') '),
(Token.Literal.String, os.path.basename(os.getcwd())+': '),
(Token.Prompt, 'In ['),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, ']: '),
]
def my_cmd_help(lines):
news = []
for line in lines:
if not (line.startswith('%') or line.startswith('help(')):
tmp = line.rstrip()
if tmp.endswith(' %h'):
tmp = tmp[:-2]
bo = True
elif tmp.endswith(' %help'):
tmp = tmp[:-5]
bo = True
else:
bo = False
if bo:
# cases (simplified):
## os.getenv %h
## os.getenv('PATH') %h
## v = os.getenv('PATH') %h
ndx = tmp.find('(')
if ndx != -1:
tmp = tmp[:ndx]
ndx = tmp.find('=')
if ndx != -1:
tmp = tmp[ndx+1:]
line = 'help('+tmp+')'
news.append(line)
return news
ip = get_ipython()
ip.prompts = MyPrompt(ip)
#ip.prompts.shell.term_title_format = title
if 'schrodinger' in sys.prefix.lower():
ip.prompts.shell.term_title_format = 'Schrodinger: {cwd}'
ip.input_transformers_cleanup.append(my_cmd_help)