blob: ac3bb59278eec5d5b613edeeda72bb33b2e6052e [file] [log] [blame]
Ryan0df98ca2011-06-01 11:22:22 -10001#!/bin/bash
Ryanf3787fd2011-06-01 11:33:41 -10002#
Ryan0df98ca2011-06-01 11:22:22 -10003# This theme was obviously inspired a lot by
Ryanf3787fd2011-06-01 11:33:41 -10004#
Ryan0df98ca2011-06-01 11:22:22 -10005# - Demula theme
6#
7# which in itself was inspired by :
8#
9# - Ronacher's dotfiles (mitsuhikos) - http://github.com/mitsuhiko/dotfiles/tree/master/bash/
10# - Glenbot - http://theglenbot.com/custom-bash-shell-for-development/
11# - My extravagant zsh - http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
12# - Monokai colors - http://monokai.nl/blog/2006/07/15/textmate-color-theme/
13# - Bash_it modern theme
14#
Ryanf3787fd2011-06-01 11:33:41 -100015# Hawaii50 theme supports :
16#
17# - configurable directory length
18# - hg, svn, git detection (I work in all of them)
19# - virtualenv, rvm + gemsets
Ryan0df98ca2011-06-01 11:22:22 -100020#
Ryan Kannodcb0dde2011-06-07 08:25:45 -100021# Screenshot: http://i.imgur.com/4IAMJ.png
Ryan0df98ca2011-06-01 11:22:22 -100022#
23# by Ryan Kanno <ryankanno@localkinegrinds.com>
24#
25# And yes, we code out in Hawaii. :D
26#
27# Note: I also am really new to this bash scripting game, so if you see things
28# that are flat out wrong, or if you think of something neat, just send a pull
Ryanf3787fd2011-06-01 11:33:41 -100029# request. This probably only works on a Mac - as some functions are OS
30# specific like getting ip, etc.
31#
Ryan0df98ca2011-06-01 11:22:22 -100032
33# COLORS ======================================================================
Ryan1a83a4d2011-05-31 13:19:20 -100034ORANGE='\e[0;33m'
35GREY='\e[1:37m'
36
37DEFAULT_COLOR='\[${white}\]'
38
39USER_COLOR='\[${purple}\]'
40SUPERUSER_COLOR='\[${red}\]'
41MACHINE_COLOR=$ORANGE
Ryan0df98ca2011-06-01 11:22:22 -100042IP_COLOR=$ORANGE
Ryan Kanno0611f862011-05-31 21:38:41 -100043DIRECTORY_COLOR='\[${green}\]'
Ryan1a83a4d2011-05-31 13:19:20 -100044
Ryan Kanno0611f862011-05-31 21:38:41 -100045VE_COLOR='\[${cyan}\]'
46RVM_COLOR='\[${cyan}\]'
Ryan1a83a4d2011-05-31 13:19:20 -100047
Ryan Kanno9bd5b542011-05-31 21:23:33 -100048REF_COLOR='\[${purple}\]'
49
Ryan1a83a4d2011-05-31 13:19:20 -100050# SCM prompts
51SCM_THEME_PROMPT_DIRTY=' ${bold_red}✗${normal}'
52SCM_THEME_PROMPT_CLEAN=' ${bold_green}✓${normal}'
Ryan0df98ca2011-06-01 11:22:22 -100053SCM_THEME_PROMPT_PREFIX=" "
54SCM_THEME_PROMPT_SUFFIX=""
Ryan1a83a4d2011-05-31 13:19:20 -100055
56# Max length of PWD to display
57MAX_PWD_LENGTH=20
58
59# Max length of Git Hex to display
60MAX_GIT_HEX_LENGTH=5
61
Ryan0df98ca2011-06-01 11:22:22 -100062# FUNCS =======================================================================
Ryan Kannodcb0dde2011-06-07 08:25:45 -100063
64# TODO: Should check with `uname` and use ip addr
Ryan09dfe752011-05-31 19:17:10 -100065function ip {
66 echo $(ifconfig en1 | grep "inet " | awk '{ print $2 }')
67}
68
Ryanaa2f1cb2011-05-31 18:41:07 -100069# Override function scm
70function scm {
71 if [[ -d .git ]]; then SCM=$GIT
72 elif [[ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]]; then SCM=$GIT
73 elif [[ -n "$(hg summary 2> /dev/null)" ]]; then SCM=$HG
74 elif [[ -d .svn ]]; then SCM=$SVN
75 else SCM='NONE'
76 fi
77}
78
Ryan1a83a4d2011-05-31 13:19:20 -100079# Displays the current virtualenv information
80function curr_virtualenv_info() {
81 [ ! -z "$VIRTUAL_ENV" ] && echo "`basename $VIRTUAL_ENV`"
82}
83
84# Displays the current rvm information w/gemset
85function curr_rvm_info() {
86 local ruby_version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
87 local ruby_gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
88
89 if [ "$ruby_version" != "" ]; then
90 [ "$ruby_gemset" != "" ] && ruby_gemset="@$ruby_gemset"
91 echo "$ruby_version$ruby_gemset"
92 fi
93}
94
95# Displays using ...
96function virtual_info() {
97 local virtual_env_info=$(curr_virtualenv_info)
98 local rvm_info=$(curr_rvm_info)
Ryan1a83a4d2011-05-31 13:19:20 -100099
100 # If no virtual info, just return
101 [ "$virtual_env_info" == "" -a "$rvm_info" == "" ] && return
102
Ryan Kanno0611f862011-05-31 21:38:41 -1000103 local prompt=" using"
104
Ryan1a83a4d2011-05-31 13:19:20 -1000105 # If virtual_env info present, append to prompt
106 [ "$virtual_env_info" != "" ] && prompt="$prompt virtualenv: ${VE_COLOR}$virtual_env_info${DEFAULT_COLOR}"
107
108 if [ "$rvm_info" != "" ]
109 then
110 [ "$virtual_env_info" != "" ] && prompt="$prompt,"
111 prompt="$prompt rvm: ${RVM_COLOR}$rvm_info${DEFAULT_COLOR}"
112 fi
Ryan Kanno0611f862011-05-31 21:38:41 -1000113 echo "$prompt"
Ryan1a83a4d2011-05-31 13:19:20 -1000114}
115
Ryan1a83a4d2011-05-31 13:19:20 -1000116# SCM information
117function scm_info() {
118 SCM_CHAR=$(scm_char)
119 [ "$SCM_CHAR" == "$SCM_NONE_CHAR" ] && return
Ryan Kanno0611f862011-05-31 21:38:41 -1000120 local prompt=" on"
Ryan1a83a4d2011-05-31 13:19:20 -1000121 [ "$SCM_CHAR" == "$SCM_GIT_CHAR" ] && echo "$prompt$(parse_git_info)" && return
Ryan06d71ab2011-05-31 16:10:30 -1000122 [ "$SCM_CHAR" == "$SCM_SVN_CHAR" ] && echo "$prompt$(parse_svn_info)" && return
123 [ "$SCM_CHAR" == "$SCM_HG_CHAR" ] && echo "$prompt$(parse_hg_info)" && return
Ryan1a83a4d2011-05-31 13:19:20 -1000124}
125
126# Parse git info
127function parse_git_info() {
128 if [[ -n $(git status -s 2> /dev/null |grep -v ^# |grep -v "working directory clean") ]]; then
129 state=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
130 else
131 state=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
132 fi
133 prefix=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
134 suffix=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
135 ref=$(git symbolic-ref HEAD 2> /dev/null) || return
136 rawhex=$(git rev-parse HEAD 2>/dev/null) || return
137
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000138 echo "$prefix${REF_COLOR}${ref#refs/heads/}${DEFAULT_COLOR}:${rawhex:0:$MAX_GIT_HEX_LENGTH}$state$suffix"
Ryan1a83a4d2011-05-31 13:19:20 -1000139}
140
Ryan06d71ab2011-05-31 16:10:30 -1000141# Parse hg info
142function parse_hg_info() {
Ryanaa2f1cb2011-05-31 18:41:07 -1000143 if [[ -n $(hg status 2> /dev/null) ]]; then
Ryan06d71ab2011-05-31 16:10:30 -1000144 state=${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
145 else
146 state=${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
147 fi
148 prefix=${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
149 suffix=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
Ryanaa2f1cb2011-05-31 18:41:07 -1000150 branch=$(hg summary 2> /dev/null | grep branch | awk '{print $2}')
151 changeset=$(hg summary 2> /dev/null | grep parent | awk '{print $2}')
Ryan06d71ab2011-05-31 16:10:30 -1000152
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000153 echo "$prefix${REF_COLOR}${branch}${DEFAULT_COLOR}:${changeset#*:}$state$suffix"
Ryan06d71ab2011-05-31 16:10:30 -1000154}
155
Ryan2a6d1da2011-05-31 19:03:28 -1000156# Parse svn info
157function parse_svn_info() {
158 if [[ -n $(svn status --ignore-externals -q 2> /dev/null) ]]; then
159 state=${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
160 else
161 state=${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
162 fi
163 prefix=${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
164 suffix=${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
165 ref=$(svn info 2> /dev/null | awk -F/ '/^URL:/ { for (i=0; i<=NF; i++) { if ($i == "branches" || $i == "tags" ) { print $(i+1); break }; if ($i == "trunk") { print $i; break } } }') || return
166 revision=$(svn info 2> /dev/null | sed -ne 's#^Revision: ##p' )
167 [[ -z $ref ]] && return
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000168 echo -e "$prefix${REF_COLOR}$ref${DEFAULT_COLOR}:$revision$state$suffix"
Ryan2a6d1da2011-05-31 19:03:28 -1000169}
Ryan06d71ab2011-05-31 16:10:30 -1000170
Ryan1a83a4d2011-05-31 13:19:20 -1000171# Displays last X characters of pwd
172function limited_pwd() {
173
174 # Replace $HOME with ~ if possible
175 RELATIVE_PWD=${PWD/#$HOME/\~}
176
177 local offset=$((${#RELATIVE_PWD}-$MAX_PWD_LENGTH))
178
179 if [ $offset -gt "0" ]
180 then
181 local truncated_symbol="..."
182 TRUNCATED_PWD=${RELATIVE_PWD:$offset:$MAX_PWD_LENGTH}
183 echo "${truncated_symbol}/${TRUNCATED_PWD#*/}"
184 else
185 echo "${RELATIVE_PWD}"
186 fi
187}
188
189# Displays the current prompt
190function prompt() {
191
192 local UC=$USER_COLOR
193 [ $UID -eq "0" ] && UC=$SUPERUSER_COLOR
194
Ryan Kanno0611f862011-05-31 21:38:41 -1000195 PS1="$(scm_char) ${UC}\u ${DEFAULT_COLOR}at ${MACHINE_COLOR}\h ${DEFAULT_COLOR}(${IP_COLOR}$(ip)${DEFAULT_COLOR})${DEFAULT_COLOR} in ${DIRECTORY_COLOR}$(limited_pwd)${DEFAULT_COLOR}$(virtual_info)$(scm_info) \$ "
Ryan1a83a4d2011-05-31 13:19:20 -1000196 PS2='> '
197 PS4='+ '
198}
199
200PROMPT_COMMAND=prompt