Skip to content

Instantly share code, notes, and snippets.

@parndt
Created February 14, 2012 02:13
Show Gist options
  • Save parndt/1822547 to your computer and use it in GitHub Desktop.
Save parndt/1822547 to your computer and use it in GitHub Desktop.
My ~/.bash_profile contains this prompt thanks to @ndbroadbent
# Returns the current git branch (returns nothing if not a git repository)
parse_git_branch() {
\git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_git_dirty() {
[ -n "$(\git status --short 2> /dev/null)" ] && echo "±"
}
# Returns the current ruby version.
parse_ruby_version() {
if (which ruby | grep -q ruby); then
ruby -v | cut -d ' ' -f2
fi
}
# Returns the Travis CI status for a github project
parse_travis_status() {
local branch="$1"
if [ -z "$branch" ]; then branch="master"; fi
local stat_file=$(find_in_cwd_or_parent ".travis_status~")
if [ -e "$stat_file" ]; then
case "$(grep -m 1 "^$branch " "$stat_file")" in
*passed) echo "\[\e[01;32m\]✔ ";; # green
*failed) echo "\[\e[01;31m\]✘ ";; # red
*running) echo "\[\e[01;33m\]⁇ ";; # yellow
esac
fi
}
# Test whether file exists in current or parent directories
find_in_cwd_or_parent() {
local slashes=${PWD//[^\/]/}; local directory=$PWD;
for (( n=${#slashes}; n>0; --n )); do
test -e $directory/$1 && echo "$directory/$1" && return 0
directory=$directory/..
done
return 1
}
# Allow symbols to represent users & machines
user_symbol(){ [ -e $HOME/.user_sym ] && cat $HOME/.user_sym || echo "$USER"; }
host_symbol(){ [ -e /home/.hostname_sym ] && cat /home/.hostname_sym || echo "$HOSTNAME"; }
user_host_sep() { ([ -e $HOME/.user_sym ] && [ -e /home/.hostname_sym ]) || echo "@"; }
set_ps1() {
local user_str="\[$_usr_col\]$(user_symbol)\[$_sep_col\]$(user_host_sep)\[$_hst_col\]$(host_symbol)\[$_txt_col\]"
local dir_str="\[$_cwd_col\]\w"
local git_branch=`parse_git_branch`
local git_dirty=`parse_git_dirty`
local trav_str=`parse_travis_status "$git_branch"`
local ruby=`parse_ruby_version`
git_str="\[$_git_col\]$git_branch\[$_wrn_col\]$git_dirty"
# Git repo & ruby version
if [ -n "$git_branch" ] && [ -n "$ruby" ]; then
env_str="\[$_env_col\][$git_str\[$_env_col\]|$ruby]"
# Just git repo
elif [ -n "$git_branch" ]; then
env_str="\[$_env_col\][$git_str\[$_env_col\]]"
# Just ruby version
elif [ -n "$ruby" ]; then
env_str="\[$_env_col\][$ruby]"
else
unset env_str
fi
# < username >@< hostname > < current directory > < ci status > [< git branch >|< ruby version >]
#PS1="${debian_chroot:+($debian_chroot)}$user_str $dir_str $trav_str$env_str\[$_chr_col\]$ \[$_txt_col\]"
PS1="\t:\[\033[32m\]${debian_chroot:+($debian_chroot)} $trav_str$env_str \[$_chr_col\]\[\033[34m\]$dir_str\[\033[00m\]$ \[$_txt_col\]"
}
### END PS1
# Set custom prompt
PROMPT_COMMAND+='set_ps1;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment