have awesome promptting

This commit is contained in:
Anthony Scopatz 2015-03-01 17:14:28 -06:00
parent cacf3e3a48
commit a9f003b2b5
2 changed files with 111 additions and 2 deletions

View file

@ -1,15 +1,55 @@
"""Environment for the xonsh shell.
"""
import os
import socket
import builtins
import subprocess
default_prompt_template = r'\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[01;31m\]$(get_curr_branch ) \[\033[01;34m\]\$\[\033[00m\]'
from xonsh.tools import TERM_COLORS
def current_branch(cwd=None):
"""Gets the branch for a current working directory. Returns None
if the cwd is not a repository. This currently only works for git,
bust should be extended in the future.
"""
cwd = os.getcwd() if cwd is None else cwd
try:
# note that this is about 10x faster than bash -i "__git_ps1"
d = subprocess.check_output(['which', 'git'], cwd=cwd,
stderr=subprocess.PIPE,
universal_newlines=True)
d = os.path.dirname(os.path.dirname(d))
input = ('source ' + d + '/lib/git-core/git-sh-prompt; '
'__git_ps1 "${1:-%s}"')
s = subprocess.check_output(['bash',], cwd=cwd, input=input,
stderr=subprocess.PIPE,
universal_newlines=True)
except subprocess.CalledProcessError:
s = ''
if len(s) == 0:
s = None
return s
default_prompt_template = ('{GREEN}{user}@{hostname}{BLUE} '
'{cwd}{RED}{curr_branch} {BLUE}${NO_COLOR} ')
def default_prompt():
"""Returns the default xonsh prompt string. This takes no arguments."""
env = builtins.__xonsh_env__
return 'yo '
cwd = env['PWD']
branch = current_branch(cwd=cwd)
branch = '' if branch is None else ' ' + branch
p = default_prompt_template.format(user=env.get('USER', '<user>'),
hostname=socket.gethostname(),
cwd=cwd.replace(env['HOME'], '~'),
curr_branch=branch,
RED=TERM_COLORS['BOLD_RED'],
BLUE=TERM_COLORS['BOLD_BLUE'],
GREEN=TERM_COLORS['BOLD_GREEN'],
NO_COLOR=TERM_COLORS['NO_COLOR'],
)
return p
BASE_ENV = {
'PROMPT': default_prompt,
@ -19,6 +59,7 @@ def bash_env():
"""Attempts to compute the bash envinronment variables."""
try:
s = subprocess.check_output(['bash', '-i'], input='env', env={},
stderr=subprocess.PIPE,
universal_newlines=True)
except subprocess.CalledProcessError:
s = ''

View file

@ -54,3 +54,71 @@ def safe_hasattr(obj, attr):
return True
except:
return False
TERM_COLORS = {
# Reset
'NO_COLOR': '\033[0m', # Text Reset
# Regular Colors
'BLACK': '\033[0;30m', # BLACK
'RED': '\033[0;31m', # RED
'GREEN': '\033[0;32m', # GREEN
'YELLOW': '\033[0;33m', # YELLOW
'BLUE': '\033[0;34m', # BLUE
'PURPLE': '\033[0;35m', # PURPLE
'CYAN': '\033[0;36m', # CYAN
'WHITE': '\033[0;37m', # WHITE
# Bold
'BOLD_BLACK': '\033[1;30m', # BLACK
'BOLD_RED': '\033[1;31m', # RED
'BOLD_GREEN': '\033[1;32m', # GREEN
'BOLD_YELLOW': '\033[1;33m', # YELLOW
'BOLD_BLUE': '\033[1;34m', # BLUE
'BOLD_PURPLE': '\033[1;35m', # PURPLE
'BOLD_CYAN': '\033[1;36m', # CYAN
'BOLD_WHITE': '\033[1;37m', # WHITE
# Underline
'UNDERLINE_BLACK': '\033[4;30m', # BLACK
'UNDERLINE_RED': '\033[4;31m', # RED
'UNDERLINE_GREEN': '\033[4;32m', # GREEN
'UNDERLINE_YELLOW': '\033[4;33m', # YELLOW
'UNDERLINE_BLUE': '\033[4;34m', # BLUE
'UNDERLINE_PURPLE': '\033[4;35m', # PURPLE
'UNDERLINE_CYAN': '\033[4;36m', # CYAN
'UNDERLINE_WHITE': '\033[4;37m', # WHITE
# Background
'BACKGROUND_BLACK': '\033[40m', # BLACK
'BACKGROUND_RED': '\033[41m', # RED
'BACKGROUND_GREEN': '\033[42m', # GREEN
'BACKGROUND_YELLOW': '\033[43m', # YELLOW
'BACKGROUND_BLUE': '\033[44m', # BLUE
'BACKGROUND_PURPLE': '\033[45m', # PURPLE
'BACKGROUND_CYAN': '\033[46m', # CYAN
'BACKGROUND_WHITE': '\033[47m', # WHITE
# High Intensity
'INTENSE_BLACK': '\033[0;90m', # BLACK
'INTENSE_RED': '\033[0;91m', # RED
'INTENSE_GREEN': '\033[0;92m', # GREEN
'INTENSE_YELLOW': '\033[0;93m', # YELLOW
'INTENSE_BLUE': '\033[0;94m', # BLUE
'INTENSE_PURPLE': '\033[0;95m', # PURPLE
'INTENSE_CYAN': '\033[0;96m', # CYAN
'INTENSE_WHITE': '\033[0;97m', # WHITE
# Bold High Intensity
'BOLD_INTENSE_BLACK': '\033[1;90m', # BLACK
'BOLD_INTENSE_RED': '\033[1;91m', # RED
'BOLD_INTENSE_GREEN': '\033[1;92m', # GREEN
'BOLD_INTENSE_YELLOW': '\033[1;93m', # YELLOW
'BOLD_INTENSE_BLUE': '\033[1;94m', # BLUE
'BOLD_INTENSE_PURPLE': '\033[1;95m', # PURPLE
'BOLD_INTENSE_CYAN': '\033[1;96m', # CYAN
'BOLD_INTENSE_WHITE': '\033[1;97m', # WHITE
# High Intensity backgrounds
'BACKGROUND_INTENSE_BLACK': '\033[0;100m', # BLACK
'BACKGROUND_INTENSE_RED': '\033[0;101m', # RED
'BACKGROUND_INTENSE_GREEN': '\033[0;102m', # GREEN
'BACKGROUND_INTENSE_YELLOW': '\033[0;103m', # YELLOW
'BACKGROUND_INTENSE_BLUE': '\033[0;104m', # BLUE
'BACKGROUND_INTENSE_PURPLE': '\033[0;105m', # PURPLE
'BACKGROUND_INTENSE_CYAN': '\033[0;106m', # CYAN
'BACKGROUND_INTENSE_WHITE': '\033[0;107m', # WHITE
}