blob: b6937508a743e92b43f8103b3128a16a0b1fba65 [file] [log] [blame]
Eitan Adler3fc60b52012-04-17 00:24:58 -04001#!/usr/bin/env 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
Ryan Kannocaa1bba2011-07-09 14:04:08 -100033# IMPORTANT THINGS TO CHANGE ==================================================
34
35# Show IP in prompt
36# One thing to be weary about if you have slow Internets
37IP_ENABLED=1
38
39# virtual prompts
40VIRTUAL_PROMPT_ENABLED=1
41
Ryan0df98ca2011-06-01 11:22:22 -100042# COLORS ======================================================================
Ryan Kanno8e110772011-06-17 10:31:49 -100043ORANGE='\[\e[0;33m\]'
Ryan1a83a4d2011-05-31 13:19:20 -100044
JFSIII8b380c12011-06-18 10:57:59 -040045DEFAULT_COLOR="${white}"
Ryan1a83a4d2011-05-31 13:19:20 -100046
JFSIII8b380c12011-06-18 10:57:59 -040047USER_COLOR="${purple}"
48SUPERUSER_COLOR="${red}"
Ryan1a83a4d2011-05-31 13:19:20 -100049MACHINE_COLOR=$ORANGE
Ryan0df98ca2011-06-01 11:22:22 -100050IP_COLOR=$ORANGE
JFSIII8b380c12011-06-18 10:57:59 -040051DIRECTORY_COLOR="${green}"
Ryan1a83a4d2011-05-31 13:19:20 -100052
JFSIII8b380c12011-06-18 10:57:59 -040053VE_COLOR="${cyan}"
54RVM_COLOR="${cyan}"
Ryan1a83a4d2011-05-31 13:19:20 -100055
JFSIII8b380c12011-06-18 10:57:59 -040056REF_COLOR="${purple}"
Ryan Kanno9bd5b542011-05-31 21:23:33 -100057
Ryan1a83a4d2011-05-31 13:19:20 -100058# SCM prompts
JFSIII8b380c12011-06-18 10:57:59 -040059SCM_THEME_PROMPT_DIRTY=" ${bold_red}✗${normal}"
60SCM_THEME_PROMPT_CLEAN=" ${bold_green}✓${normal}"
Ryan09bd0ef2011-06-08 13:34:27 -100061SCM_THEME_PROMPT_PREFIX=' on '
Ryan0713fdc2011-06-08 11:32:50 -100062SCM_THEME_PROMPT_SUFFIX=''
63
Ryan Kannob96345e2011-06-09 12:18:11 -100064# rvm prompts
Ryan0713fdc2011-06-08 11:32:50 -100065RVM_THEME_PROMPT_PREFIX=''
66RVM_THEME_PROMPT_SUFFIX=''
67
Ryan Kannob96345e2011-06-09 12:18:11 -100068# virtualenv prompts
Ryan0713fdc2011-06-08 11:32:50 -100069VIRTUALENV_THEME_PROMPT_PREFIX=''
70VIRTUALENV_THEME_PROMPT_SUFFIX=''
Ryan1a83a4d2011-05-31 13:19:20 -100071
Ryan Kannob96345e2011-06-09 12:18:11 -100072VIRTUAL_THEME_PROMPT_PREFIX=' using '
73VIRTUAL_THEME_PROMPT_SUFFIX=''
74
Ryan1a83a4d2011-05-31 13:19:20 -100075# Max length of PWD to display
76MAX_PWD_LENGTH=20
77
78# Max length of Git Hex to display
79MAX_GIT_HEX_LENGTH=5
80
Ryan Kanno49f897a2011-06-09 13:20:41 -100081# IP address
82IP_SEPARATOR=', '
83
Ryan0df98ca2011-06-01 11:22:22 -100084# FUNCS =======================================================================
Ryan Kannodcb0dde2011-06-07 08:25:45 -100085
Ryan09dfe752011-05-31 19:17:10 -100086function ip {
Ryan Kanno49f897a2011-06-09 13:20:41 -100087 myip=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+')
88 echo -e "$(ips | sed -e :a -e '$!N;s/\n/${IP_SEPARATOR}/;ta' | sed -e 's/127\.0\.0\.1\${IP_SEPARATOR}//g'), ${myip}"
Ryan09dfe752011-05-31 19:17:10 -100089}
90
Ryan Kanno86aa9ff2011-07-07 22:13:29 -100091# Displays ip prompt
92function ip_prompt_info() {
93 if [[ $IP_ENABLED == 1 ]]; then
94 echo -e " ${DEFAULT_COLOR}(${IP_COLOR}$(ip)${DEFAULT_COLOR})"
95 fi
96}
97
Ryan Kannob96345e2011-06-09 12:18:11 -100098# Displays virtual info prompt (virtualenv/rvm)
99function virtual_prompt_info() {
Ryan0713fdc2011-06-08 11:32:50 -1000100 local virtual_env_info=$(virtualenv_prompt)
Hendrik Mans1be44672012-01-26 16:07:06 +0100101 local rvm_info=$(ruby_version_prompt)
Ryan Kannob96345e2011-06-09 12:18:11 -1000102 local virtual_prompt=""
103
104 local prefix=${VIRTUAL_THEME_PROMPT_PREFIX}
105 local suffix=${VIRTUAL_THEME_PROMPT_SUFFIX}
Ryan1a83a4d2011-05-31 13:19:20 -1000106
107 # If no virtual info, just return
Ryan Kannob96345e2011-06-09 12:18:11 -1000108 [[ -z "$virtual_env_info" && -z "$rvm_info" ]] && return
Ryan Kanno0611f862011-05-31 21:38:41 -1000109
Ryan1a83a4d2011-05-31 13:19:20 -1000110 # If virtual_env info present, append to prompt
Ryan Kannob96345e2011-06-09 12:18:11 -1000111 [[ -n "$virtual_env_info" ]] && virtual_prompt="virtualenv: ${VE_COLOR}$virtual_env_info${DEFAULT_COLOR}"
Ryan1a83a4d2011-05-31 13:19:20 -1000112
Ryan Kannob96345e2011-06-09 12:18:11 -1000113 if [[ -n "$rvm_info" ]]
Ryan1a83a4d2011-05-31 13:19:20 -1000114 then
Ryan Kannob96345e2011-06-09 12:18:11 -1000115 [[ -n "$virtual_env_info" ]] && virtual_prompt="$virtual_prompt, "
116 virtual_prompt="${virtual_prompt}rvm: ${RVM_COLOR}$rvm_info${DEFAULT_COLOR}"
Ryan1a83a4d2011-05-31 13:19:20 -1000117 fi
Ryan Kannob96345e2011-06-09 12:18:11 -1000118 echo -e "$prefix$virtual_prompt$suffix"
Ryan1a83a4d2011-05-31 13:19:20 -1000119}
120
Ryan1a83a4d2011-05-31 13:19:20 -1000121# Parse git info
Ryan28e2aed2011-06-08 11:57:16 -1000122function git_prompt_info() {
Ryan1a83a4d2011-05-31 13:19:20 -1000123 if [[ -n $(git status -s 2> /dev/null |grep -v ^# |grep -v "working directory clean") ]]; then
Ryan Kannob96345e2011-06-09 12:18:11 -1000124 state=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
Ryan1a83a4d2011-05-31 13:19:20 -1000125 else
Ryan Kannob96345e2011-06-09 12:18:11 -1000126 state=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
Ryan1a83a4d2011-05-31 13:19:20 -1000127 fi
128 prefix=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
129 suffix=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
130 ref=$(git symbolic-ref HEAD 2> /dev/null) || return
Ryan Kannob96345e2011-06-09 12:18:11 -1000131 commit_id=$(git rev-parse HEAD 2>/dev/null) || return
Ryan1a83a4d2011-05-31 13:19:20 -1000132
Ryan Kannob96345e2011-06-09 12:18:11 -1000133 echo -e "$prefix${REF_COLOR}${ref#refs/heads/}${DEFAULT_COLOR}:${commit_id:0:$MAX_GIT_HEX_LENGTH}$state$suffix"
Ryan1a83a4d2011-05-31 13:19:20 -1000134}
135
Ryan06d71ab2011-05-31 16:10:30 -1000136# Parse hg info
Ryan28e2aed2011-06-08 11:57:16 -1000137function hg_prompt_info() {
Ryanaa2f1cb2011-05-31 18:41:07 -1000138 if [[ -n $(hg status 2> /dev/null) ]]; then
Ryan Kannob96345e2011-06-09 12:18:11 -1000139 state=${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
Ryan06d71ab2011-05-31 16:10:30 -1000140 else
Ryan Kannob96345e2011-06-09 12:18:11 -1000141 state=${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
Ryan06d71ab2011-05-31 16:10:30 -1000142 fi
143 prefix=${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
144 suffix=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
Ryanaa2f1cb2011-05-31 18:41:07 -1000145 branch=$(hg summary 2> /dev/null | grep branch | awk '{print $2}')
146 changeset=$(hg summary 2> /dev/null | grep parent | awk '{print $2}')
Ryan06d71ab2011-05-31 16:10:30 -1000147
Ryan Kannob96345e2011-06-09 12:18:11 -1000148 echo -e "$prefix${REF_COLOR}${branch}${DEFAULT_COLOR}:${changeset#*:}$state$suffix"
Ryan06d71ab2011-05-31 16:10:30 -1000149}
150
Ryan2a6d1da2011-05-31 19:03:28 -1000151# Parse svn info
Ryan28e2aed2011-06-08 11:57:16 -1000152function svn_prompt_info() {
Ryan2a6d1da2011-05-31 19:03:28 -1000153 if [[ -n $(svn status --ignore-externals -q 2> /dev/null) ]]; then
Ryan Kannob96345e2011-06-09 12:18:11 -1000154 state=${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
Ryan2a6d1da2011-05-31 19:03:28 -1000155 else
Ryan Kannob96345e2011-06-09 12:18:11 -1000156 state=${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
Ryan2a6d1da2011-05-31 19:03:28 -1000157 fi
158 prefix=${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
159 suffix=${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
160 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
Ryan2a6d1da2011-05-31 19:03:28 -1000161 [[ -z $ref ]] && return
Ryan Kannob96345e2011-06-09 12:18:11 -1000162
163 revision=$(svn info 2> /dev/null | sed -ne 's#^Revision: ##p' )
164
Ryan Kanno9bd5b542011-05-31 21:23:33 -1000165 echo -e "$prefix${REF_COLOR}$ref${DEFAULT_COLOR}:$revision$state$suffix"
Ryan2a6d1da2011-05-31 19:03:28 -1000166}
Ryan06d71ab2011-05-31 16:10:30 -1000167
Ryan1a83a4d2011-05-31 13:19:20 -1000168# Displays last X characters of pwd
169function limited_pwd() {
170
171 # Replace $HOME with ~ if possible
172 RELATIVE_PWD=${PWD/#$HOME/\~}
173
174 local offset=$((${#RELATIVE_PWD}-$MAX_PWD_LENGTH))
175
176 if [ $offset -gt "0" ]
177 then
178 local truncated_symbol="..."
179 TRUNCATED_PWD=${RELATIVE_PWD:$offset:$MAX_PWD_LENGTH}
Ryan Kannob96345e2011-06-09 12:18:11 -1000180 echo -e "${truncated_symbol}/${TRUNCATED_PWD#*/}"
Ryan1a83a4d2011-05-31 13:19:20 -1000181 else
Ryan Kannob96345e2011-06-09 12:18:11 -1000182 echo -e "${RELATIVE_PWD}"
Ryan1a83a4d2011-05-31 13:19:20 -1000183 fi
184}
185
186# Displays the current prompt
187function prompt() {
Ryan Kannob96345e2011-06-09 12:18:11 -1000188 local UC=$USER_COLOR
189 [ $UID -eq "0" ] && UC=$SUPERUSER_COLOR
Ryan1a83a4d2011-05-31 13:19:20 -1000190
Ryan Kannob96345e2011-06-09 12:18:11 -1000191 if [[ $VIRTUAL_PROMPT_ENABLED == 1 ]]; then
Ryan Kanno86aa9ff2011-07-07 22:13:29 -1000192 PS1="$(scm_char) ${UC}\u ${DEFAULT_COLOR}at ${MACHINE_COLOR}\h$(ip_prompt_info) ${DEFAULT_COLOR}in ${DIRECTORY_COLOR}$(limited_pwd)${DEFAULT_COLOR}$(virtual_prompt_info)$(scm_prompt_info)${reset_color} \$ "
Ryan Kannob96345e2011-06-09 12:18:11 -1000193 else
Ryan Kanno86aa9ff2011-07-07 22:13:29 -1000194 PS1="$(scm_char) ${UC}\u ${DEFAULT_COLOR}at ${MACHINE_COLOR}\h$(ip_prompt_info) ${DEFAULT_COLOR}in ${DIRECTORY_COLOR}$(limited_pwd)${DEFAULT_COLOR}$(scm_prompt_info)${reset_color} \$ "
Ryan Kannob96345e2011-06-09 12:18:11 -1000195 fi
196 PS2='> '
197 PS4='+ '
Ryan1a83a4d2011-05-31 13:19:20 -1000198}
199
200PROMPT_COMMAND=prompt