#/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()