jdx / usage

A specification for CLIs
https://usage.jdx.dev
MIT License
157 stars 6 forks source link

generated zsh completion does not define `$spec` #156

Closed powerman closed 3 weeks ago

powerman commented 3 weeks ago
$ usage --version
usage-cli 1.1.1

$ usage g c zsh usage | grep spec
  local curcontext="$curcontext" spec cache_policy
  _arguments "*: :(($(usage complete-word --shell zsh -s "$spec" -- "${words[@]}" )))"

$ diff -u <(usage g c zsh usage) <(usage --completions zsh)
--- /proc/self/fd/12    2024-11-05 08:50:54.673650113 +0200
+++ /proc/self/fd/14    2024-11-05 08:50:54.673650113 +0200
@@ -1,6 +1,16 @@
 #compdef usage
 local curcontext="$curcontext"

+# caching config
+_usage_usage_cache_policy() {
+  if [[ -z "${lifetime}" ]]; then
+    lifetime=$((60*60*4)) # 4 hours
+  fi
+  local -a oldp
+  oldp=( "$1"(Nms+${lifetime}) )
+  (( $#oldp ))
+}
+
 _usage() {
   typeset -A opt_args
   local curcontext="$curcontext" spec cache_policy
@@ -12,6 +22,18 @@
       return 1
   fi

+  zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
+  if [[ -z $cache_policy ]]; then
+    zstyle ":completion:${curcontext}:" cache-policy _usage_usage_cache_policy
+  fi
+
+  if ( [[ -z "${_usage_usage_spec:-}" ]] || _cache_invalid _usage_usage_spec ) \
+      && ! _retrieve_cache _usage_usage_spec;
+  then
+    spec="$(usage --usage-spec)"
+    _store_cache _usage_usage_spec spec
+  fi
+
   _arguments "*: :(($(usage complete-word --shell zsh -s "$spec" -- "${words[@]}" )))"
   return 0
 }
jdx commented 3 weeks ago

https://github.com/jdx/usage/commit/3cae2ae4a1ad6a97358bb49d9d0f3e15c65feb40