You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the following code in my startup file for IPython to make it show status of the current Git repository. However, it runs slowly. Is there a fast way to show Git repository status in IPython prompt?
class MyPrompt(Prompts):
def _git_status(self) -> List[bytes]:
try:
proc = subprocess.run(['git', 'status'], stdout=subprocess.PIPE)
except (FileNotFoundError, subprocess.CalledProcessError):
return []
lines = [line.strip() for line in proc.stdout.splitlines()]
# get rid of the leading #
for idx, line in enumerate(lines):
if line.startswith(b'#'):
lines[idx] = line[1:].strip()
return [line for line in lines if line != b'']
def _changed(self, line: bytes) -> bool:
"""Check whether the line indicating changes in the Git Repository.
"""
keywords = [
b'new file:',
b'deleted:',
b'modified:',
b'renamed:',
b'untracked files:',
b'your branch is ahead',
]
line = line.lower().strip()
for key in keywords:
if line.startswith(key):
return True
return False
def _git_changed(self) -> bool:
for line in self._git_status():
if self._changed(line):
return True
return False
def _git_branch(self) -> str:
"""Get the current branch the current Git repository.
:return: The name of the current branch.
"""
try:
proc = subprocess.run(['git', 'branch'], stdout=subprocess.PIPE)
except (FileNotFoundError, subprocess.CalledProcessError):
return ''
for line in proc.stdout.splitlines():
line = line.strip()
if line.startswith(b'*'):
return line[1:].strip().decode()
return ''
def _git_remote(self) -> List[str]:
"""Get the remote names of the current Git repository.
:return: A list of remote names.
"""
try:
proc = subprocess.run(['git', 'remote'], stdout=subprocess.PIPE)
except (FileNotFoundError, subprocess.CalledProcessError):
return ''
return [line.strip().decode() for line in proc.stdout.splitlines() if line.strip() != b'']
def in_prompt_tokens(self, cli=None):
user = getpass.getuser()
host = socket.gethostname()
if host.endswith('.local'):
host = host[:-6]
remotes = ''
branch = ''
if Path('.git').is_dir():
remotes = '(' + '|'.join(self._git_remote()) + ')'
branch = '[' + self._git_branch() + f'{"*" if self._git_changed() else ""}]'
cwd = Path.cwd()
return [
(Token, f'{user}@{host} {cwd} {remotes} {branch}'),
(Token.Prompt, '\n >>> ')
]
get_ipython().prompts = MyPrompt(get_ipython())
The text was updated successfully, but these errors were encountered:
I have the following code in my startup file for IPython to make it show status of the current Git repository. However, it runs slowly. Is there a fast way to show Git repository status in IPython prompt?
The text was updated successfully, but these errors were encountered: