Skip to content

Instantly share code, notes, and snippets.

@phantamanta44
Last active February 16, 2018 01:50
Show Gist options
  • Save phantamanta44/1846be199690f4b136effc88446451bf to your computer and use it in GitHub Desktop.
Save phantamanta44/1846be199690f4b136effc88446451bf to your computer and use it in GitHub Desktop.
#
# Cool bash prompt
# Modified verison of a dotfile by Artem Sapegin, sapegin.me
# ...which was originally inspired by: https://github.com/sindresorhus/pure
#
# You'll want to add the following to your bashrc to make this work properly:
#
# RED="$(tput setaf 1)"
# GREEN="$(tput setaf 2)"
# YELLOW="$(tput setaf 3)"
# BLUE="$(tput setaf 4)"
# MAGENTA="$(tput setaf 5)"
# CYAN="$(tput setaf 6)"
# WHITE="$(tput setaf 7)"
# GRAY="$(tput setaf 8)"
# BOLD="$(tput bold)"
# UNDERLINE="$(tput sgr 0 1)"
# INVERT="$(tput sgr 1 0)"
# NOCOLOR="$(tput sgr0)"
# local_username=$USER
#
# User color
case $(id -u) in
0) user_color="\[$RED\]" ;; # root
*) user_color="\[$GREEN\]" ;;
esac
# Symbols
prompt_symbol="ÔØ»"
prompt_venv_symbol="Ôÿé "
function prompt_command() {
# Local or SSH session?
local remote=
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && remote=1
# 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/}"
# Working tree status (red when dirty)
local dirty=
# Modified files
git diff --no-ext-diff --quiet --exit-code --ignore-submodules 2>/dev/null || dirty=1
# Untracked files
[ -z "$dirty" ] && test -n "$(git status --porcelain)" && dirty=1
# Format Git info
if [ -n "$dirty" ]; then
git_prompt=" \[$RED\]$branch\[$NOCOLOR\]"
else
git_prompt=" \[$GREEN\]$branch\[$NOCOLOR\]"
fi
fi
# Virtualenv
local venv_prompt=
if [ -n "$VIRTUAL_ENV" ]; then
venv_prompt="\[$BLUE\]$prompt_venv_symbol$(basename $VIRTUAL_ENV)\[$NOCOLOR\]"
fi
# Only show username if not default
local user_prompt=
[ "$USER" != "$local_username" ] && user_prompt="$user_color$USER\[$NOCOLOR\] "
# Show hostname inside SSH session
local host_prompt=
[ -n "$remote" ] && host_prompt="@\[$YELLOW\]$HOSTNAME\[$NOCOLOR\]"
# Format prompt
first_line=
[ -n "$user_prompt" ] || [ -n "$host_prompt" ] || [ -n "$venv_prompt" ] && first_line="$user_prompt$host_prompt\[$NOCOLOR\]$venv_prompt\n"
# Text (commands) inside \[...\] does not impact line length calculation which fixes stange bug when looking through the history
# $? is a status of last command, should be processed every time prompt prints
second_line="\W$git_prompt \`if [ \$? = 0 ]; then echo \[\$CYAN\]; else echo \[\$RED\]; fi\`\[\$prompt_symbol\]\[\$NOCOLOR\] "
PS1="\n$first_line$second_line"
# Multiline command
PS2="\[$CYAN\]$prompt_symbol\[$NOCOLOR\] "
# Terminal title
local title="$(basename "$PWD")"
[ -n "$remote" ] && title="$title \xE2\x80\x94 $HOSTNAME"
echo -ne "\033]0;$title"; echo -ne "\007"
}
# Show awesome prompt only if Git is istalled
command -v git >/dev/null 2>&1 && PROMPT_COMMAND=prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment