Skip to content

Instantly share code, notes, and snippets.

@ByScripts
Last active October 9, 2015 07:37
Show Gist options
  • Select an option

  • Save ByScripts/3462873 to your computer and use it in GitHub Desktop.

Select an option

Save ByScripts/3462873 to your computer and use it in GitHub Desktop.
Bash and Zsh prompt with Git support
Color_Off="\e[0m" # Text Reset
Cyan="\e[0;36m" # Cyan
Red="\e[1;31m" # Bold Red
Green="\e[1;32m" # Bold Green
Yellow="\e[0;93m" # Yellow
Blue="\e[0;94m" # Blue
ICyan="\e[0;96m" # Intense Cyan
function getShortWorkingDirectory {
escaped_home=$(echo $HOME | sed -e 's/[\/&]/\\&/g')
short_working_directory=$(pwd | sed -e "s/$escaped_home/~/g")
echo $short_working_directory
}
function getGitBranch {
echo $(__git_ps1 "%s")
}
function getGitStatus {
if [[ $(getGitBranch) != "" ]]; then
git status | grep "nothing to commit" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "ok"
else
echo "nok"
fi
else
echo "nogit"
fi
}
function gitPrompt {
dir=$(getShortWorkingDirectory)
branch=$(getGitBranch)
status=$(getGitStatus)
time=$(date +%H:%M:%S)
before="───┤ "
after=" ├───"
prompt="┌$before$time$after"
colored_prompt="$Cyan┌$before$ICyan$time$Cyan$after"
if [[ $status != "nogit" ]]; then
prompt="$prompt$before"
colored_prompt="$colored_prompt$before"
if [[ $status == "ok" ]]; then
prompt="$prompt ✓ $branch"
colored_prompt="$colored_prompt $Green✓ $branch"
else
prompt="$prompt ✕ $branch"
colored_prompt="$colored_prompt $Red✕ $branch"
fi
prompt="$prompt$after"
colored_prompt="$colored_prompt$Cyan$after"
fi
prompt="$prompt$before$dir$after"
colored_prompt="$colored_prompt$before$Yellow$dir$Cyan$after"
length=$(echo $prompt | wc -m)
let fill_count=$COLUMNS-$length
while [[ $fill_count -gt 0 ]]; do
prompt="$prompt─"
colored_prompt="$colored_prompt─"
let fill_count=$fill_count-1
done
echo -e $colored_prompt
echo -ne "└─▶ "
}
export PS1="\$(gitPrompt)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment