Skip to content

Instantly share code, notes, and snippets.

@GabrielAnca
Created May 28, 2017 15:21
Show Gist options
  • Save GabrielAnca/bab63401376c43019acaee9581d6f9a2 to your computer and use it in GitHub Desktop.
Save GabrielAnca/bab63401376c43019acaee9581d6f9a2 to your computer and use it in GitHub Desktop.
Personalized PS1 with git status detection and warning on errors
#!/bin/bash
# Bash colors
DEFAULT_PS1_COLOR="\[\033[01;38;5;33m\]"
PS1_DATE="\[\033[01;38;5;242m\]\t$DEFAULT_PS1_COLOR"
PS1_HOST_STRING="\[\033[01;38;5;39m\]\u@\h$DEFAULT_PS1_COLOR"
PS1_PATH="$DEFAULT_PS1_COLOR[\w]$DEFAULT_PS1_COLOR"
PS1_EXEC_LINE="$DEFAULT_PS1_COLOR\$ $PS1_COLOR_RESET"
PS1_COLOR_RESET="\[\033[00;38;5;255m\]"
function get_last_command_failed_ps1()
{
# This has to be the first command, otherwise we lose the exit code
EXIT_CODE="$?"
if [ "$EXIT_CODE" -ne "0" ] && [ "$EXIT_CODE" -ne 130 ]; then
PS1_FAILURE_COLOR="\\033[01;38;5;220m\\033[48;5;196m"
PS1_COLOR_RESET="\\033[00;38;5;255m"
echo -ne " ${PS1_FAILURE_COLOR} ! ${PS1_COLOR_RESET} "
fi
}
function get_git_ps1()
{
PS1_GIT_CLEAN_COLOR="\\033[00;38;5;71m"
PS1_GIT_DIRTY_COLOR="\\033[01;38;5;202m"
PS1_COLOR_RESET="\\033[00;38;5;255m"
git branch > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
git status | grep "nothing to commit" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
# Clean repository - nothing to commit
echo -ne "$PS1_GIT_CLEAN_COLOR"$(__git_ps1 "(%s)")
else
# Changes to working tree
echo -ne "$PS1_GIT_DIRTY_COLOR"$(__git_ps1 "{%s}")
fi
echo -ne "$PS1_COLOR_RESET"
fi
}
# You might want to configure something in the $PS1_EXTRA variable before including this file.
# For example work related stuff that you don't want in your personal computer.
PS1='$(get_last_command_failed_ps1)'$PS1_HOST_STRING:\ $PS1_PATH\ '$(get_git_ps1)'$PS1_EXTRA\\n$PS1_EXEC_LINE$PS1_COLOR_RESET
@GabrielAnca
Copy link
Author

This is how it looks like:
ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment