diff options
Diffstat (limited to 'git-prompt.sh')
-rwxr-xr-x | git-prompt.sh | 142 |
1 files changed, 75 insertions, 67 deletions
diff --git a/git-prompt.sh b/git-prompt.sh index 322164d..66abb07 100755 --- a/git-prompt.sh +++ b/git-prompt.sh @@ -9,6 +9,7 @@ # clear vars from previous invocation unset dir_color rc_color user_id_color root_id_color init_vcs_color clean_vcs_color + unset prompt_color jobs_color time_color unset modified_vcs_color added_vcs_color addmoded_vcs_color untracked_vcs_color op_vcs_color detached_vcs_color hex_vcs_color unset rawhex_len @@ -39,10 +40,13 @@ #### dir, rc, root color - cols=`tput colors` # in emacs shell-mode tput colors returns -1 + cols="$(tput colors)" # in emacs shell-mode tput colors returns -1 if [[ -n "$cols" && $cols -ge 8 ]]; then # if terminal supports colors dir_color=${dir_color:-CYAN} + time_color=${time_color:-BLUE} rc_color=${rc_color:-red} + jobs_color=${jobs_color:-cyan} + prompt_color=${prompt_color:-CYAN} virtualenv_color=${virtualenv_color:-green} user_id_color=${user_id_color:-blue} root_id_color=${root_id_color:-magenta} @@ -53,8 +57,8 @@ unset cols #### prompt character, for root/non-root - prompt_char=${prompt_char:-'>'} - root_prompt_char=${root_prompt_char:-'>'} + prompt_char="${prompt_char:-'>'}" + root_prompt_char="${root_prompt_char:-'>'}" #### vcs colors init_vcs_color=${init_vcs_color:-WHITE} # initial @@ -75,7 +79,7 @@ count_only=${count_only:-off} rawhex_len=${rawhex_len:-5} - aj_max=20 + aj_max=${aj_max:-20} ##################################################################### post config @@ -113,32 +117,32 @@ ### if term support colors, then use color prompt, else bold - black='\['`tput sgr0; tput setaf 0`'\]' - red='\['`tput sgr0; tput setaf 1`'\]' - green='\['`tput sgr0; tput setaf 2`'\]' - yellow='\['`tput sgr0; tput setaf 3`'\]' - blue='\['`tput sgr0; tput setaf 4`'\]' - magenta='\['`tput sgr0; tput setaf 5`'\]' - cyan='\['`tput sgr0; tput setaf 6`'\]' - white='\['`tput sgr0; tput setaf 7`'\]' + black='\['"$(tput sgr0; tput setaf 0)"'\]' + red='\['"$(tput sgr0; tput setaf 1)"'\]' + green='\['"$(tput sgr0; tput setaf 2)"'\]' + yellow='\['"$(tput sgr0; tput setaf 3)"'\]' + blue='\['"$(tput sgr0; tput setaf 4)"'\]' + magenta='\['"$(tput sgr0; tput setaf 5)"'\]' + cyan='\['"$(tput sgr0; tput setaf 6)"'\]' + white='\['"$(tput sgr0; tput setaf 7)"'\]' - BLACK='\['`tput setaf 0; tput bold`'\]' - RED='\['`tput setaf 1; tput bold`'\]' - GREEN='\['`tput setaf 2; tput bold`'\]' - YELLOW='\['`tput setaf 3; tput bold`'\]' - BLUE='\['`tput setaf 4; tput bold`'\]' - MAGENTA='\['`tput setaf 5; tput bold`'\]' - CYAN='\['`tput setaf 6; tput bold`'\]' - WHITE='\['`tput setaf 7; tput bold`'\]' + BLACK='\['"$(tput setaf 0; tput bold)"'\]' + RED='\['"$(tput setaf 1; tput bold)"'\]' + GREEN='\['"$(tput setaf 2; tput bold)"'\]' + YELLOW='\['"$(tput setaf 3; tput bold)"'\]' + BLUE='\['"$(tput setaf 4; tput bold)"'\]' + MAGENTA='\['"$(tput setaf 5; tput bold)"'\]' + CYAN='\['"$(tput setaf 6; tput bold)"'\]' + WHITE='\['"$(tput setaf 7; tput bold)"'\]' - dim='\['`tput sgr0; tput setaf p1`'\]' # half-bright + dim='\['"$(tput sgr0; tput setaf p1)"'\]' # half-bright - bw_bold='\['`tput bold`'\]' + bw_bold='\['"$(tput bold)"'\]' on='' off=': ' - bell="\[`eval ${!error_bell} tput bel`\]" - colors_reset='\['`tput sgr0`'\]' + bell="\["$(eval ${!error_bell} tput bel)"\]" + colors_reset='\['"$(tput sgr0)"'\]' # replace symbolic colors names to raw treminfo strings init_vcs_color=${!init_vcs_color} @@ -162,6 +166,7 @@ fi #################################################################### MARKERS + screen_marker="sCRn" if [[ "$LC_CTYPE $LC_ALL" =~ "UTF" && $TERM != "linux" ]]; then elipses_marker="…" else @@ -209,7 +214,7 @@ cwd_truncate() { if [[ ${#path_middle} -gt $(( $cwd_middle_max + ${#elipses_marker} + 5 )) ]]; then # truncate - middle_tail=${path_middle:${#path_middle}-${cwd_middle_max}} + middle_tail="${path_middle:${#path_middle}-${cwd_middle_max}}" # trunc on dir boundary (trunc 1st, probably tuncated dir) [[ $middle_tail =~ '[^/]*/(.*)$' ]] @@ -217,7 +222,7 @@ cwd_truncate() { # use truncated only if we cut at least 4 chars if [[ $(( ${#path_middle} - ${#middle_tail})) -gt 4 ]]; then - cwd=$path_head$elipses_marker$middle_tail$path_last_dir + cwd="$path_head$elipses_marker$middle_tail$path_last_dir" fi fi fi @@ -234,7 +239,7 @@ set_shell_label() { screen_label() { # FIXME: run this only if screen is in xterm (how to test for this?) - xterm_label "$plain_who_where $@" + xterm_label "$screen_marker $plain_who_where $@" # FIXME $STY not inherited though "su -" [ "$STY" ] && screen -S $STY -X title "$*" @@ -263,13 +268,13 @@ set_shell_label() { export -f set_shell_label ###################################################### ID (user name) - id=`id -un` - id=${id#$default_user} + id="$(id -un)" + id="${id#$default_user}" ########################################################### TTY - tty=`tty` - tty=`echo $tty | sed "s:/dev/pts/:p:; s:/dev/tty::" ` # RH tty devs - tty=`echo $tty | sed "s:/dev/vc/:vc:" ` # gentoo tty devs + tty="$(tty)" + tty="$(echo $tty | sed "s:/dev/pts/:p:; s:/dev/tty::")" # RH tty devs + tty="$(echo $tty | sed "s:/dev/vc/:vc:")" # gentoo tty devs if [[ "$TERM" = "screen" ]] ; then @@ -288,7 +293,10 @@ set_shell_label() { esac dir_color=${!dir_color} + time_color=${!time_color} rc_color=${!rc_color} + prompt_color=${!prompt_color} + jobs_color=${!jobs_color} virtualenv_color=${!virtualenv_color} user_id_color=${!user_id_color} root_id_color=${!root_id_color} @@ -299,19 +307,19 @@ set_shell_label() { # How to find out if session is local or remote? Working with "su -", ssh-agent, and so on ? ## is sshd our parent? - # if { for ((pid=$$; $pid != 1 ; pid=`ps h -o pid --ppid $pid`)); do ps h -o command -p $pid; done | grep -q sshd && echo == REMOTE ==; } + # if { for ((pid=$$; $pid != 1 ; pid="$(ps h -o pid --ppid $pid)")); do ps h -o command -p $pid; done | grep -q sshd && echo == REMOTE ==; } #then host=${HOSTNAME} if [[ $short_hostname = "on" ]]; then if [[ "$(uname)" =~ "CYGWIN" ]]; then - host=`hostname` + host="$(hostname)" else - host=`hostname -s` + host="$(hostname -s)" fi fi host=${host#$default_host} - uphost=`echo ${host} | tr a-z-. A-Z_` + uphost="$(echo ${host} | tr a-z-. A-Z_)" if [[ $upcase_hostname = "on" ]]; then host=${uphost} fi @@ -319,7 +327,7 @@ set_shell_label() { host_color=${uphost}_host_color host_color=${!host_color} if [[ -z $host_color && -x /usr/bin/cksum ]] ; then - cksum_color_no=`echo $uphost | cksum | awk '{print $1%6}'` + cksum_color_no="$(echo $uphost | cksum | awk '{print $1%6}')" color_index=(green yellow blue magenta cyan white) # FIXME: bw, color-256 host_color=${color_index[cksum_color_no]} fi @@ -331,11 +339,11 @@ set_shell_label() { #################################################################### WHO_WHERE # [[user@]host[-tty]] - - if [[ -n $id || -n $host ]] ; then + + if [[ -n $id || -n $host || -n $special_env ]] ; then [[ -n $id && -n $host ]] && at='@' || at='' - color_who_where="${id}${host:+$host_color$at$host}${tty:+ $tty}" - plain_who_where="${id}$at$host" + color_who_where="${special_env:+($special_env) }${id}${host:+$host_color$at$host}${tty:+ $tty}" + plain_who_where="${special_env:+($special_env) }${id}$at$host" # add trailing " " color_who_where="$color_who_where " @@ -359,23 +367,23 @@ parse_svn_status() { vcs=svn ### get rev - eval ` + eval "$( svn info | sed -n " s@^URL[^/]*//@repo_dir=@p s/^Revision: /rev=/p " - ` + )" ### get status unset status modified added clean init added mixed untracked op detached - eval `svn status 2>/dev/null | + eval "$(svn status 2>/dev/null | sed -n ' s/^A... \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p s/^M... \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p s/^\?... \([^.].*\)/untracked=untracked; untracked_files[${#untracked_files[@]}]=\"\1\";/p ' - ` + )" # TODO branch detection if standard repo layout [[ -z $modified ]] && [[ -z $untracked ]] && clean=clean @@ -392,16 +400,16 @@ parse_hg_status() { ### get status unset status modified added clean init added mixed untracked op detached - eval `hg status 2>/dev/null | + eval "$(hg status 2>/dev/null | sed -n ' s/^M \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p s/^A \([^.].*\)/added=added; added_files[${#added_files[@]}]=\"\1\";/p s/^R \([^.].*\)/added=added;/p s/^! \([^.].*\)/modified=modified;/p s/^? \([^.].*\)/untracked=untracked; untracked_files[${#untracked_files[@]}]=\\"\1\\";/p - '` + ')" - branch=`hg branch 2> /dev/null` + branch="$(hg branch 2> /dev/null)" [[ -f $hg_root/.hg/bookmarks.current ]] && bookmark=`cat "$hg_root/.hg/bookmarks.current"` @@ -418,9 +426,9 @@ parse_git_status() { # TODO add status: LOCKED (.git/index.lock) - git_dir=`[[ $git_module = "on" ]] && git rev-parse --git-dir 2> /dev/null` - #git_dir=`eval \$$git_module git rev-parse --git-dir 2> /dev/null` - #git_dir=` git rev-parse --git-dir 2> /dev/null` + git_dir="$([[ $git_module = "on" ]] && git rev-parse --git-dir 2> /dev/null)" + #git_dir="$(eval \$$git_module git rev-parse --git-dir 2> /dev/null)" + #git_dir="$( git rev-parse --git-dir 2> /dev/null)" [[ -n ${git_dir/./} ]] || return 1 [[ -f ${git_dir}/git-prompt-ignored ]] && return 1 @@ -445,9 +453,9 @@ parse_git_status() { s/^\(# \)*On branch /branch=/p s/^nothing to commi.*/clean=clean/p s/^\(# \)*Initial commi.*/init=init/p - s/^\(# \)*Your branch is ahead of \(.\).\+\1 by [[:digit:]]\+ commit.*/freshness=${WHITE}↑/p - s/^\(# \)*Your branch is behind \(.\).\+\1 by [[:digit:]]\+ commit.*/freshness=${YELLOW}↓/p - s/^\(# \)*Your branch and \(.\).\+\1 have diverged.*/freshness=${YELLOW}↕/p + s/^\(# \)*Your branch is ahead of ..\+. by [[:digit:]]\+ commit.*/freshness=${WHITE}↑/p + s/^\(# \)*Your branch is behind ..\+. by [[:digit:]]\+ commit.*/freshness=${YELLOW}↓/p + s/^\(# \)*Your branch and ..\+. have diverged.*/freshness=${YELLOW}↕/p ' )" @@ -522,7 +530,7 @@ parse_git_status() { #### GET GIT HEX-REVISION if [[ $rawhex_len -gt 0 ]] ; then - rawhex=`git rev-parse HEAD 2>/dev/null` + rawhex="$(git rev-parse HEAD 2>/dev/null)" rawhex=${rawhex/HEAD/} rawhex="$hex_vcs_color${rawhex:0:$rawhex_len}" else @@ -543,7 +551,7 @@ parse_git_status() { else if [[ "$detached" ]] ; then - branch="<detached:`git name-rev --name-only HEAD 2>/dev/null`" + branch="<detached:$(git name-rev --name-only HEAD 2>/dev/null)" elif [[ "$op" ]]; then @@ -591,9 +599,9 @@ parse_vcs_status() { if [[ $vim_module = "on" ]] ; then # equivalent to vim_glob=`ls .*.vim` but without running ls unset vim_glob vim_file vim_files - old_nullglob=`shopt -p nullglob` + old_nullglob="$(shopt -p nullglob)" shopt -s nullglob - vim_glob=`echo .*.sw?` + vim_glob="$(echo .*.sw?)" eval $old_nullglob if [[ $vim_glob ]]; then @@ -660,8 +668,8 @@ disable_set_shell_label() { enable_set_shell_label() { disable_set_shell_label # check for BASH_SOURCE being empty, no point running set_shell_label on every line of .bashrc - trap '[[ -z "$BASH_SOURCE" && ($BASH_COMMAND != prompt_command_function) ]] && - set_shell_label $BASH_COMMAND' DEBUG >& /dev/null + trap '[[ "$BASH_COMMAND" != prompt_command_function ]] && + set_shell_label "$BASH_COMMAND"' DEBUG >& /dev/null } declare -ft disable_set_shell_label @@ -693,15 +701,13 @@ alias jumpstart='echo ${aj_dir_list[@]}' prompt_command_function() { rc="$?" - if [[ "$rc" == "0" ]]; then - rc="" - else - rc="$rc_color$rc$colors_reset$bell " - fi + rc="$rc_color($rc)$colors_reset" - cwd=${PWD/$HOME/\~} # substitute "~" + cwd="${PWD/$HOME/~}" # substitute "~" set_shell_label "${cwd##[/~]*/}/" # default label - path last dir + timestamp="$(date -Iseconds)" + parse_virtualenv_status parse_vcs_status @@ -714,7 +720,9 @@ prompt_command_function() { # else eval cwd_cmd, cwd should have path after exection eval "${cwd_cmd/\\/cwd=\\\\}" - PS1="$colors_reset$rc$head_local$color_who_where$dir_color$cwd$tail_local$dir_color$prompt_char $colors_reset" + jobs="$jobs_color[\j]$jobs_color" + + PS1="$colors_reset$time_color$timestamp$colors_reset\n$color_who_where$dir_color$cwd $head_local$colors_reset\n$rc$jobs$prompt_color$prompt_char $colors_reset" unset head_local tail_local pwd } |