Instantly share code, notes, and snippets.

Embed
What would you like to do?
A little way of hacking bash to get some insight into your current shell environment
#
# Author: Kelvin Jones
# Github: https://github.com/kelvinj
# Inspired by http://marcpalmer.net/hacking-my-shell-prompt-so-i-make-less-mistakes-working-with-xcode-projects/
#
# This is a .bash_profile file that you can write to your home directory to give
# you a custom shell prompt with various bits of info about your environment.
#
# Available options:
# - host
# - host_full
# - user
# - dir
# - dir_parent
# - dir_basename
# - git
# - laravel
# - xcode
# - dollar ($ sign)
## Using the options above, change this config to get the desired output
PS1_CONFIG="{host}:{dir_parent} {xcode} {laravel} {git} {dollar} "
# For each option, you can optionally set a text colour you wish to use, see the list of colours in the color_map below.
declare -A opt_colors=(
["git"]="CYAN"
["laravel"]="ORANGE"
["dollar"]="BLUE"
["xcode"]="YELLOW"
)
# make sure we still process .bashrc if it exists
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
# make sure we still process .profile if it exists
if [ -f ~/.profile ]; then
source ~/.profile
fi
declare -A color_map=(
["NORMAL"]='\033[0;00m'
["RESET"]='\033[0;00m'
["BLACK"]='\033[22;30m'
["RED"]='\033[22;31m'
["GREEN"]='\033[22;32m'
["YELLOW"]='\033[22;33m'
["ORANGE"]='\033[38;5;208m'
["BLUE"]='\033[22;34m'
["MAGENTA"]='\033[22;35m'
["CYAN"]='\033[22;36m'
["GRAY"]='\033[22;37m'
["WHITE"]='\033[22;37m'
["DARKGRAY"]='\033[01;30m'
["LIGHTRED"]='\033[01;31m'
["LIGHTGREEN"]='\033[01;32m'
["LIGHTYELLOW"]='\033[01;33m'
["LIGHTBLUE"]='\033[01;34m'
["LIGHTMAGENTA"]='\033[01;35m'
["LIGHTCYAN"]='\033[01;36m'
["LIGHTWHITE"]='\033[01;37m'
)
function echo_ {
if [[ -z "$2" ]]; then
local text=$1
local color=${color_map[NORMAL]}
else
local color=${color_map[$1]}
local text=$2
fi
local reset=${color_map[RESET]}
echo -e "$color$text$reset"
}
# get current branch in git repo
function ps1_git() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]; then
echo_ ${opt_colors[git]} "[${BRANCH}]"
else
echo ""
fi
}
function ps1_laravel {
if [ -f "composer.json" ]; then
local version=`composer show 2>&1 | grep "laravel\/framework" | awk '{gsub("v","",$2); print $2}' | sed -E -e 's/^([0-9].[0-9]+).*/\1/g'`
if [ ! "${version}" == "" ]; then
echo_ ${opt_colors[laravel]} "L$version"
else
echo ""
fi
else
echo ""
fi
}
# Parse out version from xcodebuild's plist based on output of xcode-select
# Thanks to @danielpunkass for the performance improvement that avoids executing "xcodebuild -version"
function ps1_xcode {
echo_ ${opt_colors[xcode]} `plutil -p \`xcode-select -p\`/../Info.plist | grep -e CFBundleShortVersionString | sed 's/[^0-9\.]*//g'`
}
ps1_host=`hostname -s`
function ps1_host {
echo_ ${opt_colors[host]} $ps1_host
}
ps1_host_full=`hostname`
function ps1_host_full {
echo_ ${opt_colors[host_full]} $ps1_host_full
}
ps1_user=`whoami`
function ps1_user {
echo_ ${opt_colors[user]} $ps1_user
}
function ps1_dollar {
echo_ ${opt_colors[dollar]} "$"
}
function ps1_dir {
echo_ ${opt_colors[dir]} ${PWD}
}
function ps1_dir_basename {
echo_ ${opt_colors[dir_basename]} `basename ${PWD}`
}
function ps1_dir_parent {
echo_ ${opt_colors[dir_parent]} ${PWD#"${PWD%/*/*}/"}
}
export PS1=`echo "$PS1_CONFIG" | sed -E -e 's/\{([a-z_]+)\}/\`ps1_\1\`/g'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment