Last active
October 5, 2018 14:54
-
-
Save cmccandless/08327e4559ef53f4e3887849685c0057 to your computer and use it in GitHub Desktop.
Bash prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Custom bash prompt | |
# Usage: add `source set_prompt.sh` to your .bashrc | |
# Settings: 0 or 1 | |
DEBUG=0 | |
USE_16M_COLOR=1 | |
# lowercase: decimal | |
# uppercase: hexadecimal | |
# rrr;ggg;bbb or #RGB or #RRGGBB or $VARIABLE | |
COLOR_PREFIX="#fc0" | |
COLOR_USER="#0c6" | |
COLOR_HOST=$COLOR_USER | |
COLOR_CWD="#077" | |
COLOR_REPO="#0ff" | |
# No changes to commit | |
COLOR_BRANCH_CLEAN="#0f0" | |
# All changes are staged | |
COLOR_BRANCH_STAGED="#ff0" | |
# Any modified or new files that are not staged | |
COLOR_BRANCH_DIRTY="#f00" | |
# Used for '$' at end of prompt | |
COLOR_END="#fff" | |
BOLD_USER=1 | |
BOLD_HOST=1 | |
BOLD_CWD=0 | |
BOLD_REPO=0 | |
BOLD_BRANCH=0 | |
BOLD_END=0 | |
unset PS1_SUFFIX | |
dbg() | |
{ | |
if [ $DEBUG -eq 1 ]; then | |
>&2 echo $@ | |
fi | |
} | |
dec2hex() | |
{ | |
echo "obase=16; $1" | bc | |
} | |
octal_digit() | |
{ | |
x=$1 | |
y=7 | |
printf '%d' "$(( x>7?7:x ))" | |
} | |
color_from_hex(){ | |
hex=${1#"#"} | |
if [[ ( "${#hex}" == 3 ) ]]; then | |
r=$(printf '0x%0.1s%0.1s' ${hex} ${hex}) | |
g=$(printf '0x%0.1s%0.1s' ${hex#?} ${hex#?}) | |
b=$(printf '0x%0.1s%0.1s' ${hex#??} ${hex#??}) | |
else | |
r=$(printf '0x%0.2s' "$hex") | |
g=$(printf '0x%0.2s' ${hex#??}) | |
b=$(printf '0x%0.2s' ${hex#????}) | |
fi | |
if [ $USE_16M_COLOR -eq 0 ]; then | |
index="$(( (r<75?0:(r-35)/40)*36 + | |
(g<75?0:(g-35)/40)*6 + | |
(b<75?0:(b-35)/40) + 16 ))" | |
index=$( | |
for (( i=0; i<${#index}; i++)); do octal_digit ${index:$i:1}; done | |
) | |
else | |
index=$(printf "%03d;%03d;%03d" $r $g $b) | |
fi | |
dbg "#$hex->$index" | |
echo $index | |
} | |
reset_fmt(){ | |
printf '\[\e[0m\]' | |
} | |
bold() | |
{ | |
printf "%s%s%s" '\[\e[1m\]' $1 $(reset_fmt) | |
} | |
color(){ | |
case "$1" in | |
"#"*) COLOR=$(color_from_hex $1);; | |
*";"*";"*) COLOR=$1:: | |
esac | |
TEXT=$2 | |
if [ $USE_16M_COLOR -eq 0 ]; then | |
printf '\[\e[38;5;%dm\]%s%s' $COLOR "$TEXT" $(reset_fmt) | |
else | |
printf '\[\e[038;2;%sm\]%s%s' $COLOR "$TEXT" $(reset_fmt) | |
fi | |
} | |
parse_git_info() | |
{ | |
REPO=$(git config --get remote.origin.url || git rev-parse --show-toplevel 2> /dev/null) | |
if [ $? -eq 0 ]; then | |
REPO="$(basename ${REPO%.git})" | |
BRANCH="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')" | |
if [[ "$REPO" ]]; then | |
REPO="$(color $COLOR_REPO $REPO)" | |
if [ $BOLD_REPO -eq 1 ]; then | |
REPO="$(bold $REPO)" | |
fi | |
if [ -z "$BRANCH" ]; then | |
BRANCH="$(git status 2> /dev/null | grep -oP '(?<=On branch ).*')" | |
else | |
BRANCH="${BRANCH//\(}" | |
BRANCH="${BRANCH//\)}" | |
fi | |
if git status | grep -E 'Changes not staged|Untracked files' > /dev/null; then | |
BRANCH="$(color $COLOR_BRANCH_DIRTY "$BRANCH*")" | |
elif git status | grep 'Changes to be committed' > /dev/null; then | |
BRANCH=$(color $COLOR_BRANCH_STAGED "$BRANCH*") | |
else | |
BRANCH="$(color $COLOR_BRANCH_CLEAN $BRANCH)" | |
fi | |
if [ $BOLD_BRANCH -eq 1 ]; then | |
GIT_INFO+="$(bold $BRANCH)" | |
fi | |
echo "($REPO: $BRANCH)\n" | |
fi | |
fi | |
} | |
set_ps1() | |
{ | |
PS1='' | |
if [ ! -z "${PS1_PREFIX+x}" ]; then | |
PS1+="$(color $COLOR_PREFIX "$PS1_PREFIX ")" | |
fi | |
PS1+="$(parse_git_info)" | |
PS1+="${debian_chroot:+($debian_chroot)}" | |
if [ $BOLD_USER -eq 1 ]; then | |
PS1+="$(bold $(color $COLOR_USER \\u))" | |
else | |
PS1+="$(color $COLOR_USER \\u)" | |
fi | |
PS1+="@" | |
if [ $BOLD_HOST -eq 1 ]; then | |
PS1+="$(bold $(color $COLOR_HOST \\h))" | |
else | |
PS1+="$(color $COLOR_HOST \\h)" | |
fi | |
PS1+=":" | |
if [ $BOLD_CWD -eq 1 ]; then | |
PS1+="$(bold $(color $COLOR_CWD \\w))" | |
else | |
PS1+="$(color $COLOR_CWD \\w)" | |
fi | |
if [ $BOLD_END -eq 1 ]; then | |
PS1+="$(bold $(color $COLOR_END \\$))" | |
else | |
PS1+="$(color $COLOR_END \\$)" | |
fi | |
PS1+=" " | |
export PS1 | |
export PS1_SUFFIX=${PS1//$PS1_PREFIX /} | |
} | |
if [[ ! "$PROMPT_COMMAND" = *"set_ps1"* ]]; then | |
PROMPT_COMMAND+='set_ps1;' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment