From b989ec489cd1b1ae6408bc6a4b2ab030099cf3b5 Mon Sep 17 00:00:00 2001 From: Robin Ramael Date: Mon, 10 Jan 2011 17:11:46 +0100 Subject: [PATCH] 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) --- lib/appearance.zsh | 17 ++++++++++++++ scm/git.scm.zsh | 22 ++++++++++++++++++ scm/svn.scm.zsh | 42 +++++++++++++++++++++++++++++++++++ themes/awesomepanda.zsh-theme | 11 +++++++++ 4 files changed, 92 insertions(+) create mode 100644 scm/git.scm.zsh create mode 100644 scm/svn.scm.zsh create mode 100644 themes/awesomepanda.zsh-theme diff --git a/lib/appearance.zsh b/lib/appearance.zsh index ffee52b5e..1e6c8d1f1 100644 --- a/lib/appearance.zsh +++ b/lib/appearance.zsh @@ -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" \ No newline at end of file diff --git a/scm/git.scm.zsh b/scm/git.scm.zsh new file mode 100644 index 000000000..095f36012 --- /dev/null +++ b/scm/git.scm.zsh @@ -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 +} \ No newline at end of file diff --git a/scm/svn.scm.zsh b/scm/svn.scm.zsh new file mode 100644 index 000000000..1d5410724 --- /dev/null +++ b/scm/svn.scm.zsh @@ -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" \ No newline at end of file diff --git a/themes/awesomepanda.zsh-theme b/themes/awesomepanda.zsh-theme new file mode 100644 index 000000000..e0bb3e73f --- /dev/null +++ b/themes/awesomepanda.zsh-theme @@ -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 +