Skip to content

Instantly share code, notes, and snippets.

@indirect
Last active December 20, 2015 14:49
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 indirect/6149602 to your computer and use it in GitHub Desktop.
Save indirect/6149602 to your computer and use it in GitHub Desktop.
Bash prompt with exit status, dotfiles git status, git status, and job status.
# Using this file, you prompt looks something like this:
# <exit status> <username color coded with dotfiles git status> <cwd> <git branch or rev-name> <job status> $
# 💙 andre ~/src/bundler/bundler v1.3.0.pre.2~13 👥 $
BLUE='\001\033[0;34m\002'
CYAN='\001\033[0;36m\002'
GREEN='\001\033[0;32m\002'
YELLOW='\001\033[0;33m\002'
RESET='\001\033[0m\002'
function status_color {
if [[ "$1" != *"nothing to commit"* ]]; then
printf $YELLOW
elif [[ "$1" == *"Your branch is ahead of"* ]]; then
printf $CYAN
else
printf $GREEN
fi
}
function exit_status {
if [[ $? -eq 0 ]]; then
printf "💙 "
else
printf "💔 "
fi
}
function dotfiles_status_colorize {
local dotfiles="$HOME/src/indirect/dotfiles"
status_color "$(git --git-dir=$dotfiles/.git --work-tree=$dotfiles status 2> /dev/null)"
printf $1
printf $RESET
}
function git_status {
# Check the status and bail if git failed
local status=$(git status 2> /dev/null) exited=$?
if [[ $exited -ne 0 ]]; then return 1; fi
# Get a human-friendly reference for this commit
local name=$(git symbolic-ref -q HEAD)
if [[ -n "$name" ]]; then
name="${b#refs/heads/}"
else
name=$(git name-rev --name-only --no-undefined --always HEAD)
name="${name#tags/}"
name="${name#remotes/}"
fi
status_color $status
printf "$name "
printf $RESET
}
function job_status {
[[ $1 -gt 0 ]] && printf "👥 "
}
# Set the prompt to "~/Sites/monkeys master $ "
export PS1="\$(exit_status) \$(dotfiles_status_colorize \u) $BLUE\w \$(git_status)\$(job_status \j)$BLUE\$$RESET "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment