functions/zshrc_vcsinfo
changeset 0 7215ca490221
equal deleted inserted replaced
-1:000000000000 0:7215ca490221
       
     1 #
       
     2 # MiKael, 2012-07-10
       
     3 # Sources:
       
     4 #   https://github.com/whiteinge/dotfiles/blob/master/.zsh_shouse_prompt
       
     5 #   http://git.rolinh.ch/index.php?p=dotfiles.git
       
     6 
       
     7 # {{{
       
     8 
       
     9 autoload -Uz vcs_info
       
    10 
       
    11 zstyle ':vcs_info:*' enable git hg
       
    12 zstyle ':vcs_info:(hg*|git*):*' get-revision true
       
    13 zstyle ':vcs_info:(hg*|git*):*' check-for-changes true
       
    14 
       
    15 zstyle ':vcs_info:hg*' formats "(%B%s%{${reset_color}%})[%i%u %b %m]" # rev+changes branch misc
       
    16 #zstyle ':vcs_info:hg*' actionformats "(%s|%{$fg[red]%}%a%{$fg[white]%})[%i%u %b %m]"
       
    17 zstyle ':vcs_info:hg*' actionformats "%{$fg[red]%}%a %{${reset_color}%}[%i%u %{$fg[blue]%}%b%{${reset_color}%}%m]"
       
    18 
       
    19 zstyle ':vcs_info:hg*:*' get-bookmarks true
       
    20 zstyle ':vcs_info:hg*:*' get-mq true
       
    21 
       
    22 zstyle ':vcs_info:hg*:*' get-unapplied true
       
    23 zstyle ':vcs_info:hg*:*' patch-format "mq(%g):%n/%c %p"
       
    24 zstyle ':vcs_info:hg*:*' nopatch-format "mq(%g):%n/%c %p"
       
    25 
       
    26 zstyle ':vcs_info:hg*:*' unstagedstr "%{$fg_bold[red]%}+%{${reset_color}%}"
       
    27 zstyle ':vcs_info:hg*:*' hgrevformat "%r" # only show local rev.
       
    28 zstyle ':vcs_info:hg*:*' branchformat "%b" # only show branch
       
    29 
       
    30 zstyle ':vcs_info:git*' formats "(%B%s%{${reset_color}%}) %12.12i %c%u %b%m" # hash changes branch misc
       
    31 #zstyle ':vcs_info:git*' formats "%c%u %b%m" # hash changes branch misc
       
    32 zstyle ':vcs_info:git*' actionformats "(%s|%{$fg[white]%}%a) %12.12i %c%u %b%m"
       
    33 
       
    34 zstyle ':vcs_info:git*:*' stagedstr "%{$fg[green]%}S%{${reset_color}%}"
       
    35 zstyle ':vcs_info:git*:*' unstagedstr "%{$fg[red]%}U%{${reset_color}%}"
       
    36 
       
    37 # zstyle ':vcs_info:hg:*:-all-' command fakehg
       
    38 # zstyle ':vcs_info:*+*:*' debug true
       
    39 
       
    40 zstyle ':vcs_info:hg*+set-hgrev-format:*' hooks hg-hashfallback
       
    41 zstyle ':vcs_info:hg*+set-message:*' hooks mq-vcs
       
    42 #zstyle ':vcs_info:git*+set-message:*' hooks git-st git-stash
       
    43 zstyle ':vcs_info:git*+set-message:*' hooks git-untracked git-st
       
    44 
       
    45 ### Dynamically set hgrevformat based on if the local rev is available
       
    46 # We don't always know the local revision, e.g. if use-simple is set
       
    47 # Truncate long hash to 12-chars but also allow for multiple parents
       
    48 function +vi-hg-hashfallback() {
       
    49     if [[ -z ${hook_com[localrev]} ]] ; then
       
    50         local -a parents
       
    51 
       
    52         parents=( ${(s:+:)hook_com[hash]} )
       
    53         parents=( ${(@r:12:)parents} )
       
    54         hook_com[rev-replace]="${(j:+:)parents}"
       
    55 
       
    56         ret=1
       
    57     fi
       
    58 }
       
    59 
       
    60 ### Show when mq itself is under version control
       
    61 function +vi-mq-vcs() {
       
    62     # if [[ -d ${hook_com[base]}/.hg/patches/.hg ]]; then
       
    63         # hook_com[hg-mqpatch-string]="mq:${hook_com[hg-mqpatch-string]}"
       
    64     # fi
       
    65 }
       
    66 
       
    67 # Show remote ref name and number of commits ahead-of or behind
       
    68 function +vi-git-st() {
       
    69     local ahead behind remote
       
    70     local -a gitstatus
       
    71 
       
    72     # Are we on a remote-tracking branch?
       
    73     remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
       
    74         --symbolic-full-name --abbrev-ref 2>/dev/null)}
       
    75 
       
    76     if [[ -n ${remote} ]] ; then
       
    77         # for git prior to 1.7
       
    78         # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
       
    79         ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
       
    80         (( $ahead )) && gitstatus+=( "%{$fg[green]%}+${ahead}%{${reset_color}%}" )
       
    81 
       
    82         # for git prior to 1.7
       
    83         # behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
       
    84         behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
       
    85         (( $behind )) && gitstatus+=( "%{$fg[red]%}-${behind}%{${reset_color}%}" )
       
    86 
       
    87         #hook_com[branch]="${hook_com[branch]} [${remote} ${(j:/:)gitstatus}]"
       
    88         hook_com[branch]="[%{$fg[blue]%}${hook_com[branch]}%{${reset_color}%}]"
       
    89     fi
       
    90 }
       
    91 
       
    92 # Show count of stashed changes
       
    93 function +vi-git-stash() {
       
    94     local -a stashes
       
    95 
       
    96     if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then
       
    97         stashes=$(git stash list 2>/dev/null | wc -l)
       
    98         #hook_com[misc]+=" (${stashes} stashed)"
       
    99         hook_com[misc]+="(%{$fg[red]%}${stashes} %{${reset_color}%}stashed)"
       
   100     fi
       
   101 }
       
   102 
       
   103 # Show count of untracked files
       
   104 function +vi-git-untracked(){
       
   105 	if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
       
   106 		git status --porcelain | grep '??' &> /dev/null ; then
       
   107 		# This will show the marker if there are any untracked files in repo.
       
   108 		# If instead you want to show the marker only if there are untracked
       
   109 		# files in $PWD, use:
       
   110 		#[[ -n $(git ls-files --others --exclude-standard) ]] ; then
       
   111 		local nb_untracked=$(git status --porcelain | grep "\? \?" | wc -l)
       
   112 		local nb_up=$(git status --porcelain | grep "M" | wc -l)
       
   113 		hook_com[staged]+="%{${reset_color}%}$nb_untracked%{$fg[red]%}N %{${reset_color}%}$nb_up"
       
   114 	fi
       
   115 }
       
   116 
       
   117 # }}}
       
   118 
       
   119 # {{{ Prompts
       
   120 
       
   121 setopt transient_rprompt
       
   122 
       
   123 # Others prompts
       
   124 PS2="%{$fg_no_bold[yellow]%}%_>%{${reset_color}%} "
       
   125 PS3="%{$fg_no_bold[yellow]%}?#%{${reset_color}%} "
       
   126 
       
   127 # }}}
       
   128 
       
   129 # {{{ precmd()
       
   130 
       
   131 function __precmd_vcsinfo {
       
   132 
       
   133   vcs_info
       
   134 
       
   135   local deco="%{${fg_no_bold[white]}%}"
       
   136 
       
   137   if [[ -O "$PWD" ]]; then
       
   138     local path_color="${fg_no_bold[magenta]}"
       
   139   elif [[ -w "$PWD" ]]; then
       
   140     local path_color="${fg_no_bold[blue]}"
       
   141   else
       
   142     local path_color="${fg_bold[red]}"
       
   143   fi
       
   144 
       
   145   if [[ $UID -eq 0 ]]; then
       
   146     local user_color="${fg_bold[red]}"
       
   147   else
       
   148     local user_color="${fg_bold[default]}"
       
   149   fi
       
   150 
       
   151   local return_code="%(?..${deco}!%{${fg_bold[red]}%}%?${deco}! %{${reset_color}%})"
       
   152   #local cwd="%{${path_color}%}%32<...<%~"
       
   153   local cwd="%{${path_color}%}%~%{${reset_color}%}"
       
   154   local sign="%(!.%{${fg_bold[red]}%}.${deco})%#"
       
   155 
       
   156   PS1="${cwd}
       
   157 ${return_code}%D{%m%d %H:%M} [%B%n%b] %{%{$fg[red]%}%}%m%{${reset_color}%}${sign}%{${reset_color}%} "
       
   158 
       
   159   # Prompt on the right, displayed when in a VCS repository
       
   160   RPS1="${vcs_info_msg_0_}%{${reset_color}%}"
       
   161 
       
   162   setopt prompt_subst
       
   163 }
       
   164 
       
   165 # }}}