blob: 4933123cdb71a28e924928c6fad694d46b22fcf3 [file] [log] [blame]
Ryan0df98ca2011-06-01 11:22:22 -10001#!/bin/bash
Ryan1a83a4d2011-05-31 13:19:20 -10002
Ryan0df98ca2011-06-01 11:22:22 -10003# This theme was obviously inspired a lot by
4# - Demula theme
5#
6# which in itself was inspired by :
7#
8# - Ronacher's dotfiles (mitsuhikos) - http://github.com/mitsuhiko/dotfiles/tree/master/bash/
9# - Glenbot - http://theglenbot.com/custom-bash-shell-for-development/
10# - My extravagant zsh - http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
11# - Monokai colors - http://monokai.nl/blog/2006/07/15/textmate-color-theme/
12# - Bash_it modern theme
13#
14# Things theme supports:
15# - shortended directory
16# - hg, svn detection
17# - virtualenv, rvm
18#
19# Screenshot:
20#
21# by Ryan Kanno <ryankanno@localkinegrinds.com>
22#
23# And yes, we code out in Hawaii. :D
24#
25# Note: I also am really new to this bash scripting game, so if you see things
26# that are flat out wrong, or if you think of something neat, just send a pull
27# request.
28
29# COLORS ======================================================================
Ryan1a83a4d2011-05-31 13:19:20 -100030ORANGE='\e[0;33m'
31GREY='\e[1:37m'
32
33DEFAULT_COLOR='\[${white}\]'
34
35USER_COLOR='\[${purple}\]'
36SUPERUSER_COLOR='\[${red}\]'
37MACHINE_COLOR=$ORANGE
Ryan0df98ca2011-06-01 11:22:22 -100038IP_COLOR=$ORANGE
Ryan Kanno0611f862011-05-31 21:38:41 -100039DIRECTORY_COLOR='\[${green}\]'
Ryan1a83a4d2011-05-31 13:19:20 -100040
Ryan Kanno0611f862011-05-31 21:38:41 -100041VE_COLOR='\[${cyan}\]'
42RVM_COLOR='\[${cyan}\]'
Ryan1a83a4d2011-05-31 13:19:20 -100043
Ryan Kanno9bd5b542011-05-31 21:23:33 -100044REF_COLOR='\[${purple}\]'
45
Ryan1a83a4d2011-05-31 13:19:20 -100046# SCM prompts
47SCM_THEME_PROMPT_DIRTY=' ${bold_red}✗${normal}'
48SCM_THEME_PROMPT_CLEAN=' ${bold_green}✓${normal}'
Ryan0df98ca2011-06-01 11:22:22 -100049SCM_THEME_PROMPT_PREFIX=" "
50SCM_THEME_PROMPT_SUFFIX=""
Ryan1a83a4d2011-05-31 13:19:20 -100051
52# Max length of PWD to display
53MAX_PWD_LENGTH=20
54
55# Max length of Git Hex to display
56MAX_GIT_HEX_LENGTH=5
57
Ryan0df98ca2011-06-01 11:22:22 -100058# FUNCS =======================================================================
Ryan09dfe752011-05-31 19:17:10 -100059function ip {
60 echo $(ifconfig en1 | grep "inet " | awk '{ print $2 }')
61}
62
Ryanaa2f1cb2011-05-31 18:41:07 -100063# Override function scm
64function scm {
65 if [[ -d .git ]]; then SCM=$GIT
66 elif [[ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]]; then SCM=$GIT
67 elif [[ -n "$(hg summary 2> /dev/null)" ]]; then SCM=$HG
68 elif [[ -d .svn ]]; then SCM=$SVN
69 else SCM='NONE'
70 fi
71}
72
Ryan1a83a4d2011-05-31 13:19:20 -100073# Displays the current virtualenv information
74function curr_virtualenv_info() {
75 [ ! -z "$VIRTUAL_ENV" ] && echo "`basename $VIRTUAL_ENV`"
76}
77
78# Displays the current rvm information w/gemset
79function curr_rvm_info() {
80 local ruby_version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
81 local ruby_gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
82
83 if [ "$ruby_version" != "" ]; then
84 [ "$ruby_gemset" != "" ] && ruby_gemset="@$ruby_gemset"
85 echo "$ruby_version$ruby_gemset"
86 fi
87}
88
89# Displays using ...
90function virtual_info() {
91 local virtual_env_info=$(curr_virtualenv_info)
92 local rvm_info=$(curr_rvm_info)
Ryan1a83a4d2011-05-31 13:19:20 -100093
94 # If no virtual info, just return
95 [ "$virtual_env_info" == "" -a "$rvm_info" == "" ] && return
96
Ryan Kanno0611f862011-05-31 21:38:41 -100097 local prompt=" using"
98
Ryan1a83a4d2011-05-31 13:19:20 -100099 # If virtual_env info present, append to prompt
100 [ "$virtual_env_info" != "" ] && prompt="$prompt virtualenv: ${VE_COLOR}$virtual_env_info${DEFAULT_COLOR}"
101
102 if [ "$rvm_info" != "" ]
103 then
104 [ "$virtual_env_info" != "" ] && prompt="$prompt,"
105 prompt="$prompt rvm: ${RVM_COLOR}$rvm_info${DEFAULT_COLOR}"
106 fi
Ryan Kanno0611f862011-05-31 21:38:41 -1000107 echo "$prompt"
Ryan1a83a4d2011-05-31 13:19:20 -1000108}
109
Ryan1a83a4d2011-05-31 13:19:20 -1000110# SCM information
111function scm_info() {
112 SCM_CHAR=$(scm_char)
113 [ "$SCM_CHAR" == "$SCM_NONE_CHAR" ] && return
Ryan Kanno0611f862011-05-31 21:38:41 -1000114 local prompt=" on"
Ryan1a83a4d2011-05-31 13:19:20 -1000115 [ "$SCM_CHAR" == "$SCM_GIT_CHAR" ] && echo "$prompt$(parse_git_info)" && return
Ryan06d71ab2011-05-31 16:10:30 -1000116 [ "$SCM_CHAR" == "$SCM_SVN_CHAR" ] && echo "$prompt$(parse_svn_info)" && return
117 [ "$SCM_CHAR" == "$SCM_HG_CHAR" ] && echo "$prompt$(parse_hg_info)" && return
Ryan1a83a4d2011-05-31 13:19:20 -1000118}
119
120# Parse git info
121function parse_git_info() {
122 if [[ -n $(git status -s 2> /dev/null |grep -v ^# |grep -v "working directory clean") ]]; then
123 state=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
124 else
125 state=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
126 fi
127 prefix=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
128 suffix=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
129 ref=$(git symbolic-ref HEAD 2> /dev/null) || return
130 rawhex=$(git rev-parse HEAD 2>/dev/null) || return
131
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000132 echo "$prefix${REF_COLOR}${ref#refs/heads/}${DEFAULT_COLOR}:${rawhex:0:$MAX_GIT_HEX_LENGTH}$state$suffix"
Ryan1a83a4d2011-05-31 13:19:20 -1000133}
134
Ryan06d71ab2011-05-31 16:10:30 -1000135# Parse hg info
136function parse_hg_info() {
Ryanaa2f1cb2011-05-31 18:41:07 -1000137 if [[ -n $(hg status 2> /dev/null) ]]; then
Ryan06d71ab2011-05-31 16:10:30 -1000138 state=${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
139 else
140 state=${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
141 fi
142 prefix=${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
143 suffix=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
Ryanaa2f1cb2011-05-31 18:41:07 -1000144 branch=$(hg summary 2> /dev/null | grep branch | awk '{print $2}')
145 changeset=$(hg summary 2> /dev/null | grep parent | awk '{print $2}')
Ryan06d71ab2011-05-31 16:10:30 -1000146
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000147 echo "$prefix${REF_COLOR}${branch}${DEFAULT_COLOR}:${changeset#*:}$state$suffix"
Ryan06d71ab2011-05-31 16:10:30 -1000148}
149
Ryan2a6d1da2011-05-31 19:03:28 -1000150# Parse svn info
151function parse_svn_info() {
152 if [[ -n $(svn status --ignore-externals -q 2> /dev/null) ]]; then
153 state=${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
154 else
155 state=${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
156 fi
157 prefix=${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
158 suffix=${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
159 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
160 revision=$(svn info 2> /dev/null | sed -ne 's#^Revision: ##p' )
161 [[ -z $ref ]] && return
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000162 echo -e "$prefix${REF_COLOR}$ref${DEFAULT_COLOR}:$revision$state$suffix"
Ryan2a6d1da2011-05-31 19:03:28 -1000163}
Ryan06d71ab2011-05-31 16:10:30 -1000164
Ryan1a83a4d2011-05-31 13:19:20 -1000165# Displays last X characters of pwd
166function limited_pwd() {
167
168 # Replace $HOME with ~ if possible
169 RELATIVE_PWD=${PWD/#$HOME/\~}
170
171 local offset=$((${#RELATIVE_PWD}-$MAX_PWD_LENGTH))
172
173 if [ $offset -gt "0" ]
174 then
175 local truncated_symbol="..."
176 TRUNCATED_PWD=${RELATIVE_PWD:$offset:$MAX_PWD_LENGTH}
177 echo "${truncated_symbol}/${TRUNCATED_PWD#*/}"
178 else
179 echo "${RELATIVE_PWD}"
180 fi
181}
182
183# Displays the current prompt
184function prompt() {
185
186 local UC=$USER_COLOR
187 [ $UID -eq "0" ] && UC=$SUPERUSER_COLOR
188
Ryan Kanno0611f862011-05-31 21:38:41 -1000189 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 -1000190 PS2='> '
191 PS4='+ '
192}
193
194PROMPT_COMMAND=prompt