changeset 3600:932dadd2e614

zsh: add optional remote directory cache
author Brendan Cully <brendan@kublai.com>
date Tue, 31 Oct 2006 15:44:16 -0800
parents e00920b4f1cb
children cedf056bb723
files contrib/zsh_completion
diffstat 1 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/zsh_completion	Tue Oct 31 23:56:52 2006 +0100
+++ b/contrib/zsh_completion	Tue Oct 31 15:44:16 2006 -0800
@@ -76,9 +76,17 @@
     fi
   fi
 
+  curcontext="${curcontext%:*:*}:hg-${cmd}:"
+
+  zstyle -s ":completion:$curcontext:" cache-policy update_policy
+
+  if [[ -z "$update_policy" ]]
+  then
+    zstyle ":completion:$curcontext:" cache-policy _hg_cache_policy
+  fi
+
   if (( $+functions[_hg_cmd_${cmd}] ))
   then
-    curcontext="${curcontext%:*:*}:hg-${cmd}:"
     _hg_cmd_${cmd}
   else
     # complete unknown commands normally
@@ -87,6 +95,16 @@
   fi
 }
 
+_hg_cache_policy() {
+  typeset -a old
+
+  # cache for a minute
+  old=( "$1"(mm+10) )
+  (( $#old )) && return 0
+
+  return 1
+}
+
 _hg_get_commands() {
   typeset -ga _hg_cmd_list
   typeset -gA _hg_alias_list
@@ -172,8 +190,13 @@
       typeset -a remdirs
       compset -p $(( $#host + 1 ))
       local rempath=${(M)PREFIX##*/}
+      local cacheid="hg_${host}_${rempath//\//_}"
       compset -P '*/'
-      remdirs=(${${(M)${(f)"$(_call_program files ssh -a -x $host ls -1FL "${(q)rempath}" 2> /dev/null)"}##*/}%/})
+      if _cache_invalid "$cacheid" || ! _retrieve_cache "$cacheid"
+      then
+        remdirs=(${${(M)${(f)"$(_call_program files ssh -a -x $host ls -1FL "${(q)rempath}" 2> /dev/null)"}##*/}%/})
+        _store_cache "$cacheid" remdirs
+      fi
       _describe -t directories 'remote directory' remdirs -S/
     else
       _message 'remote directory'