Skip to content

Instantly share code, notes, and snippets.

@grieve
Last active December 14, 2015 03:09
Show Gist options
  • Save grieve/5019230 to your computer and use it in GitHub Desktop.
Save grieve/5019230 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ANSI color codes
RS="\[\033[0m\]" # reset
HC="\[\033[1m\]" # hicolor
UL="\[\033[4m\]" # underline
INV="\[\033[7m\]" # inverse background and foreground
CON="\[\033[8m\]" # concealed
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white
function git_branch {
branch=$(git branch | grep \* | sed 's/^\* //')
if [ $branch == "" ]; then
branch="$FRED(not git)$RS"
else
branch="$FGRN($branch)$RS"
fi
echo $branch
}
shorten_path()
{
x=${1}
len=${#x}
max_len=$2
if [ $len -gt $max_len ]
then
# finds all the '/' in
# the path and stores their
# positions
#
pos=( )
for ((i=0;i<len;i++))
do
if [ "${x:i:1}" == "/" ]
then
pos=( ${pos[@]} $i )
fi
done
pos=( ${pos[@]} $len )
# we have the '/'s, let's find the
# left-most that doesn't break the
# length limit
#
i=0
while [ $((len-pos[i])) -gt $((max_len-3)) ]
do
i=$((i+1))
done
# let us check if it's OK to
# print the whole thing
#
if [ ${pos[i]} == 0 ]
then
# the path is shorter than
# the maximum allowed length,
# so no need for ...
#
echo ${x}
elif [ ${pos[i]} == $len ]
then
# constraints are broken because
# the maximum allowed size is smaller
# than the last part of the path, plus
# '...'
#
echo ...${x:((len-max_len+3))}
else
# constraints are satisfied, at least
# some parts of the path, plus ..., are
# shorter than the maximum allowed size
#
echo ...${x:pos[i]}
fi
else
echo ${x}
fi
}
function topline {
WIDTH=`tput cols`
HOST=`hostname`
HOST_WIDTH=${#HOST}
JOBS="$(jobs | wc -l)"
JOBS_WIDTH=${#JOBS}
DIR=$(shorten_path `pwd` 40)
DIR_LENGTH=${#DIR}
SPACE=$(($WIDTH - $HOST_WIDTH - $DIR_LENGTH - $JOBS_WIDTH - 27))
echo -n ":$DIR"
for (( c=1; c<=$SPACE; c++))
do
echo -n " "
done
echo -n "$OUTPUT$JOBS jobs running "
}
function branchremote {
echo "\`git branch 2> /dev/null | grep \* | sed 's/^\* //'\`$RS$FCYN \`git remote 2> /dev/null | tr '\\n' '/' | sed 's/\/$//'\`"
}
PS1="$FCYN$UL$HC `hostname`$RS$FCYN$UL\$(topline)- \t $RS #\! $RS$FCYN$HC\`git branch 2> /dev/null | grep \* | sed 's/^\* //'\`$RS$FCYN \`git remote 2> /dev/null | tr '\\n' '/' | sed 's/\/$//' \`$RS> "
clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment