dotfiles/bash_prompt

127 lines
3.7 KiB
Bash
Raw Normal View History

2016-10-14 21:59:34 +02:00
#!/usr/bin/env bash
2012-06-25 18:51:38 +02:00
2012-06-18 01:10:31 +02:00
########################################################################
2012-06-18 21:46:35 +02:00
# bash_prompt, from @mathiasbynens
2012-06-25 18:51:38 +02:00
# slightly modified
2012-06-18 21:46:35 +02:00
# https://github.com/mathiasbynens/dotfiles/blob/master/.bash_prompt
2012-06-18 01:10:31 +02:00
########################################################################
2012-06-18 21:46:35 +02:00
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
2015-08-08 23:32:07 +02:00
export TERM='gnome-256color';
2012-06-18 21:46:35 +02:00
elif infocmp xterm-256color >/dev/null 2>&1; then
2015-08-08 23:32:07 +02:00
export TERM='xterm-256color';
2014-09-01 10:58:26 +02:00
fi;
2012-06-18 01:10:31 +02:00
2014-09-01 10:58:26 +02:00
prompt_git() {
2015-08-08 23:32:07 +02:00
local s='';
local branchName='';
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Check if the current directory is in a Git repository.
2017-06-12 01:04:18 +02:00
if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ]; then
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Check for uncommitted changes in the index.
2017-09-10 14:46:58 +02:00
# shellcheck disable=SC2091
2015-08-08 23:32:07 +02:00
if ! $(git diff --quiet --ignore-submodules --cached); then
s+='+';
fi;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Check for unstaged changes.
2017-09-10 14:46:58 +02:00
# shellcheck disable=SC2091
2015-08-08 23:32:07 +02:00
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Check for stashed files.
2017-09-10 14:46:58 +02:00
# shellcheck disable=SC2091
2015-08-08 23:32:07 +02:00
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$';
fi;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
fi;
2014-09-01 10:58:26 +02:00
2015-08-08 23:32:07 +02:00
# Get the short symbolic ref.
# If HEAD isnt a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
2014-09-01 10:58:26 +02:00
2015-09-25 11:16:30 +02:00
[ -n "${s}" ] && s=" ${s}";
2014-09-01 10:58:26 +02:00
2015-09-25 11:16:30 +02:00
echo -e "${1}${branchName}${orange}${s}";
2015-08-08 23:32:07 +02:00
else
return;
fi;
2012-06-18 01:10:31 +02:00
}
2017-09-10 14:46:58 +02:00
# shellcheck disable=SC2034
2014-09-01 10:58:26 +02:00
if tput setaf 1 &> /dev/null; then
2015-08-08 23:32:07 +02:00
tput sgr0; # reset colors
bold=$(tput bold);
2016-11-09 20:09:13 +01:00
dim=$(tput setaf 8);
2015-08-08 23:32:07 +02:00
reset=$(tput sgr0);
2015-09-25 11:16:30 +02:00
# Tomorrow colors, taken from https://github.com/Slava/vim-colors-tomorrow/blob/master/colors/tomorrow.vim
black=$(tput setaf 236);
gray=$(tput setaf 242);
blue=$(tput setaf 109);
green=$(tput setaf 143);
2017-03-24 03:12:46 +01:00
cyan=$(tput setaf 43);
2015-09-25 11:16:30 +02:00
orange=$(tput setaf 173);
2015-08-08 23:32:07 +02:00
purple=$(tput setaf 125);
2015-09-25 11:16:30 +02:00
red=$(tput setaf 167);
magenta=$(tput setaf 139);
violet=$(tput setaf 139);
2015-08-08 23:32:07 +02:00
white=$(tput setaf 15);
yellow=$(tput setaf 136);
2014-09-01 10:58:26 +02:00
else
2015-08-08 23:32:07 +02:00
bold='';
reset="\e[0m";
black="\e[1;30m";
blue="\e[1;34m";
cyan="\e[1;36m";
green="\e[1;32m";
orange="\e[1;33m";
purple="\e[1;35m";
red="\e[1;31m";
violet="\e[1;35m";
white="\e[1;37m";
yellow="\e[1;33m";
2014-09-01 10:58:26 +02:00
fi;
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
2015-08-08 23:32:07 +02:00
userStyle="${red}";
2014-09-01 10:58:26 +02:00
else
2016-11-09 20:09:13 +01:00
userStyle="${gray}";
2014-09-01 10:58:26 +02:00
fi;
if [[ "${SSH_TTY}" ]]; then
2016-11-09 20:09:13 +01:00
PS1="\[\033]0;\W • \h\007\]"; # Set the terminal title
PS1+="\n";
PS1+="\[${userStyle}\]\u "; # username
PS1+="\[${orange}\]\h \[${reset}\]"; # host
2014-09-01 10:58:26 +02:00
else
2016-11-09 20:09:13 +01:00
PS1="\[\033]0;\W\007\]"; # Set the terminal title
PS1+="\n";
2014-09-01 10:58:26 +02:00
fi;
2016-10-14 21:59:34 +02:00
PS1+="\[${cyan}\]\w"; # working directory full path
2015-11-21 21:33:09 +01:00
PS1+="\$(prompt_git \" \[${gray}\]\")"; # Git repository details
2015-09-25 11:16:30 +02:00
PS1+="\n";
2016-11-09 20:09:13 +01:00
PS1+="\[${white}\]→ \[${reset}\]"; # `→` (and reset color)
2014-09-01 10:58:26 +02:00
export PS1;
2012-06-18 01:10:31 +02:00
2016-11-09 20:09:13 +01:00
PS2="\[${white}\]→ \[${reset}\]";
2015-08-08 23:32:07 +02:00
export PS2;