Skip to content

Instantly share code, notes, and snippets.

@caruccio
Last active August 26, 2019 17:52
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 caruccio/6445181 to your computer and use it in GitHub Desktop.
Save caruccio/6445181 to your computer and use it in GitHub Desktop.
My bash PS1
# Based on code from http://andrewray.me/bash-prompt-builder/index.html
DELTA_CHAR="δ"
CONFLICT_CHAR="☓"
NOBRANCH_TEXT="no-branch"
REBASE_TEXT="✂ ʀebase"
# Colors for prompt
COLOR_RED=$(tput sgr0 && tput setaf 1)
COLOR_GREEN=$(tput sgr0 && tput setaf 2)
COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
COLOR_BLUE=$(tput sgr0 && tput setaf 4)
COLOR_MAGENTA=$(tput sgr0 && tput setaf 5)
COLOR_CYAN=$(tput sgr0 && tput setaf 6)
COLOR_GRAY=$(tput sgr0 && tput setaf 7)
COLOR_WHITE=$(tput sgr0 && tput setaf 7 && tput bold)
COLOR_LIGHTRED=$(tput sgr0 && tput setaf 1 && tput bold)
COLOR_LIGHTGREEN=$(tput sgr0 && tput setaf 2 && tput bold)
COLOR_LIGHTYELLOW=$(tput sgr0 && tput setaf 3 && tput bold)
COLOR_LIGHTBLUE=$(tput sgr0 && tput setaf 4 && tput bold)
COLOR_LIGHTMAGENTA=$(tput sgr0 && tput setaf 5 && tput bold)
COLOR_LIGHTCYAN=$(tput sgr0 && tput setaf 6 && tput bold)
COLOR_RESET=$(tput sgr0)
[ $UID -eq 0 ] && COLOR_USER="$COLOR_RED" || COLOR_USER="$COLOR_BLUE"
function ps1_function()
{
# From bash(1) man, section PROMPTING:
# \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
# \] end a sequence of non-printing characters
PS1="\[$COLOR_USER\]\u\[$COLOR_WHITE\]@\[$COLOR_LIGHTBLUE\]\h \[$COLOR_GREEN\]\t \[$COLOR_LIGHTGREEN\]\w"
# set -x
local git_dir=`git rev-parse --show-toplevel 2> /dev/null`
if [ -z "$git_dir" ]; then
PS1+=" \[$COLOR_WHITE\]\$\[$COLOR_RESET\] "
return
fi
PS1+=" \[$COLOR_WHITE\]("
## Branch
local git_branch=$(git symbolic-ref HEAD 2> /dev/null)
if [ -z "$git_branch" ]; then
if [ -d "$git_dir/.git/rebase-apply" -o -d "$git_dir/.git/rebase-merge" ]; then
PS1+="\[$COLOR_LIGHT_CYAN\]$REBASE_TEXT"
else
PS1+="\[$COLOR_LIGHTRED\]$NOBRANCH_TEXT"
fi
else
git_branch=${git_branch#refs/heads/}
[ "$git_branch" == 'master' ] && PS1+="\[$COLOR_LIGHTYELLOW\]" || PS1+="\[$COLOR_RED\]"
local rev_no=$(git rev-parse HEAD 2>/dev/null)
PS1+="$git_branch\[$COLOR_WHITE\]:\[$COLOR_GRAY\]${rev_no:0:6}"
fi
## Flags
# local uncommited changes
local git_status="`git status --short -uno -b`"
echo "$git_status" | grep -qv '^#' && PS1+=" \[$COLOR_LIGHTRED\]$DELTA_CHAR"
# number os commit ahead remote
local ahead=`echo "$git_status" | sed -ne 's/^##.*\[ahead \([0-9]\+\)\]$/\1/p'`
if [ -n "$ahead" ]; then
PS1+=" \[$COLOR_CYAN\]+$ahead"
fi
# number of conflicts
local con_no="`git ls-files --unmerged|wc -l`"
if [ "$con_no" -gt 0 ]; then
PS1+=" \[$COLOR_RED\]$CONFLICT_CHAR$con_no"
fi
PS1+="\[$COLOR_WHITE\]) \$\[$COLOR_RESET\] "
if [ -n "$VIRTUAL_ENV" ]; then
export PS1="(${VIRTUAL_ENV##*/}) $PS1"
fi
# set +x
}
# Execute on every prompt refresh
PROMPT_COMMAND=ps1_function
# fallback to default
#export PS1="\[$COLOR_USER\]\u\[$COLOR_WHITE\] \[$COLOR_LIGHTGREEN\]\w \[$COLOR_WHITE\]\$\[$COLOR_RESET\] "
@caruccio
Copy link
Author

caruccio commented Sep 5, 2013

Atualizado, a pedidos.

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