dotfiles/includes/bash_prompt.bash

66 lines
2.0 KiB
Bash
Raw Normal View History

2013-02-20 14:06:09 +00:00
# Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile & https://github.com/sindresorhus/pure
# Add to ~/.bashlocal user name you dont want to see in the prompt: `local_username="admin"`
2012-10-12 08:53:13 +00:00
# User color
case $(id -u) in
2013-02-20 14:06:09 +00:00
0) user_color="$RED" ;; # root
*) user_color="$GREEN" ;;
esac
2012-10-12 08:53:13 +00:00
2013-02-20 14:06:09 +00:00
# Prompt symbol
prompt_symbol=""
2012-10-12 08:53:13 +00:00
function prompt_command() {
2013-02-04 10:27:13 +00:00
# Local or SSH session?
local remote=
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
2013-02-20 14:06:09 +00:00
# Working directory name
local dir_name="$PWD"
2012-10-22 09:10:13 +00:00
if [ "$HOME" == "$PWD" ]; then
2013-02-20 14:06:09 +00:00
dir_name="~"
2012-10-22 09:10:13 +00:00
elif [ "$HOME" == "${PWD:0:${#HOME}}" ]; then
2013-02-20 14:06:09 +00:00
dir_name="~${PWD:${#HOME}}"
2012-10-12 08:53:13 +00:00
fi
2013-02-20 14:06:09 +00:00
# Git branch name and work tree status (only when we are inside Git working tree)
local git_prompt=
if [[ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]]; then
# Branch name
local branch="$(git symbolic-ref HEAD 2>/dev/null)"
branch="${branch##refs/heads/}"
2012-10-12 08:53:13 +00:00
2013-02-20 14:06:09 +00:00
# Working tree status (red when dirty)
local branch_color="$GREEN"
git diff --no-ext-diff --quiet --exit-code --ignore-submodules || branch_color="$RED"
2012-10-12 08:53:13 +00:00
2013-02-20 14:06:09 +00:00
# Format Git info
git_prompt=" #$branch_color$branch$NOCOLOR"
2012-10-12 08:53:13 +00:00
fi
2013-02-20 14:06:09 +00:00
# Only show username if not default
local user_prompt=
[ "$USER" != "$local_username" ] && user_prompt="$user_color$USER$NOCOLOR"
2012-10-12 08:53:13 +00:00
2013-02-20 14:06:09 +00:00
# Show hostname inside SSH session
2013-02-04 10:27:13 +00:00
local host_prompt=
2013-02-20 14:06:09 +00:00
[ -n "$remote" ] && host_prompt="@$YELLOW$HOSTNAME$NOCOLOR"
# Show delimiter if user or host visible
local login_delimiter=
[ -n "$user_prompt" ] || [ -n "$host_prompt" ] && login_delimiter=":"
# Format prompt
2013-02-21 14:45:15 +00:00
# Text (commands) inside \[...\] does not impact line length which fixes stange bug when looking through the history
2013-02-21 14:50:26 +00:00
PS1="\n$user_prompt$host_prompt$login_delimiter$WHITE$dir_name$NOCOLOR$git_prompt\n\[$CYAN\]$prompt_symbol\[$NOCOLOR\] "
2012-10-12 08:53:13 +00:00
# Terminal title
2013-02-20 14:06:09 +00:00
local title="$(basename $dir_name)"
[ -n "$remote" ] && title="$title \xE2\x80\x94 $HOSTNAME"
echo -ne "\033]0;$title"; echo -ne "\007"
2012-10-12 08:53:13 +00:00
}
2013-02-20 14:06:09 +00:00
# Show awesome prompt only if Git is istalled
command -v git >/dev/null 2>&1 && PROMPT_COMMAND=prompt_command