Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if [[ $- == *i* ]] ; then
- # some people may prefer this off, so lets handle it nicely
- function use_extglob {
- # it's defaulting to on, leave it as is
- if [ -z "$extglob_off" ] ; then return ; fi
- # else we're either turning it on to use it, or turning it off to return to prefered setting
- if [ "$1" = "on" ] ; then shopt -s extglob ; else shopt -u extglob ; fi
- }
- # what branch is this?
- function git_branch {
- foo=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
- if [ "$foo" != "" ] ; then foo="[$foo]" ; fi
- echo $foo
- }
- # bash does not have a builtin version of this, so time to do it the long winded way :/
- function expand_ps1 {
- if [[ "$PWD" =~ ^$HOME ]] ; then pwd=$( echo $PWD | sed "s:^${HOME}:~:") ; else pwd="$PWD" ; fi
- use_extglob on
- sane_PS1="${PS1//\\\[*(\\[^]]|[^\\])\\\]/}"
- use_extglob off
- sane_PS1="${sane_PS1//\\t/xx:xx:xx}"
- sane_PS1="${sane_PS1//\\u/$USER}"
- sane_PS1="${sane_PS1//\\h/${HOSTNAME%%.*}}"
- sane_PS1="${sane_PS1//\\w/$pwd}"
- sane_PS1="${sane_PS1/\$(git_branch)/$(git_branch)}"
- sane_PS1="${sane_PS1/\\n$/}"
- }
- function prompt_command {
- # store the state of the extglob shell option (and then either act on it or not in the use_extglob function)
- shopt -q extglob || extglob_off=1
- # expand the PS1, setting sane_PS1
- expand_ps1
- # COLUMNS only gets set on a SIGWINCH, which doesn't happen on remote shells, fudge
- # around it
- if [ -z "$COLUMNS" ] ; then cols="$(tput col)" ; else cols="${COLUMNS}" ; fi
- # if the expanded PS1 is greater than screen size, just set it to "-"
- if [ "${#sane_PS1}" -gt "$cols" ] ; then
- fill="-"
- # else we need to calc how big to make it
- else
- # create a $fill of all screen width minus the time string and a space:
- fillsize="$(( $cols - ${#sane_PS1} + 3 ))"
- # echo $sane_PS1 | hexdump -C
- # perl -e "print 'sane_PS1: ' . length('$sane_PS1') . \" - $sane_PS1\n\";"
- # echo "Working with fillsize of $fillsize, $cols ${#sane_PS1}"
- fill=" "
- while [ "$fillsize" -gt "0" ]
- do
- fill="${fill}-" # fill with underscores to work on
- let fillsize=${fillsize}-1
- done
- fill="${fill} "
- fi
- }
- # are we on a color-capable TERM?
- color_term="$(tput colors)"
- if [ "$color_term" -gt "0" ] ; then
- res="\[$(tput sgr0)\]"
- und="\[$(tput smul)\]"
- bol="\[$(tput bold)\]"
- bli="\[$(tput blink)\]"
- # might need this for bold
- # bol="\[$(tput smso)\]"
- col0="\[$(tput setaf 0)\]" # black
- col1="\[$(tput setaf 1)\]" # red
- col2="\[$(tput setaf 2)\]" # green
- col3="\[$(tput setaf 3)\]" # yellow
- col4="\[$(tput setaf 4)\]" # blue
- col5="\[$(tput setaf 5)\]" # magenta
- col6="\[$(tput setaf 6)\]" # cyan
- col7="\[$(tput setaf 7)\]" # white
- fi
- prom='$ '
- usercol=$col5
- # if I am root? set different username and prompt (# vs $)
- if [ "$UID" = "0" ]; then
- usercol=$bol$col1
- prom='# '
- fi
- # Set the prompt
- PS1="${bol}${col7}(${res}${usercol}\u@${res}${col2}\h${bol}${col7}${bol})${res}-${bol}(${res}${col6}\w${bol}${col7})${col5}\$(git_branch)${res}"
- # Modify for xterms to set the title bar too
- if [ "$TERM" = 'xterm-color' -o "$TERM" = 'xterm' ] ; then
- PS1="\[\033]0;[\h] \w\007\]$PS1"
- fi
- OLD_PS1="$PS1"
- PS1="$OLD_PS1"'$fill'"${bol}(${res}\t${bol})${res}\n${prom}"
- PROMPT_COMMAND=prompt_command
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement