Skip to content

Instantly share code, notes, and snippets.

@pyatil
Last active January 1, 2016 16:19
Show Gist options
  • Save pyatil/8170016 to your computer and use it in GitHub Desktop.
Save pyatil/8170016 to your computer and use it in GitHub Desktop.
prompt for status git
#/usr/bin/python
# coding: utf-8
"""
prompt for status git
"""
import re
import sh
class my_err_out():
def __init__(self):
self.merr = ''
def write(self, line):
self.merr += line
def flush(self):
pass
cur_err = my_err_out()
try:
git_status = sh.git("status", _err=cur_err)
except:
git_status = cur_err.merr
def color():
if 'working directory clean' in git_status:
return '${grey}[%s]' % branch() # green
elif 'Not a git repository' in git_status:
return ''
else:
return '${red}[%s]' % branch() # red
def branch():
try:
cur_brunch = re.findall("# On branch (.*)", str(git_status))[0]
except:
cur_brunch = ''
return cur_brunch
if __name__ == '__main__':
import sys
if sys.argv[1] == 'color':
sys.stdout.write(color())
else:
print branch()
autoload -U colors && colors
for color in red green yellow blue magenta cyan black white; do
eval $color='%{$fg_no_bold[${color}]%}'
#eval ${color}_bold='%{$fg_bold[${color}]%}'
done
reset="%{$reset_color%}"
if [[ -n "$SSH_CLIENT" || -n "$SSH2_CLIENT" ]]; then
host="${black}[${blue}%m${black}] " #SSH
else
unset host # no SSH
fi
my_git() {
python /home/snu/Projects/git/git_prompt.py color
}
setopt prompt_subst
# git
precmd() {
GIT=$(my_git)
if [ "$EUID" -eq 0 ]; then
PROMPT="${host}${red}[${magenta}%2c${red}]${reset} " # root
else
PROMPT="${green}[%T]${black} ${host}${black}[${magenta}%2c${black}] $GIT${reset} " # user
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment