Skip to content

Commit dad8c0b

Browse files
committed
add prompt and config reload
1 parent 17d5bd5 commit dad8c0b

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

ipython/00-init.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import shlex, subprocess
1+
from IPython.core.magic import register_line_magic
2+
import os, shlex, subprocess
23

3-
# Make system commands run in interactive shell and allow executables in PATH to be ran without !
44
ip = get_ipython()
5-
ip.run_line_magic('rehashx', '') # makes it so you don't need ! for executables in your PATH
6-
ip.system = lambda cmd: ip.system_raw(f'bash -c {shlex.quote("shopt -s expand_aliases; source ~/.bashrc; " + cmd)}') # overwrites ! to run in an interactive shell
7-
ip.getoutput = lambda cmd: subprocess.run(['bash', '-c', f'shopt -s expand_aliases; source ~/.bashrc; {cmd}'], # overwrites !! to run an interactive shell
5+
ip.run_line_magic('rehashx', '')
6+
ip.system = lambda cmd: ip.system_raw(f'bash -c {shlex.quote("shopt -s expand_aliases; source ~/.bashrc; " + cmd)}')
7+
ip.getoutput = lambda cmd: subprocess.run(['bash', '-c', f'shopt -s expand_aliases; source ~/.bashrc; {cmd}'],
88
capture_output=True, text=True).stdout.strip().split('\n')
99

10-
# --- Export builtin support ---
11-
from IPython.core.magic import register_line_magic
12-
1310
@register_line_magic
1411
def export(line): ip.run_line_magic('env', line)
1512

@@ -23,10 +20,14 @@ def _builtin_transformer(lines):
2320

2421
ip.input_transformers_cleanup.append(_builtin_transformer)
2522

26-
# Import bash aliases into IPython
23+
@register_line_magic
24+
def reload_config(line):
25+
import glob
26+
for f in sorted(glob.glob(os.path.expanduser('~/.ipython/profile_default/startup/*.py'))): ip.run_line_magic('run', f)
27+
print('Reloaded startup files')
28+
2729
aliases = ip.getoutput('alias')
2830
parsed = [[a.split('=', 1)[0].replace('alias ', ''), a.split('=', 1)[1].strip("'")] for a in aliases if '=' in a]
2931
for name, cmd in parsed:
3032
try: ip.run_line_magic('alias', f'{name} {cmd}')
31-
except: pass # Skip builtins like 'cd'
32-
33+
except: pass

ipython/01-prompt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from IPython.terminal.prompts import Prompts, Token
2+
import os, subprocess
3+
4+
def _git_branch():
5+
r = subprocess.run(['git', 'branch', '--show-current'], capture_output=True, text=True)
6+
return r.stdout.strip()
7+
8+
def _venv_name():
9+
v = os.environ.get('VIRTUAL_ENV', '')
10+
return os.path.basename(v) if v else ''
11+
12+
class PathPrompt(Prompts):
13+
def in_prompt_tokens(self):
14+
path = os.getcwd().replace(os.path.expanduser('~'), '~')
15+
toks = []
16+
venv = _venv_name()
17+
if venv: toks.append((Token.Generic, f'({venv}) '))
18+
toks.append((Token.Prompt, f'{path}'))
19+
branch = _git_branch()
20+
if branch: toks.append((Token.PromptNum, f' ({branch})'))
21+
toks += [(Token.Prompt, ' ['), (Token.PromptNum, str(self.shell.execution_count)), (Token.Prompt, ']: ')]
22+
return toks
23+
24+
get_ipython().prompts = PathPrompt(get_ipython())

0 commit comments

Comments
 (0)