Skip to content

Instantly share code, notes, and snippets.

@SerpentChris
Last active January 1, 2017 02:31
Show Gist options
  • Save SerpentChris/18593ccb6298c6a6bcb39217dc8e4234 to your computer and use it in GitHub Desktop.
Save SerpentChris/18593ccb6298c6a6bcb39217dc8e4234 to your computer and use it in GitHub Desktop.
My .bashrc: complete with colored prompt, virtualenv support, and github support.
set_prompt () {
if venv_file=$(find-venv); then
source $venv_file
elif [ "$(type -t deactivate)" == "function" ]; then
deactivate
fi
PS1=$(append-git-branch)
}
GREY="\[\e[1;30m\]"
BLUE="\[\e[1;34m\]"
GREEN="\[\e[1;32m\]"
DEFAULT="\[\e[0m\]"
export CLICOLOR=1
export PS1="${GREY}\h${DEFAULT}:${GREEN}\w ${BLUE}\u${DEFAULT}$ "
export PATH=$PATH:$HOME/bin
# export PATH=$PATH:$HOME/bin:$HOME/go-ethereum/build/bin
export PROMPT_COMMAND=set_prompt
alias ls="ls -Fa"
alias ll="ls -Fal"
# alias geth="geth --fast --cache 4096"
# alias geth-console="geth attach"
#!/usr/bin/python
# make sure to `chmod +x` this and put it somewhere in your path!
import subprocess
from subprocess import CalledProcessError
import os
import re
DEVNULL = open(os.devnull, 'w')
BRANCH = re.compile('^.+? \((.+)\) .+\$ $')
def get_current_branch():
try:
result = subprocess.check_output(['git', 'branch'], universal_newlines=True, stderr=DEVNULL)
except CalledProcessError:
return None
for line in result.strip().split('\n'):
if line.startswith('* '):
return line.lstrip('* ')
return None
def main():
PS1 = os.environ['PS1']
branch = get_current_branch()
match = BRANCH.match(PS1)
if branch and match:
print PS1[:match.start(1)] + branch + PS1[match.end(1):]
elif branch and (match is None):
parts = PS1.rsplit(' ', 2)
parts[0] += ' ({})'.format(branch)
print ' '.join(parts)
elif (branch is None) and match:
print PS1[:match.start(1)-2] + PS1[match.end(1)+1:]
else:
print PS1
if __name__ == '__main__':
main()
#!/usr/bin/python
# make sure you `chmod +x` this and put it somewhere in your path!
import os
def check_path(path):
"""Checks if a path is a virtualenv."""
return os.path.isfile(os.path.join(path, 'bin', 'activate'))
def find_venv():
"""Searches for a virtualenv in the current path or it's parents."""
search_path = os.getcwd()
while not check_path(search_path):
new_search_path = os.path.dirname(search_path)
if new_search_path == search_path:
return None
search_path = new_search_path
return search_path
def main():
result = find_venv()
if result is None:
return 1
else:
print(os.path.join(result, 'bin', 'activate'))
return 0
if __name__ == '__main__':
import sys
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment