Git-prompt.sh

From HallCWiki
Revision as of 16:25, 3 January 2013 by Brads (Talk | contribs) (Git information in your bash prompt)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Media:Git-prompt.txt

#!/bin/bash
#  Source this file at the end of your ~/.bashrc to set a dynamic prompt
#
# Configure Colors:
COLOR_WHITE='\033[1;37m'
COLOR_LIGHTGRAY='033[0;37m'
COLOR_GRAY='\033[1;30m'
COLOR_BLACK='\033[0;30m'
COLOR_RED='\033[0;31m'
COLOR_LIGHTRED='\033[1;31m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTGREEN='\033[1;32m'
COLOR_BROWN='\033[0;33m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[0;34m'
COLOR_LIGHTBLUE='\033[1;34m'
COLOR_PURPLE='\033[0;35m'
COLOR_PINK='\033[1;35m'
COLOR_CYAN='\033[0;36m'
COLOR_LIGHTCYAN='\033[1;36m'
COLOR_DEFAULT='\033[0m'
COLOR_BGRED='\[\e[1;41m\]'

# Function to set prompt_command to.
function promptcmd () {
    local PATH="/bin:/usr/bin:/apps/bin"

    history -a
    local SSH_FLAG=0
    local TTY=$(/usr/bin/tty | /usr/bin/awk -F/dev/ '{print $2}')
    if [[ ${TTY} ]]; then
#        local SESS_SRC=$(/usr/bin/who | /bin/grep "$TTY "  | /usr/bin/awk '{print $6 }')
        local SESS_SRC=$(/usr/bin/who | /bin/grep "$TTY "  | /usr/bin/awk '{print $6 ; nextfile}')
    fi

    # Titlebar
    case ${TERM} in
        *term | rxvt* | screen* )
            local TITLEBAR='\[\033]0;\u@\h: { \w }  \007\]'
            ;;
        *       )
            local TITLEBAR=''
            ;;
    esac
    PS1="${TITLEBAR}"

    # Test for day change.
    if [ -z $DAY ] ; then
        export DAY=$(/bin/date +%A)
    else
        local today=$(/bin/date +%A)
        if [ "${DAY}" != "${today}" ]; then
            PS1="${PS1}\n\[${COLOR_GREEN}\]Day changed to $(date '+%A, %d %B %Y').\n"
            export DAY=$today
       fi
    fi

    # User
    if [ ${UID} -eq 0 ] ; then
        if [ "${USER}" == "${LOGNAME}" ]; then
            if [[ ${SUDO_USER} ]]; then
                PS1="${PS1}\[${COLOR_RED}\]\u"
            else
                PS1="${PS1}\[${COLOR_LIGHTRED}\]\u"
            fi
        else
            PS1="${PS1}\[${COLOR_YELLOW}\]\u"
        fi
    else
        if [ ${USER} == ${LOGNAME} ]; then
            PS1="${PS1}\[${COLOR_GREEN}\]\u"
        else
            PS1="${PS1}\[${COLOR_BROWN}\]\u"
        fi
    fi

#    # HTTP Proxy var configured or not
#    if [ -n "$http_proxy" ] ; then
#        PS1="${PS1}\[${COLOR_GREEN}\]@"
#    else
#        PS1="${PS1}\[${COLOR_LIGHTRED}\]@"
#    fi
     PS1="${PS1}@"

    # Host
    if [[ ${SSH_CLIENT} ]] || [[ ${SSH2_CLIENT} ]]; then
        SSH_FLAG=1
    fi
    if [ ${SSH_FLAG} -eq 1 ]; then
       PS1="${PS1}\[${COLOR_CYAN}\]\h "
    elif [[ -n ${SESS_SRC} ]]; then
        if [ "${SESS_SRC}" == "(:0.0)" ]; then
            PS1="${PS1}\[${COLOR_GREEN}\]\h "
        else
            local parent_process=`/bin/cat /proc/${PPID}/cmdline`
            if [[ "$parent_process" == "in.rlogind*" ]]; then
                PS1="${PS1}\[${COLOR_BROWN}\]\h "
            elif [[ "$parent_process" == "in.telnetd*" ]]; then
                PS1="${PS1}\[${COLOR_YELLOW}\]\h "
            else
                PS1="${PS1}\[${COLOR_LIGHTRED}\]\h "
            fi
        fi
    elif [[ "${SESS_SRC}" == "" ]]; then
        PS1="${PS1}\[${COLOR_GREEN}\]\h "
    else
        PS1="${PS1}\[${COLOR_RED}\]\h "
    fi

  # GIT repo information
  local GITBIN=`which git`
  if [ -x "$GITBIN" ]; then

  local GITBR=$("$GITBIN" branch 2>&1 | /usr/bin/perl -ne 'if(/^\* (.*)/) {print $1; last}')
  if [ -n "${GITBR}" ]; then
      PS1="${PS1}\[${COLOR_BLUE}\][git:${GITBR}"
      local GITST=$("$GITBIN" status --porcelain 2>&1 | /usr/bin/wc -l)
      if [ ${GITST} -gt 0 ]; then
          PS1="${PS1}*"
      fi
      PS1="${PS1}] "
  fi
  fi

  PS1="${PS1}\[${COLOR_DEFAULT}\]\!%"

  PS1="${PS1}\[${COLOR_DEFAULT}\] "
}

export PS1 promptcmd