Advertisement
dravster

.bash_ps1

Sep 2nd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.80 KB | None | 0 0
  1. if [[ $- == *i* ]] ; then
  2.  
  3.     # some people may prefer this off, so lets handle it nicely
  4.     function use_extglob {
  5.         # it's defaulting to on, leave it as is
  6.         if [ -z "$extglob_off" ] ; then return ; fi
  7.  
  8.         # else we're either turning it on to use it, or turning it off to return to prefered setting
  9.         if [ "$1" = "on" ] ; then shopt -s extglob ; else shopt -u extglob ; fi
  10.     }
  11.  
  12.     # what branch is this?
  13.     function git_branch {
  14.         foo=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
  15.         if [ "$foo" != "" ] ; then foo="[$foo]" ; fi
  16.         echo $foo
  17.     }
  18.  
  19.     # bash does not have a builtin version of this, so time to do it the long winded way :/
  20.     function expand_ps1 {
  21.         if [[ "$PWD" =~ ^$HOME ]] ; then pwd=$( echo $PWD | sed "s:^${HOME}:~:") ; else pwd="$PWD" ; fi
  22.         use_extglob on
  23.         sane_PS1="${PS1//\\\[*(\\[^]]|[^\\])\\\]/}"
  24.         use_extglob off
  25.         sane_PS1="${sane_PS1//\\t/xx:xx:xx}"
  26.         sane_PS1="${sane_PS1//\\u/$USER}"
  27.         sane_PS1="${sane_PS1//\\h/${HOSTNAME%%.*}}"
  28.         sane_PS1="${sane_PS1//\\w/$pwd}"
  29.         sane_PS1="${sane_PS1/\$(git_branch)/$(git_branch)}"
  30.         sane_PS1="${sane_PS1/\\n$/}"
  31.     }
  32.  
  33.     function prompt_command {
  34.         # store the state of the extglob shell option (and then either act on it or not in the use_extglob function)
  35.         shopt -q extglob || extglob_off=1
  36.  
  37.         # expand the PS1, setting sane_PS1
  38.         expand_ps1
  39.  
  40.         # COLUMNS only gets set on a SIGWINCH, which doesn't happen on remote shells, fudge
  41.         # around it
  42.         if [ -z "$COLUMNS" ] ; then cols="$(tput col)" ; else cols="${COLUMNS}" ; fi
  43.  
  44.         # if the expanded PS1 is greater than screen size, just set it to "-"
  45.         if [ "${#sane_PS1}" -gt "$cols" ] ; then
  46.             fill="-"
  47.  
  48.         # else we need to calc how big to make it
  49.         else
  50.  
  51.             # create a $fill of all screen width minus the time string and a space:
  52.             fillsize="$(( $cols - ${#sane_PS1} + 3 ))"
  53.  
  54.             # echo $sane_PS1 | hexdump -C
  55.             # perl -e "print 'sane_PS1: ' . length('$sane_PS1') . \" - $sane_PS1\n\";"
  56.             # echo "Working with fillsize of $fillsize, $cols ${#sane_PS1}"
  57.  
  58.             fill=" "
  59.             while [ "$fillsize" -gt "0" ]
  60.             do
  61.                 fill="${fill}-" # fill with underscores to work on
  62.                 let fillsize=${fillsize}-1
  63.             done
  64.             fill="${fill} "
  65.         fi
  66.     }
  67.  
  68.     # are we on a color-capable TERM?
  69.     color_term="$(tput colors)"
  70.  
  71.     if [ "$color_term" -gt "0" ] ; then
  72.         res="\[$(tput sgr0)\]"
  73.         und="\[$(tput smul)\]"
  74.         bol="\[$(tput bold)\]"
  75.         bli="\[$(tput blink)\]"
  76.  
  77.         # might need this for bold
  78.         # bol="\[$(tput smso)\]"
  79.  
  80.         col0="\[$(tput setaf 0)\]" # black
  81.         col1="\[$(tput setaf 1)\]" # red
  82.         col2="\[$(tput setaf 2)\]" # green
  83.         col3="\[$(tput setaf 3)\]" # yellow
  84.         col4="\[$(tput setaf 4)\]" # blue
  85.         col5="\[$(tput setaf 5)\]" # magenta
  86.         col6="\[$(tput setaf 6)\]" # cyan
  87.         col7="\[$(tput setaf 7)\]" # white
  88.     fi
  89.  
  90.     prom='$ '
  91.     usercol=$col5
  92.  
  93.     # if I am root? set different username and prompt (# vs $)
  94.     if [ "$UID" = "0" ]; then
  95.         usercol=$bol$col1
  96.         prom='# '
  97.     fi
  98.  
  99.     # Set the prompt
  100.     PS1="${bol}${col7}(${res}${usercol}\u@${res}${col2}\h${bol}${col7}${bol})${res}-${bol}(${res}${col6}\w${bol}${col7})${col5}\$(git_branch)${res}"
  101.  
  102.     # Modify for xterms to set the title bar too
  103.     if [ "$TERM" = 'xterm-color' -o "$TERM" = 'xterm' ] ; then
  104.         PS1="\[\033]0;[\h] \w\007\]$PS1"
  105.     fi
  106.  
  107.     OLD_PS1="$PS1"
  108.     PS1="$OLD_PS1"'$fill'"${bol}(${res}\t${bol})${res}\n${prom}"
  109.  
  110.     PROMPT_COMMAND=prompt_command
  111. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement