Infrastructure for displaying scm info in prompt + implemented for git and svn

this should not break the old way of putting git info in the prompt.

Also changed awesomepanda theme to use the new scm architecture.

for users:
this works the same as plugins: the scms variable is set in ~/.zshrc with a list of SCM for which info should be displayed in the prompt, as simple as that.

for theme developers:
instead of calling the git_prompt_info function, you can call the get_scm_prompt, which will display info for all the scms in the scms variable.
the following variables can be used in the old git-way:

ZSH_THEME_SCM_PROMPT_PREFIX: before everything but the scm's name (svn, git, ..)
ZSH_THEME_SCM_PROMPT_SUFFIX: after everything
ZSH_THEME_SCM_PROMPT_DIRTY: displayed when the repo is dirty
ZSH_THEME_SCM_PROMPT_CLEAN	displated when the repo is clean

by default, the name of the scm (git, svn, ...) is not displayed before the scm info, but this can be changed by setting the ZSH_THEME_SCM_DISPLAY_NAME to 1.

for scm-plugin developers:
to make an scm plugin with the name 'foo':
add a script called 'foo.scm.zsh' in to the scm folder. this script has to contain two functions:
    scm_in_foo_repo: checks wether we are in a foo repo, usually by checking if a .foo folder is present
    scm_foo_prompt_info: returns the prompt which follows the rules outlined above (in the "for theme developers"-section)
This commit is contained in:
Robin Ramael 2011-01-10 17:11:46 +01:00
commit b989ec489c
4 changed files with 92 additions and 0 deletions

View file

@ -34,5 +34,22 @@ ZSH_THEME_GIT_PROMPT_CLEAN="" # Text to display if the branch is c
# Setup the prompt with pretty colors
setopt prompt_subst
#load scm prompt info
ZSH_THEME_SCM_DISPLAY_NAME=0
for scm in $scms; do
source $ZSH/scm/$scm.scm.zsh
done
function get_scm_prompt () {
for scm in $scms; do
if [ $("scm_in_"$scm"_repo") ]; then
echo `"scm_"$scm"_prompt_info"`
fi
done
}
# Load the theme
source "$ZSH/themes/$ZSH_THEME.zsh-theme"

22
scm/git.scm.zsh Normal file
View file

@ -0,0 +1,22 @@
function scm_in_git_repo () {
if [[ -d .git ]]; then
echo 1
fi
}
function scm_git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
out="$ZSH_THEME_SCM_PROMPT_PREFIX${ref#refs/heads/}$(scm_parse_git_dirty)$ZSH_THEME_SCM_PROMPT_SUFFIX"
if [[ ZSH_THEME_SCM_DISPLAY_NAME -eq 1 ]]; then
out="git$out"
fi
echo $out
}
scm_parse_git_dirty () {
if [[ -n $(git status -s 2> /dev/null) ]]; then
echo "$ZSH_THEME_SCM_PROMPT_DIRTY"
else
echo "$ZSH_THEME_SCM_PROMPT_CLEAN"
fi
}

42
scm/svn.scm.zsh Normal file
View file

@ -0,0 +1,42 @@
function scm_in_svn_repo() {
if [[ -d .svn ]]; then
echo 1
fi
}
function scm_svn_prompt_info {
if [ -d .svn ]; then
out="$ZSH_THEME_SCM_PROMPT_PREFIX$(svn_get_repo_name)$(parse_svn_dirty)$ZSH_THEME_SCM_PROMPT_SUFFIX"
if [[ ZSH_THEME_SCM_DISPLAY_NAME -eq 1 ]]; then
out="svn$out"
fi
echo $out
fi
}
function svn_get_repo_name {
if [ in_svn_repo ]; then
svn info | sed -n 's/Repository\ Root:\ .*\///p' | read SVN_ROOT
svn info | sed -n "s/URL:\ .*$SVN_ROOT\///p" | sed "s/\/.*$//"
fi
}
function svn_get_rev_nr {
if [ in_svn_repo ]; then
svn info 2> /dev/null | sed -n s/Revision:\ //p
fi
}
function parse_svn_dirty {
if [ in_svn_repo ]; then
s=$(svn status 2>/dev/null)
if [ $s ]; then
echo $ZSH_THEME_SCM_PROMPT_DIRTY
else
echo $ZSH_THEME_SCM_PROMPT_CLEAN
fi
fi
}
ZSH_THEME_SVN_NAME="svn"

View file

@ -0,0 +1,11 @@
# the svn plugin has to be activated for this to work.
PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(get_scm_prompt)%{$reset_color%}'
ZSH_THEME_SCM_PROMPT_PREFIX=":(%{$fg[red]%}"
ZSH_THEME_SCM_PROMPT_SUFFIX=" %{$reset_color%}"
ZSH_THEME_SCM_PROMPT_DIRTY="%{$fg_bold[blue]%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_SCM_PROMPT_CLEAN="%{$fg_bold[blue]%})%{$reset_color%}"
ZSH_THEME_SCM_DISPLAY_NAME=1