Add hg root shell implementation (__hg_fast_root)
authorMikael Berthe <mikael@lilotux.net>
Sun, 15 Jul 2012 16:30:45 +0200
changeset 15 5f43e2cd8ebe
parent 14 77db3f389db7
child 16 be9820f70412
Add hg root shell implementation (__hg_fast_root)
zshrc.d/20-prompt.zrc
--- a/zshrc.d/20-prompt.zrc	Sun Jul 15 15:58:14 2012 +0200
+++ b/zshrc.d/20-prompt.zrc	Sun Jul 15 16:30:45 2012 +0200
@@ -1,9 +1,4 @@
 
-# You can use fast_hg_root from https://bitbucket.org/yaniv_aknin/fast_hg_root
-# or maybe use the command false if you don't use Mercurial at all.
-# (Override in $ZDOTLOCALDIR/zshrc)
-HG_ROOT_CMD=("hg" "root")
-export HG_ROOT_CMD
 
 
 # Enable my Vim-like mode for supported terms
@@ -16,6 +11,32 @@
 fi
 
 
+# __hg_fast_root()
+# My shell implementation of fast_hg_root.
+# Please let me now if it can be improved.
+# Mikael
+function __hg_fast_root() {
+    local checkdir=$PWD
+    local hgdir=".hg"
+    while [[ ${checkdir} != '/' ]]; do
+        [[ -r ${checkdir} ]] || return 1
+        [[ -d ${checkdir}/${hgdir} ]] && return 0
+        checkdir=${checkdir}/..
+        checkdir="${checkdir:A}"
+    done
+    #[[ ${checkdir} == '/' ]] && return 1
+    return 1
+}
+
+# You can use fast_hg_root from https://bitbucket.org/yaniv_aknin/fast_hg_root
+# or maybe use the command false if you don't use Mercurial at all.
+# You can also use the actual hg command: HG_ROOT_CMD=("hg" "root")
+# ... but it will be slow
+# (Override in $ZDOTLOCALDIR/zshrc)
+HG_ROOT_CMD=(__hg_fast_root)
+export HG_ROOT_CMD
+
+
 ### Extended prompts
 
 function __prompt_path_color {
@@ -32,8 +53,8 @@
 
 # From http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
 function __prompt_char {
+    $HG_ROOT_CMD >/dev/null 2>/dev/null && echo '☿' && return
     __gitdir >/dev/null 2>/dev/null && echo '±' && return
-    $HG_ROOT_CMD >/dev/null 2>/dev/null && echo '☿' && return
     echo '○'
 }