# jack@host [~/dotfiles] master [+!?$] # ⑆ # TODO: Add screenshot of prompt # Terminal prefs: import Solarized theme and disable bright colors for bold text # References: # terminal color: http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim # color output: http://tldp.org/LDP/abs/html/colorizing.html # bash prompt escape sequences: # https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html # http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then export TERM='gnome-256color'; elif infocmp xterm-256color >/dev/null 2>&1; then export TERM='xterm-256color'; fi; if [ -n "$ZSH_VERSION" ]; then setopt PROMPT_SUBST export RPROMPT= fi; is_in_git_repository() { [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) = 0 ] } prompt_git_branch() { local branchName="" if is_in_git_repository ; then # get the short symbolic ref # if HEAD isn't a symbolic ref, get the short SHA # otherwise, just give up branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \ git rev-parse --short HEAD 2> /dev/null || \ printf "(unknown)")" echo $branchName fi } prompt_git_status() { local s="" local root="" if is_in_git_repository ; then # 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 # ensure index is up to date git update-index --really-refresh -q &>/dev/null # get root of repository root=$(git rev-parse --show-toplevel) # check for uncommitted changes in the index if ! $(git diff --quiet --ignore-submodules --cached); then s="$s+"; fi # check for unstaged changes if ! $(git diff-files --quiet --ignore-submodules --); then s="$s!"; fi # check for untracked files if [ -n "$(git ls-files --others --exclude-standard $root)" ]; then s="$s?"; fi # check for stashed files if $(git rev-parse --verify refs/stash &>/dev/null); then s="$s$"; fi fi [ -n "$s" ] && s="[$s]" echo $s fi } # Define colors used in prompt # TODO: replace associative array to be compatible with Bash versions < 4.0 declare -A colors declare -A colors_escaped # The following colors assume use of a terminal with a solarized color palette # like gnome-terminal. If not using a solarized terminal palette, use the colors # from http://git.io/solarized-colors. if tput setaf 1 &> /dev/null; then tput sgr0; # reset colors colors[bold]=$(tput bold); colors[reset]=$(tput sgr0); colors[black]=$(tput setaf 0); colors[gray]=$(tput setaf 12); colors[white]=$(tput setaf 7); colors[red]=$(tput setaf 1); colors[orange]=$(tput setaf 9); colors[yellow]=$(tput setaf 3); colors[green]=$(tput setaf 2); colors[cyan]=$(tput setaf 6); colors[blue]=$(tput setaf 4); colors[purple]=$(tput setaf 13); else colors[bold]=''; colors[reset]="\e[0m"; colors[black]="\e[30m"; colors[gray]="\e[37m"; colors[white]="\e[37m"; colors[red]="\e[31m"; colors[orange]="\e[91m"; colors[yellow]="\e[33m"; colors[green]="\e[32m"; colors[cyan]="\e[36m"; colors[blue]="\e[34m"; colors[purple]="\e[95m"; fi; # Set variables for special prompt characters. if [ -n "$BASH_VERSION" ]; then echo $username; username="\u"; hostname="\h"; workdir="\w"; escapeLeft="\["; escapeRight="\]"; elif [ -n "$ZSH_VERSION" ]; then echo $username; username="%n"; hostname="%m"; workdir="%~"; escapeLeft="%{"; escapeRight="%}"; fi; # Wrap color sequences in escaped brackets so shell doesn't count them towards # the character length of the prompt. See a detailed explanation here: # https://unix.stackexchange.com/a/124409. generate_escaped_colors() { if [ -n "$ZSH_VERSION" ]; then emulate -L ksh; fi for i in "${!colors[@]}"; do colors_escaped[$i]="$escapeLeft${colors[$i]}$escapeRight" done } generate_escaped_colors # Highlight the username when logged in as root. if [[ "${USER}" == "root" ]]; then userStyle="${colors_escaped[red]}"; else userStyle="${colors_escaped[orange]}"; fi; # Highlight the hostname when connected via SSH. if [[ "${SSH_TTY}" ]]; then hostStyle="${colors_escaped[bold]}${colors_escaped[red]}"; else hostStyle="${colors_escaped[yellow]}"; fi; if [ -n "$ZSH_VERSION" ]; then setopt PROMPT_SUBST; fi; # Set the terminal title and prompt. PS1="${colors_escaped[reset]}" # PS1+="\[\e]0;\w\007\]"; # terminal title (set to working directory full path) PS1+="${userStyle}$username${colors_escaped[reset]}"; # username PS1+="@"; PS1+="${hostStyle}$hostname${colors_escaped[reset]}"; # host PS1+=" "; PS1+="${colors_escaped[green]}[$workdir]"; # working directory full path PS1+=" "; PS1+="${colors_escaped[cyan]}" PS1+='$(prompt_git_branch)'; # git branch PS1+="${colors_escaped[blue]} " PS1+='$(prompt_git_status)'; # git status PS1+=$'\n'; PS1+="${colors_escaped[white]}" PS1+=$'\u2446' # prompt icon (⑆) PS1+=" "; PS1+="${colors_escaped[reset]}"; export PS1; PS2="${colors_escaped[yellow]}→ ${colors_escaped[reset]}"; export PS2; unset colors_escaped; unset userStyle; unset hostStyle; unset username; unset hostname; unset workdir; unset escapeRight; unset escapeLeft;