Advertisement
goatbar

start to a colored bash prompt

May 4th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. '''
  3. Still not really that functional
  4.  
  5. Dependencies:
  6.  
  7.  - http://pypi.python.org/pypi/GitPython
  8.  - http://pypi.python.org/pypi/termcolor
  9.  
  10. Installing termcolor:
  11.  
  12. sudo apt-get install python-stdeb
  13. wget http://pypi.python.org/packages/source/t/termcolor/termcolor-1.1.0.tar.gz#md5=043e89644f8909d462fbbfa511c768df
  14. tar xf termcolor-1.1.0.tar.gz
  15. cd termcolor-1.1.0
  16. python setup.py --command-packages=stdeb.command bdist_deb
  17. sudo dpkg -i deb_dist/python-termcolor_1.1.0-1_all.deb
  18.  
  19. '''
  20.  
  21. __author__ = 'Kurt Schwehr'
  22.  
  23. import datetime
  24. import os
  25. import re
  26. import subprocess
  27. import sys
  28.  
  29. import git
  30. import termcolor
  31.  
  32. def get_width():
  33.   proc = subprocess.Popen(['tput', 'cols'] , stdout=subprocess.PIPE)
  34.   width = proc.communicate()[0].strip()
  35.   return int(width)
  36.  
  37. try:
  38.   repo = git.Repo('.')
  39.   branch = '[%s]' % repo.active_branch
  40. except InvalidGitRepositoryError:
  41.   branch = '' #None
  42.  
  43.  
  44. homedir = os.path.expanduser('~')
  45. path = re.sub('^'+homedir, '~', os.getcwd())
  46.  
  47. # FIX: trim long path from left
  48.  
  49. now = datetime.datetime.now()
  50. when = now.strftime('%a %H:%M:%S')
  51.  
  52. who = '(%s@%s)' % (os.getlogin(), os.uname()[1].split('.')[0])
  53. #who = '(%s@%s)' % (os.getlogin(), socket.gethostname()[1].split('.')[0])
  54.  
  55.  
  56. width = get_width()
  57.  
  58. print width
  59.  
  60. # FIX: Nicely balance where things go and use the width
  61. prompt = '  ({path})   {when} {branch}    {who} '.format(**locals())
  62.  
  63. prompt += ' ' * (width - len(prompt))
  64.  
  65. prompt_clr = termcolor.colored(prompt, 'white', 'on_green', attrs=['bold'])
  66. print prompt_clr
  67. print '# ',
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement