2012-10-12 08:53:13 +00:00
|
|
|
# Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# User color
|
|
|
|
case $(id -u) in
|
|
|
|
0) USER_COLOR=$RED ;; # root
|
|
|
|
*) USER_COLOR=$GREEN ;;
|
|
|
|
esac
|
2012-10-12 08:53:13 +00:00
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# Some kind of optimization - check if Git installed only on config load
|
2012-10-12 08:53:13 +00:00
|
|
|
PS1_GIT_BIN=$(which git 2>/dev/null)
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
function prompt_command() {
|
2012-10-12 08:53:13 +00:00
|
|
|
local PS1_GIT=
|
|
|
|
local GIT_BRANCH=
|
|
|
|
local GIT_DIRTY=
|
2012-10-22 09:10:13 +00:00
|
|
|
local PWDNAME="$PWD"
|
2012-10-12 08:53:13 +00:00
|
|
|
|
2013-02-04 10:27:13 +00:00
|
|
|
# Local or SSH session?
|
|
|
|
local remote=
|
|
|
|
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
|
|
|
|
|
2012-10-12 08:53:13 +00:00
|
|
|
# Beautify working directory name
|
2012-10-22 09:10:13 +00:00
|
|
|
if [ "$HOME" == "$PWD" ]; then
|
2012-10-12 08:53:13 +00:00
|
|
|
PWDNAME="~"
|
2012-10-22 09:10:13 +00:00
|
|
|
elif [ "$HOME" == "${PWD:0:${#HOME}}" ]; then
|
2012-10-12 08:53:13 +00:00
|
|
|
PWDNAME="~${PWD:${#HOME}}"
|
|
|
|
fi
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# Parse Git status and get Git variables
|
2012-10-22 09:10:13 +00:00
|
|
|
if [[ ! -z "$PS1_GIT_BIN" ]]; then
|
2012-12-12 12:52:29 +00:00
|
|
|
# Check if we are in Git repo
|
2012-10-22 09:10:13 +00:00
|
|
|
local CUR_DIR="$PWD"
|
2012-12-12 12:52:29 +00:00
|
|
|
while [[ ! -d "$CUR_DIR/.git" ]] && [[ ! "$CUR_DIR" == "/" ]] && [[ ! "$CUR_DIR" == "~" ]] && [[ ! "$CUR_DIR" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
|
|
|
|
if [[ -d "$CUR_DIR/.git" ]]; then
|
|
|
|
# Get Git branch
|
2012-11-01 11:28:32 +00:00
|
|
|
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
|
|
|
|
if [[ ! -z "$GIT_BRANCH" ]]; then
|
|
|
|
GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# Get Git status
|
2012-11-01 11:28:32 +00:00
|
|
|
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
|
|
|
|
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
|
2012-10-12 08:53:13 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# Build B&W prompt for Git
|
|
|
|
[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #$GIT_BRANCH"
|
2012-10-12 08:53:13 +00:00
|
|
|
|
|
|
|
# Calculate prompt length
|
2013-02-04 10:27:13 +00:00
|
|
|
local host_length=0
|
|
|
|
[ -n "$remote" ] && host_length=$((${#HOSTNAME}+1))
|
|
|
|
local PS1_length=$((${#USER}+${#PWDNAME}+${#PS1_GIT}+$host_length+2))
|
2012-10-12 08:53:13 +00:00
|
|
|
local FILL=
|
|
|
|
|
2012-11-01 11:28:32 +00:00
|
|
|
# If length is greater, than terminal width
|
2012-10-12 08:53:13 +00:00
|
|
|
if [[ $PS1_length -gt $COLUMNS ]]; then
|
2012-11-01 11:28:32 +00:00
|
|
|
# Strip working directory name
|
2012-10-12 08:53:13 +00:00
|
|
|
PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}"
|
|
|
|
else
|
2012-12-12 12:52:29 +00:00
|
|
|
# Calculate fillsize
|
2012-10-12 08:53:13 +00:00
|
|
|
local fillsize=$(($COLUMNS-$PS1_length))
|
2012-12-12 12:52:29 +00:00
|
|
|
FILL=$GRAY
|
|
|
|
while [[ $fillsize -gt 0 ]]; do FILL=$FILL"─"; fillsize=$(($fillsize-1)); done
|
|
|
|
FILL=$FILL$NOCOLOR
|
2012-10-12 08:53:13 +00:00
|
|
|
fi
|
|
|
|
|
2012-12-12 12:52:29 +00:00
|
|
|
# Git status for prompt
|
|
|
|
if [ ! -z "$GIT_BRANCH" ]; then
|
|
|
|
local BRANCH_COLOR=$GREEN
|
|
|
|
[ ! -z $GIT_DIRTY ] && BRANCH_COLOR=$RED
|
|
|
|
PS1_GIT=" #$BRANCH_COLOR$GIT_BRANCH$NOCOLOR"
|
2012-10-12 08:53:13 +00:00
|
|
|
fi
|
|
|
|
|
2012-11-01 11:28:32 +00:00
|
|
|
# Set new color prompt
|
2013-02-04 10:27:13 +00:00
|
|
|
local host_prompt=
|
|
|
|
[ -n "$remote" ] && host_prompt="@$YELLOW$HOSTNAME$NOCOLOR"
|
|
|
|
PS1="$USER_COLOR$USER$NOCOLOR$host_prompt:$WHITE$PWDNAME$NOCOLOR$PS1_GIT $FILL\n→ "
|
2012-10-12 08:53:13 +00:00
|
|
|
|
2012-11-01 11:28:32 +00:00
|
|
|
# Terminal title
|
2012-12-12 12:52:29 +00:00
|
|
|
local TITLE=$(basename $PWDNAME)
|
|
|
|
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="$TITLE \xE2\x80\x94 $HOSTNAME"
|
|
|
|
echo -ne "\033]0;$TITLE"; echo -ne "\007"
|
2012-10-12 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Set prompt command (title update and color prompt)
|
|
|
|
PROMPT_COMMAND=prompt_command
|
|
|
|
# Set new B&W prompt (will be overwritten in `prompt_command` later with color prompt)
|
|
|
|
PS1='\u@\h:\w\$ '
|