Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active December 29, 2015 16:08
Show Gist options
  • Select an option

  • Save zeroeth/7694944 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/7694944 to your computer and use it in GitHub Desktop.
dotfile prompts from my various machine evolutions
# from bendyworks
function parse_git_branch {
\git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
# from my old dotfile
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
function parse_rvm_ruby {
rvm-prompt u
}
function parse_rvm_gemset {
rvm-prompt g
}
function date_display {
date +%H:%M
}
function proml {
local CLEAR="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local BLACK="\[\033[0;30m\]" # black
local RED="\[\033[0;31m\]" # red
local GREEN="\[\033[0;32m\]" # green
local YELLOW="\[\033[0;33m\]" # yellow
local BLUE="\[\033[0;34m\]" # blue
local MAGENTA="\[\033[0;35m\]" # magenta
local CYAN="\[\033[0;36m\]" # cyan
local WHITE="\[\033[0;37m\]" # white
# emphasized (bolded) colors
local EBLACK="\[\033[1;30m\]"
local ERED="\[\033[1;31m\]"
local EGREEN="\[\033[1;32m\]"
local EYELLOW="\[\033[1;33m\]"
local EBLUE="\[\033[1;34m\]"
local EMAGENTA="\[\033[1;35m\]"
local ECYAN="\[\033[1;36m\]"
local EWHITE="\[\033[1;37m\]"
# background colors
local BGBLACK="\[\033[40m\]"
local BGRED="\[\033[41m\]"
local BGGREEN="\[\033[42m\]"
local BGYELLOW="\[\033[43m\]"
local BGBLUE="\[\033[44m\]"
local BGMAGENTA="\[\033[45m\]"
local BGCYAN="\[\033[46m\]"
local BGWHITE="\[\033[47m\]"
# want to add status return but its not working
# \${?} works inside PS1
PS1="${RED}$(parse_rvm_ruby) ${EGREEN}\u@\h ${EBLUE}\w ${MAGENTA}$(parse_git_branch)${CYAN}$(parse_rvm_gemset) ${RETURN_COLOR}\$${CLEAR} "
PS2='> '
PS4='+ '
}
PROMPT_COMMAND=proml
# Colors for scripts
CLR="\[\033[0m\]" # unsets color to term's fg color
# regular colors
BLK="\[\033[0;30m\]" # black
RED="\[\033[0;31m\]" # red
GRN="\[\033[0;32m\]" # green
YLW="\[\033[0;33m\]" # yellow
BLU="\[\033[0;34m\]" # blue
MGN="\[\033[0;35m\]" # magenta
CYN="\[\033[0;36m\]" # cyan
WHT="\[\033[0;37m\]" # white
# emphasized (bolded) colors
EBLK="\[\033[1;30m\]"
ERED="\[\033[1;31m\]"
EGRN="\[\033[1;32m\]"
EYLW="\[\033[1;33m\]"
EBLU="\[\033[1;34m\]"
EMGN="\[\033[1;35m\]"
ECYN="\[\033[1;36m\]"
EWHY="\[\033[1;37m\]"
# background colors
BGBLK="\[\033[40m\]"
BGRED="\[\033[41m\]"
BGGRN="\[\033[42m\]"
BGYLY="\[\033[43m\]"
BGBLU="\[\033[44m\]"
BGMGN="\[\033[45m\]"
BGCYN="\[\033[46m\]"
BGWHT="\[\033[47m\]"
# Outputs the current trunk, branch, or tag
svn_branch() {
local url=
url=`svn info 2> /dev/null | awk '/URL:/ {print $2}'`
if [[ $url =~ trunk ]]; then
echo trunk
elif [[ $url =~ /branches/ ]]; then
echo $url | sed -e 's#^.*/\(branches/.*\)/.*$#\1#'
elif [[ $url =~ /tags/ ]]; then
echo $url | sed -e 's#^.*/\(tags/.*\)/.*$#\1#'
fi
}
# Outputs the current revision
svn_rev() {
local r=$(svn info 2> /dev/null | awk '/Revision:/ {print $2}')
local svnst flag
svnst=$(svn status 2> /dev/null | grep '^\s*[?ACDMR?!]')
[ -z "$svnst" ] && flag=''
r=$r$flag
echo $r
}
function svn_prompt {
echo $(svn_branch) $(svn_rev) | sed -e 's/.*/ (&)/;s/()//;s/\ $//'
}
function git_prompt {
\git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YLW}↑"
else
remote="${YLW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
function parse_rvm_prompt {
rvm-prompt v g
}
function date_display {
date +%H:%M
}
function proml {
#PS1="$CLR\u@\h:\W$YLW\$(svn_branch)$GRN\$(svn_rev)$MGN\$(git_prompt)$EBLU\$$CLR "
PS1="$CLR$WHT\u$MGN@$EBLK\h:$ECYN\W${YLW}$(svn_prompt)${MGN}$(git_prompt)$BLU\$$CLR "
PS2='> '
PS4='+ '
}
proml
# PS1='\u@\h:\W\[\033[01;33m\]$(__git_svn_ps1)\[\033[00m\]$ '
### PROMPT SETUP ###
function set_window_and_tab_title
{
local title="$1"
if [[ -z "$title" ]]; then
title="root"
fi
local tmpdir=~/Library/Caches/${FUNCNAME}_temp
local cmdfile="$tmpdir/$title"
# Set window title
# echo -n -e "\e]0;${title}\a"
# Set tab title
if [[ -n ${CURRENT_TAB_TITLE_PID:+1} ]]; then
kill $CURRENT_TAB_TITLE_PID
fi
mkdir -p $tmpdir
ln /bin/sleep "$cmdfile"
"$cmdfile" 10 &
CURRENT_TAB_TITLE_PID=$(jobs -x echo %+)
disown %+
kill -STOP $CURRENT_TAB_TITLE_PID
command rm -f "$cmdfile"
}
# Colours
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
MAGENTA="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
GRAY="\[\033[1;30m\]"
function rvm_ruby {
ruby_version="$(rvm-prompt u)"
if [[ -z "$ruby_version" ]]; then
echo ""
else
echo "$ruby_version"
fi
}
function rvm_gemset {
echo "$(rvm-prompt g)"
}
# Get the current git branch, if there is one
function parse_git_branch {
\git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " ${CYAN}(${branch}${MAGENTA}$(rvm_gemset)${CYAN})${remote}${state}"
else
if [[ ! -z "$(rvm_gemset)" ]]; then
echo " ${CYAN}(${MAGENTA}$(rvm_gemset)${CYAN})"
fi
fi
}
function prompt_func {
previous_return_value=$?;
prompt="${TITLEBAR}${LIGHT_GRAY}\w$(parse_git_branch)${COLOR_NONE}"
if test $previous_return_value -eq 0
then
PS1="${GREEN}$(rvm_ruby) ${COLOR_NONE}${prompt}${GREEN} \$${COLOR_NONE} "
else
PS1="${RED}$(rvm_ruby) ${COLOR_NONE}${prompt}${RED} \$${COLOR_NONE} "
fi
# set_window_and_tab_title "${PWD##*/}"
}
[ -z "$PS1" ] || stty -ixon
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment