Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Created January 6, 2016 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletaylored/3bf6089e31e8c378c99a to your computer and use it in GitHub Desktop.
Save kyletaylored/3bf6089e31e8c378c99a to your computer and use it in GitHub Desktop.
A better command prompt for Mac, including git status of current directory.
function directory_size () {
# Sum the number of bytes in a directory listing
size=$(ls -l | awk 'BEGIN {x=0} NR > 1 {x+=$5} END {
split("B KB MB GB TB PB",type);
if (x > 0) {
for(i=5;y < 1;i--) {
y = x / (2**(10*i));
}
}
printf "%.2f %s", y, type[i+2]
}');
echo "${size}";
}
function parse_git () {
output='';
git_status=$(git status 2> /dev/null);
if [[ $? -eq 0 ]]; then
output="${output}$(parse_git_branch "${git_status}")";
output="${output}$(parse_git_ahead_behind "${git_status}")";
fi;
echo "${output}"
}
function parse_git_ahead_behind () {
output='';
ahead=($(grep 'ahead' <<< "${1}"));
if [[ ${#ahead[*]} -gt 0 ]]; then
for ((i = 0; i < ${#ahead[*]}; i++ )); do
string="${ahead[$i]}";
if [ -n "$(grep commit <<< "${string}")" ]; then
let i=i-1;
output="${output}(+${ahead[$i]})";
break;
fi;
done;
fi;
behind=($(grep 'behind' <<< "${1}"));
if [[ ${#behind[*]} -gt 0 ]]; then
for ((i = 0; i < ${#behind[*]}; i++ )); do
string="${behind[$i]}";
if [ -n "$(grep commit <<< "${string}")" ]; then
let i=i-1;
output="${output}(-${behind[$i]})";
break;
fi;
done;
fi;
echo "${output}"
}
function parse_git_branch () {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ (\1$(parse_git_clean "${1}"))/"
}
function parse_git_clean () {
[[ $(tail -n1 <<< "${1}") != "nothing to commit (working directory clean)" ]] && echo "*"
}
export PS1="\n\
\[\e[0;33m\][\$(date '+%m-%d %H:%M')]
\[\e[0;36m\]\$(directory_size) - \w\$(parse_git)\[\e[0m\]\n\
\[\e[0;38m\]\u\[\e[0;37m\]@\[\e[0;32m\]\h\[\e[0;37m\]$ \[\e[m\]";
if [ "$(id -u)" -eq 0 ]; then
export PS1="\n\
\[\e[0;31m\][\$(date '+%m-%d %H:%M')]
\[\e[0;35m\]\$(directory_size) - \w\$(parse_git)\[\e[0m\]\n\
\[\e[0;38m\]\u\[\e[0;37m\]@\[\e[1;31m\]\h\[\e[0;37m\]# \[\e[m\]";
fi
export LSCOLORS=ExFxCxDxBxegedabagacad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment