Guest User

Untitled

a guest
Sep 6th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.90 KB | None | 0 0
  1. # setup color variables
  2. color_is_on=
  3. color_red=
  4. color_green=
  5. color_yellow=
  6. color_blue=
  7. color_white=
  8. color_gray=
  9. color_bg_red=
  10. color_off=
  11. if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  12.     color_is_on=true
  13.     color_red="\[$(/usr/bin/tput setaf 1)\]"
  14.     color_green="\[$(/usr/bin/tput setaf 2)\]"
  15.     color_yellow="\[$(/usr/bin/tput setaf 3)\]"
  16.     color_blue="\[$(/usr/bin/tput setaf 6)\]"
  17.     color_white="\[$(/usr/bin/tput setaf 7)\]"
  18.     color_gray="\[$(/usr/bin/tput setaf 8)\]"
  19.     color_off="\[$(/usr/bin/tput sgr0)\]"
  20.     color_error="$(/usr/bin/tput setab 1)$(/usr/bin/tput setaf 7)"
  21.     color_error_off="$(/usr/bin/tput sgr0)"
  22. fi
  23.  
  24. function prompt_command {
  25.     local PS1_GIT=
  26.     local PWDNAME=$PWD
  27.  
  28.     # get cursor position and add new line if we're not in first column
  29.     exec < /dev/tty
  30.     local OLDSTTY=$(stty -g)
  31.     stty raw -echo min 0
  32.     echo -en "\033[6n" > /dev/tty && read -sdR CURPOS
  33.     stty $OLDSTTY
  34.     [[ ${CURPOS##*;} -gt 1 ]] && echo "${color_error}.${color_error_off}"
  35.  
  36.    # beautify working firectory name
  37.     if [ $HOME == $PWD ]; then
  38.         PWDNAME="~"
  39.     elif [ $HOME ==  ${PWD:0:${#HOME}} ]; then
  40.         PWDNAME="~${PWD:${#HOME}}"
  41.     fi
  42.  
  43.  
  44.     # parse git status and get git variables
  45.     parse_git_status
  46.  
  47.     # build b/w prompt for git
  48.     [[ ! -z $GIT_BRANCH ]] && PS1_GIT=" (${GIT_BRANCH})"
  49.  
  50.     local color_user=
  51.     if $color_is_on; then
  52.         # set user color
  53.         case `id -u` in
  54.             0) color_user=$color_red ;;
  55.             *) color_user=$color_green ;;
  56.         esac
  57.  
  58.         # build git status for prompt
  59.         if [ ! -z $GIT_BRANCH ]; then
  60.             if [ -z $GIT_DIRTY ]; then
  61.                 PS1_GIT=" (${color_green}${GIT_BRANCH}${color_off})"
  62.             else
  63.                 PS1_GIT=" (${color_red}${GIT_BRANCH}${color_off})"
  64.             fi
  65.         fi
  66.     fi
  67.  
  68.     # set new color prompt
  69.     PS1="${color_user}${USER}${color_off}@${color_yellow}${HOSTNAME}${color_off}:${color_white}${PWDNAME}${color_off}${PS1_GIT} \$ "
  70. }
  71. PROMPT_COMMAND=prompt_command
  72.  
  73. # get git status
  74. function parse_git_status {
  75.     # clear git variables
  76.     GIT_BRANCH=
  77.     GIT_DIRTY=
  78.  
  79.     # exit if no git found in system
  80.     local GIT_BIN=$(which git 2>/dev/null)
  81.     [[ -z $GIT_BIN ]] && return
  82.  
  83.     # check we are in git repo
  84.     local CUR_DIR=$PWD
  85.     while [ ! -d ${CUR_DIR}/.git ] && [ ! $CUR_DIR = "/" ]; do CUR_DIR=${CUR_DIR%/*}; done
  86.     [[ ! -d ${CUR_DIR}/.git ]] && return
  87.  
  88.     # 'git repo for dotfiles' fix: show git status only in home dir and other git repos
  89.     [[ $CUR_DIR == $HOME ]] && [[ $PWD != $HOME ]] && return
  90.  
  91.     # get git branch
  92.     GIT_BRANCH=$($GIT_BIN symbolic-ref HEAD 2>/dev/null)
  93.     [[ -z $GIT_BRANCH ]] && return
  94.     GIT_BRANCH=${GIT_BRANCH#refs/heads/}
  95.  
  96.     # get git status
  97.     local GIT_STATUS=$($GIT_BIN status --porcelain 2>/dev/null)
  98.     [[ -n $GIT_STATUS ]] && GIT_DIRTY=true
  99. }
Advertisement
Add Comment
Please, Sign In to add comment