misc/zshrc_vcsinfo
author Mikael Berthe <mikael@lilotux.net>
Wed, 18 Jul 2012 21:20:59 +0200
changeset 21 77f885aba914
parent 7 c032d39eafcf
child 23 c215dcd869ce
permissions -rw-r--r--
Thanks to Seth House for the vcs_info Mercurial examples Add his name at the top of the vcs_info prompt theme.

#
# MiKael, 2012-07-10
#
# Thanks to Seth House for his Mercurial+vcs_info examples.
#
# Sources:
#   https://github.com/whiteinge/dotfiles/blob/master/.zsh_shouse_prompt
#   http://git.rolinh.ch/index.php?p=dotfiles.git

# {{{

autoload -Uz vcs_info

zstyle ':vcs_info:*' enable git hg
zstyle ':vcs_info:(hg*|git*):*' get-revision true
zstyle ':vcs_info:(hg*|git*):*' check-for-changes true

zstyle ':vcs_info:hg*' formats "(%B%s%{${reset_color}%})[%i%u %b %m]" # rev+changes branch misc
#zstyle ':vcs_info:hg*' actionformats "(%s|%{$fg[red]%}%a%{$fg[white]%})[%i%u %b %m]"
zstyle ':vcs_info:hg*' actionformats "%{$fg[red]%}%a %{${reset_color}%}[%i%u %{$fg[blue]%}%b%{${reset_color}%}%m]"

zstyle ':vcs_info:hg*:*' get-bookmarks true
zstyle ':vcs_info:hg*:*' get-mq true

zstyle ':vcs_info:hg*:*' get-unapplied true
zstyle ':vcs_info:hg*:*' patch-format "mq(%g):%n/%c %p"
zstyle ':vcs_info:hg*:*' nopatch-format "mq(%g):%n/%c %p"

zstyle ':vcs_info:hg*:*' unstagedstr "%{$fg_bold[red]%}+%{${reset_color}%}"
zstyle ':vcs_info:hg*:*' hgrevformat "%r" # only show local rev.
zstyle ':vcs_info:hg*:*' branchformat "%b" # only show branch

zstyle ':vcs_info:git*' formats "(%B%s%{${reset_color}%}) %12.12i %c%u %b%m" # hash changes branch misc
#zstyle ':vcs_info:git*' formats "%c%u %b%m" # hash changes branch misc
zstyle ':vcs_info:git*' actionformats "(%s|%{$fg[white]%}%a) %12.12i %c%u %b%m"

zstyle ':vcs_info:git*:*' stagedstr "%{$fg[green]%}S%{${reset_color}%}"
zstyle ':vcs_info:git*:*' unstagedstr "%{$fg[red]%}U%{${reset_color}%}"

# zstyle ':vcs_info:hg:*:-all-' command fakehg
# zstyle ':vcs_info:*+*:*' debug true

zstyle ':vcs_info:hg*+set-hgrev-format:*' hooks hg-hashfallback
zstyle ':vcs_info:hg*+set-message:*' hooks mq-vcs
#zstyle ':vcs_info:git*+set-message:*' hooks git-st git-stash
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked git-st

### Dynamically set hgrevformat based on if the local rev is available
# We don't always know the local revision, e.g. if use-simple is set
# Truncate long hash to 12-chars but also allow for multiple parents
function +vi-hg-hashfallback() {
    if [[ -z ${hook_com[localrev]} ]] ; then
        local -a parents

        parents=( ${(s:+:)hook_com[hash]} )
        parents=( ${(@r:12:)parents} )
        hook_com[rev-replace]="${(j:+:)parents}"

        ret=1
    fi
}

### Show when mq itself is under version control
function +vi-mq-vcs() {
    # if [[ -d ${hook_com[base]}/.hg/patches/.hg ]]; then
        # hook_com[hg-mqpatch-string]="mq:${hook_com[hg-mqpatch-string]}"
    # fi
}

# Show remote ref name and number of commits ahead-of or behind
function +vi-git-st() {
    local ahead behind remote
    local -a gitstatus

    # Are we on a remote-tracking branch?
    remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
        --symbolic-full-name --abbrev-ref 2>/dev/null)}

    if [[ -n ${remote} ]] ; then
        # for git prior to 1.7
        # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
        ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
        (( $ahead )) && gitstatus+=( "%{$fg[green]%}+${ahead}%{${reset_color}%}" )

        # for git prior to 1.7
        # behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
        behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
        (( $behind )) && gitstatus+=( "%{$fg[red]%}-${behind}%{${reset_color}%}" )

        #hook_com[branch]="${hook_com[branch]} [${remote} ${(j:/:)gitstatus}]"
        hook_com[branch]="[%{$fg[blue]%}${hook_com[branch]}%{${reset_color}%}]"
    fi
}

# Show count of stashed changes
function +vi-git-stash() {
    local -a stashes

    if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then
        stashes=$(git stash list 2>/dev/null | wc -l)
        #hook_com[misc]+=" (${stashes} stashed)"
        hook_com[misc]+="(%{$fg[red]%}${stashes} %{${reset_color}%}stashed)"
    fi
}

# Show count of untracked files
function +vi-git-untracked(){
	if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
		git status --porcelain | grep '??' &> /dev/null ; then
		# This will show the marker if there are any untracked files in repo.
		# If instead you want to show the marker only if there are untracked
		# files in $PWD, use:
		#[[ -n $(git ls-files --others --exclude-standard) ]] ; then
		local nb_untracked=$(git status --porcelain | grep "\? \?" | wc -l)
		local nb_up=$(git status --porcelain | grep "M" | wc -l)
		hook_com[staged]+="%{${reset_color}%}$nb_untracked%{$fg[red]%}N %{${reset_color}%}$nb_up"
	fi
}

# }}}

# {{{ Prompts

setopt transient_rprompt

# Others prompts
PS2="%{$fg_no_bold[yellow]%}%_>%{${reset_color}%} "
PS3="%{$fg_no_bold[yellow]%}?#%{${reset_color}%} "

# }}}

# {{{ precmd()

function __precmd_vcsinfo {

  vcs_info

  local deco="%{${fg_no_bold[white]}%}"

  if [[ -O "$PWD" ]]; then
    local path_color="${fg_no_bold[magenta]}"
  elif [[ -w "$PWD" ]]; then
    local path_color="${fg_no_bold[blue]}"
  else
    local path_color="${fg_bold[red]}"
  fi

  if [[ $UID -eq 0 ]]; then
    local user_color="${fg_bold[red]}"
  else
    local user_color="${fg_bold[default]}"
  fi

  local return_code="%(?..${deco}!%{${fg_bold[red]}%}%?${deco}! %{${reset_color}%})"
  #local cwd="%{${path_color}%}%32<...<%~"
  local cwd="%{${path_color}%}%~%{${reset_color}%}"
  local sign="%(!.%{${fg_bold[red]}%}.${deco})%#"

  PS1="${cwd}
${return_code}%D{%m%d %H:%M} [%B%n%b] %{%{$fg[red]%}%}%m%{${reset_color}%}${sign}%{${reset_color}%} "

  # Prompt on the right, displayed when in a VCS repository
  RPS1="${vcs_info_msg_0_}%{${reset_color}%}"

  setopt prompt_subst
}

# }}}