Skip to content

Instantly share code, notes, and snippets.

@GRArmstrong
Created July 13, 2018 11:48
Show Gist options
  • Save GRArmstrong/84b0934f97013319e40ec5dc5d54d158 to your computer and use it in GitHub Desktop.
Save GRArmstrong/84b0934f97013319e40ec5dc5d54d158 to your computer and use it in GitHub Desktop.
Modified pwd script with custom colours.
# This function from: https://wiki.archlinux.org/index.php/Color_Bash_Prompt_%28%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%29#Wolfman.27s
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_shortener() {
# How many characters of the $PWD should be kept
local pwdmaxlen=80
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
parse_git_branch() {
BGIT_PS=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /')
}
function setPrompt {
COLOR1="\[\033[1;35m\]" #First color
COLOR2="\[\033[1;35m\]" #Second color
COLOR_GIT="\[\033[0;92m\]" #Third color
COLOR_USER="\[\033[0;91m\]" #Third color
NO_COLOR="\[\033[0m\]" #Transparent - don't change
case $TERM in
xterm*)
TITLEBAR="\[\033]0;\h - \w\007\]"
;;
*)
TITLEBAR=""
;;
esac
local dash_open="${COLOR1}-${COLOR2}-"
local dash_close="${COLOR2}-${COLOR1}>"
local spacer="${COLOR2}-"
local jobs_and_history="${COLOR2}(${COLOR1}\!${COLOR2}:${COLOR1}\j${COLOR2})"
local user_host="${COLOR2}(${COLOR1}\u${COLOR2}@${COLOR1}\h${COLOR2})"
local host="${COLOR2}(${COLOR1}\H${COLOR2})"
local root_or_not="${COLOR2}(${COLOR1}\\\$${COLOR2})"
local cwd="${COLOR2}(${COLOR1}\w${COLOR2})"
#local gitstuff=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')
#PS1="${TITLEBAR}${COLOR1}-${COLOR2}-(${COLOR1}\!${COLOR2}:${COLOR1}\j${COLOR2})-(${COLOR1}\w${COLOR2})-${COLOR1}-\n-${COLOR2}-(${COLOR1}\u${COLOR2}@${COLOR1}\H${COLOR2})-(${COLOR1}\\\$${COLOR2})-${COLOR1}- ${NO_COLOR}"
#PS1="${TITLEBAR}${dash_open}${cwd}${spacer}${root_or_not}${dash_close}\n${dash_open}${jobs_and_history}${spacer}${host}${dash_close}${NO_COLOR} "
#PS2="${COLOR2}--${COLOR1}- ${NO_COLOR}"
if [ -n "$SSH_CLIENT" ]
then
PS1="${TITLEBAR}${BGIT_PS}${user_host}${COLOR_GIT}"'${NEW_PWD}'"${COLOR2}\n\$${NO_COLOR} "
else
PS1="${TITLEBAR}${COLOR_GIT}${BGIT_PS}${COLOR_USER}\u ${COLOR1}"'${NEW_PWD}'":${COLOR2}\n\$${NO_COLOR} "
fi
PS2="$spacer$dash_close$NO_COLOR "
}
#Determine and display the exit Status of the last command, if non-zero.
function checkExitStatus() {
local status="$?"
local signal=""
local COLOR1="\033[0;0;33m" #First color
local COLOR2="\033[0;0;36m" #Second color
local NO_COLOR="\033[0m" #Transparent - don't change
if [ ${status} -ne 0 -a ${status} != 128 ]; then
# If process exited by a signal, determine name of signal.
if [ ${status} -gt 128 ]; then
signal="$(builtin kill -l $((${status} - 128)) 2>/dev/null)"
if [ "$signal" ]; then
signal="$signal"
fi
fi
echo -e "${COLOR1}[Exit ${COLOR2}${status} ${signal}${COLOR1}]${NO_COLOR}" 1>&2
#echo -ne "${COLOR1}[Exit ${COLOR2}${status}${COLOR1} ${COLOR2}${signal}${COLOR1}]${NO_COLOR} " 1>&2
fi
return 0
}
print_prompt_time() {
printf "%*s\r" $(tput cols) "$(date '+%T')"
}
promptCmd() {
bash_prompt_shortener
parse_git_branch
checkExitStatus
setPrompt
print_prompt_time
# Enable virtualenv
if [ -n "$VIRTUAL_ENV" ]
then
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="«`basename \"$VIRTUAL_ENV\"`»$PS1"
fi
export PS1
fi
}
PROMPT_COMMAND=promptCmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment