#!/usr/bin/env python
# vim set fileencoding=iso-8859-15 :

from os import environ as _
from os import uname
import subprocess

__end = '\033[0m'
__col = '\033[%s%sm'

def col(n, s, blink=False):
    return __col % (n, ';5' if blink else '') + s + __end

cwd = _.get('PWD').replace(_.get('HOME'), '~')
user = _.get('USER')
tmux = (_.get('TMUX')) is not None
host = uname()[1]

git = subprocess.Popen('git_get_branch',
        stdout=subprocess.PIPE).communicate()[0].strip()[1:-1]

gitRepo = len(git) > 1
if gitRepo:
    gitCommitted = subprocess.Popen(['git', 'diff', '--quiet']).wait() == 0
    if git == 'master':
        git = '•'
else:
    gitCommitted = False

def user_at_host():
    return col(34, user) + \
      (( col(37,'@') + col(34, host)) if not tmux else '')

def git_branch():
    return col(37,']') + (col(37,'─[') if gitRepo else '') + \
      col(37 if gitCommitted else 35, git, blink=not gitCommitted) + (col(37,']') if gitRepo else '')

print col(37,'┌─['),
print user_at_host(),
print col(37,']─['),
print col(37, col(37,'/').join([col(94,s) for s in (cwd.split('/')[:-1]+[''])])) + \
      col(96, cwd.split('/')[-1]),

print git_branch()
