diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2012-06-21 01:16:25 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2012-06-21 01:16:25 +1200 |
commit | a01641c51c758a1f3c110846e8e1e7676cc8a3af (patch) | |
tree | d31e905a1bf2c2963f0aebce07a2f6318908cb33 /bash | |
parent | Decided I don't like auto formatting on by default (diff) | |
download | dotfiles-a01641c51c758a1f3c110846e8e1e7676cc8a3af.tar.gz dotfiles-a01641c51c758a1f3c110846e8e1e7676cc8a3af.zip |
Refactor prompt into one function
Diffstat (limited to 'bash')
-rw-r--r-- | bash/bashrc | 178 |
1 files changed, 90 insertions, 88 deletions
diff --git a/bash/bashrc b/bash/bashrc index 2cd1f905..349d1fdf 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -92,16 +92,19 @@ hash tput && COLORS=$(tput colors) # Save some color codes based on our colour space. if [[ $COLORS -ge 256 ]]; then COLOR_ROOT='\[\e[38;5;9m\]' + COLOR_SUDO='\[\e[38;5;11m\]' COLOR_USER='\[\e[38;5;10m\]' - COLOR_UNDO='\[\e[0m\]' + COLOR_NORM='\[\e[0m\]' elif [[ $COLORS -ge 8 ]]; then COLOR_ROOT='\[\e[1;31m\]' + COLOR_SUDO='\[\e[1;33m\]' COLOR_USER='\[\e[1;32m\]' - COLOR_UNDO='\[\e[0m\]' + COLOR_NORM='\[\e[0m\]' else COLOR_ROOT= + COLOR_SUDO= COLOR_USER= - COLOR_UNDO= + COLOR_NORM= fi # Reset options for ls and grep. @@ -131,91 +134,6 @@ if grep --help | grep -- --exclude-dir &>/dev/null; then done fi -# Function to display branch of a Git repository. -function prompt_git { - git branch &>/dev/null || return 1 - HEAD="$(git symbolic-ref HEAD 2>/dev/null)" - BRANCH="${HEAD##*/}" - [[ -n "$(git status 2>/dev/null | \ - grep -F 'working directory clean')" ]] || STATUS="!" - echo -n "(git:${BRANCH:-unknown}${STATUS})" - return $? -} - -# Function to display branch of an SVN working copy. -function prompt_svn { - svn info &>/dev/null || return 1 - URL="$(svn info 2>/dev/null | \ - awk -F': ' '$1 == "URL" {print $2}')" - ROOT="$(svn info 2>/dev/null | \ - awk -F': ' '$1 == "Repository Root" {print $2}')" - BRANCH=${URL/$ROOT} - BRANCH=${BRANCH#/} - BRANCH=${BRANCH#branches/} - BRANCH=${BRANCH%%/*} - [[ -n "$(svn status 2>/dev/null)" ]] && STATUS="!" - echo -n "(svn:${BRANCH:-unknown}${STATUS})" - return $? -} - -# Function to display branch of a Mercurial repository. -function prompt_hg { - hg branch &>/dev/null || return 1 - BRANCH="$(hg branch 2>/dev/null)" - [[ -n "$(hg status 2>/dev/null)" ]] && STATUS="!" - echo -n "(hg:${BRANCH:-unknown}${STATUS})" - return $? -} - -# Function that calls each of the above. -function prompt_vcs { - prompt_git || prompt_svn || prompt_hg - return $? -} - -# Function to return the number of backgrounded jobs. -function prompt_jobs { - [[ -n "$(jobs)" ]] && echo -n "{$(jobs | wc -l | sed 's/ //g')}" - return $? -} - -# Function turns off my full-featured prompt for a minimal one. -function prompt_off { - unset PROMPT_COMMAND - PS1='\$ ' - return 0 -} - -# Function turns on my full-featured prompt, run by default. -function prompt_on { - - # Add history entries immediately, not on exit. - PROMPT_COMMAND='history -a' - - # Uncolored bits of my prompt, we'll color them if appropriate shortly. - PS1='[\u@\h:\w]$(prompt_vcs)$(prompt_jobs)\$' - - # Change prompt color depending on whether I'm root or not. - if [[ $EUID -eq 0 ]]; then - PS1=${COLOR_ROOT}${PS1}${COLOR_UNDO} - else - PS1=${COLOR_USER}${PS1}${COLOR_UNDO} - fi - - # Add space separator to end of prompt. - PS1="${PS1} " - - # Set window titles in various terminals. - case "$TERM" in - screen*) PS1='\[\ek\h\e\\\]'${PS1};; - xterm*) PS1='\[\e]0;\h\a\]'${PS1};; - esac - - # Done. - return 0 -} -prompt_on - # Alias ls and grep with the options we've collected. alias ls="ls ${LS_OPTS}" alias grep="grep ${GREP_OPTS}" @@ -229,3 +147,87 @@ alias sl='ls' # I actually use ed now and then. Go figure. alias ed='ed -p "ed> "' +# Decide on color for prompt. +if [[ $EUID -eq 0 ]]; then + COLOR_PROMPT=${COLOR_ROOT} +elif [[ -n $SUDO_USER ]]; then + COLOR_PROMPT=${COLOR_SUDO} +else + COLOR_PROMPT=${COLOR_USER} +fi + +# Frontend to controlling prompt. +function prompt { + case "$1" in + + # Turn complex coloured prompt on. + on) + PROMPT_COMMAND='history -a' + PS1='[\u@\h:\w]$(prompt return)$(prompt vcs)$(prompt jobs)\$' + PS1="${COLOR_PROMPT}${PS1}${COLOR_NORM} " + case "$TERM" in + screen*) PS1='\[\ek\h\e\\\]'${PS1};; + xterm*) PS1='\[\e]0;\h\a\]'${PS1};; + esac + ;; + + # Revert to simple inexpensive prompt. + off) + PROMPT_COMMAND= + PS1='\$ ' + ;; + + # Git prompt function. + git) + git branch &>/dev/null || return 1 + HEAD="$(git symbolic-ref HEAD 2>/dev/null)" + BRANCH="${HEAD##*/}" + [[ -n "$(git status 2>/dev/null | \ + grep -F 'working directory clean')" ]] || STATUS="!" + printf '(git:%s)' "${BRANCH:-unknown}${STATUS}" + ;; + + # Mercurial prompt function. + hg) + hg branch &>/dev/null || return 1 + BRANCH="$(hg branch 2>/dev/null)" + [[ -n "$(hg status 2>/dev/null)" ]] && STATUS="!" + printf '(hg:%s)' "${BRANCH:-unknown}${STATUS}" + ;; + + # Subversion prompt function. + svn) + svn info &>/dev/null || return 1 + URL="$(svn info 2>/dev/null | \ + awk -F': ' '$1 == "URL" {print $2}')" + ROOT="$(svn info 2>/dev/null | \ + awk -F': ' '$1 == "Repository Root" {print $2}')" + BRANCH=${URL/$ROOT} + BRANCH=${BRANCH#/} + BRANCH=${BRANCH#branches/} + BRANCH=${BRANCH%%/*} + [[ -n "$(svn status 2>/dev/null)" ]] && STATUS="!" + printf '(svn:%s)' "${BRANCH:-unknown}${STATUS}" + ;; + + # VCS wrapper prompt function. + vcs) + prompt_git || prompt_svn || prompt_hg + ;; + + # Return status prompt function. + return) + RETURN=$? + [[ $RETURN -ne 0 ]] && printf '<%d>' ${RETURN} + ;; + + # Job count prompt function. + jobs) + [[ -n "$(jobs)" ]] && printf '{%d}' $(jobs | wc -l | sed 's/ //g') + ;; + esac +} + +# Start with full-fledged prompt. +prompt on + |