zsh-workers
 help / color / mirror / code / Atom feed
From: doron.behar@gmail.com
To: zsh-workers@zsh.org
Subject: [PATCH 24/25] Consider `--tree` when searching installed rocks.
Date: Sat, 26 May 2018 18:06:33 +0300	[thread overview]
Message-ID: <20180526150634.15683-25-doron.behar@gmail.com> (raw)
In-Reply-To: <20180526150634.15683-1-doron.behar@gmail.com>

From: Doron Behar <doron.behar@gmail.com>

---
 Completion/Unix/Command/_luarocks | 126 +++++++++++++++++++++---------
 1 file changed, 91 insertions(+), 35 deletions(-)

diff --git a/Completion/Unix/Command/_luarocks b/Completion/Unix/Command/_luarocks
index 8de95c188..17fa8248e 100644
--- a/Completion/Unix/Command/_luarocks
+++ b/Completion/Unix/Command/_luarocks
@@ -59,6 +59,7 @@ __luarocks_rock_version(){
           # TODO: actually complete versions of installed rocks using the cache
           # How does luarocks handles multiple versions of the same package?
           # If anybody knows, please write something beautiful here
+          # TODO: get somehow from $@ the option given (if used) to --tree
           _message -e "version for installed rock ${words[$i]}"
           return
           ;;
@@ -107,56 +108,95 @@ ___luarocks_installed_rocks_cache_policy(){
 # {{{ helper: installed rocks
 (( $+functions[__luarocks_installed_rocks] )) ||
 __luarocks_installed_rocks(){
-  local update_policy
-  zstyle -s ":completion:${curcontext}:" cache-policy update_policy
-  if [[ -z "$update_policy" ]]; then
-    zstyle ":completion:${curcontext}:" cache-policy ___luarocks_installed_rocks_cache_policy
-  fi
-  if _cache_invalid luarocks_installed_list; then
-    rocks_list=($(luarocks list --porcelain))
-    _store_cache luarocks_installed_list rocks_list
+  # This function optionally recieves one argument of the tree in which
+  # installed rocks are searched for. If this argument is used, the installed
+  # rocks which will be completed by this function will not use the cache which
+  # is valid only for installed rocks on default trees like /usr/lib/luarocks
+  # and ~/.luarocks
+  local tree="$1"
+  if [[ -z ${tree} ]]; then
+    local update_policy
+    zstyle -s ":completion:${curcontext}:" cache-policy update_policy
+    if [[ -z "$update_policy" ]]; then
+      zstyle ":completion:${curcontext}:" cache-policy ___luarocks_installed_rocks_cache_policy
+    fi
+    if _cache_invalid luarocks_installed_list; then
+      rocks_list=($(luarocks list --porcelain))
+      _store_cache luarocks_installed_list rocks_list
+    else
+      _retrieve_cache luarocks_installed_list
+    fi
+    if [[ -z ${rocks_list} ]]; then
+      _message -r "no installed rocks"
+      return
+    fi
+    if _cache_invalid luarocks_installed_names; then
+      rocks_names=()
+      for i in {1.."${#rocks_list[@]}"..4}; do
+        rocks_names+=("${rocks_list[$i]}")
+      done
+      _store_cache luarocks_installed_names rocks_names
+    else
+      _retrieve_cache luarocks_installed_names
+    fi
+    if _cache_invalid luarocks_installed_versions; then
+      rocks_versions=()
+      for i in {2.."${#rocks_list[@]}"..4}; do
+        rocks_versions+=("${rocks_list[$i]}")
+      done
+      _store_cache luarocks_installed_versions rocks_versions
+    else
+      _retrieve_cache luarocks_installed_versions
+    fi
+    if _cache_invalid luarocks_installed_descriptions; then
+      rocks_descriptions=()
+      for i in {1.."${#rocks_names[@]}"}; do
+        name_version_description="$(luarocks show ${rocks_names[$i]} 2>/dev/null | head -2 | tail -1)"
+        total_length=${#name_version_description}
+        garbage_length="$((${#rocks_names[$i]} + ${#rocks_versions[$i]} + 5))"
+        description="${name_version_description[${garbage_length},${total_length}]}"
+        rocks_descriptions+=("${description}")
+      done
+      _store_cache luarocks_installed_descriptions rocks_descriptions
+    else
+      _retrieve_cache luarocks_installed_descriptions
+    fi
+    if _cache_invalid luarocks_installed_names_and_descriptions; then
+      rocks_names_and_descriptions=()
+      for i in {1.."${#rocks_names[@]}"}; do
+        name_and_description=${rocks_names[$i]}:${rocks_descriptions[$i]}
+        rocks_names_and_descriptions+=(${name_and_description})
+      done
+    else
+      _store_cache luarocks_installed_names_and_descriptions rocks_names_and_descriptions
+    fi
   else
-    _retrieve_cache luarocks_installed_list
-  fi
-  if _cache_invalid luarocks_installed_names; then
+    rocks_list=($(luarocks --tree="${tree}" list --porcelain 2> /dev/null))
+    if [[ -z ${rocks_list} ]]; then
+      _message "no installed rocks in the specified tree"
+      return
+    fi
     rocks_names=()
     for i in {1.."${#rocks_list[@]}"..4}; do
       rocks_names+=("${rocks_list[$i]}")
     done
-    _store_cache luarocks_installed_names rocks_names
-  else
-    _retrieve_cache luarocks_installed_names
-  fi
-  if _cache_invalid luarocks_installed_versions; then
     rocks_versions=()
     for i in {2.."${#rocks_list[@]}"..4}; do
       rocks_versions+=("${rocks_list[$i]}")
     done
-    _store_cache luarocks_installed_versions rocks_versions
-  else
-    _retrieve_cache luarocks_installed_versions
-  fi
-  if _cache_invalid luarocks_installed_descriptions; then
     rocks_descriptions=()
     for i in {1.."${#rocks_names[@]}"}; do
-      name_version_description="$(luarocks show ${rocks_names[$i]} | head -2 | tail -1)"
+      name_version_description="$(luarocks show ${rocks_names[$i]} 2> /dev/null | head -2 | tail -1)"
       total_length=${#name_version_description}
       garbage_length="$((${#rocks_names[$i]} + ${#rocks_versions[$i]} + 5))"
       description="${name_version_description[${garbage_length},${total_length}]}"
       rocks_descriptions+=("${description}")
     done
-    _store_cache luarocks_installed_descriptions rocks_descriptions
-  else
-    _retrieve_cache luarocks_installed_descriptions
-  fi
-  if _cache_invalid luarocks_installed_names_and_descriptions; then
     rocks_names_and_descriptions=()
     for i in {1.."${#rocks_names[@]}"}; do
       name_and_description=${rocks_names[$i]}:${rocks_descriptions[$i]}
       rocks_names_and_descriptions+=(${name_and_description})
     done
-  else
-    _store_cache luarocks_installed_names_and_descriptions rocks_names_and_descriptions
   fi
   _describe 'installed rocks' rocks_names_and_descriptions
 }
@@ -169,21 +209,37 @@ __luarocks_installed_rocks(){
 (( $+functions[__luarocks_rock] )) ||
 __luarocks_rock(){
   local -a alts=()
-  for arg in "$@"; do
-    case $arg in
+  while [[ $# -gt 0 ]]; do
+    arg="$1"
+    case "$arg" in
       (rockspec)
         alts+=(':rock file:{_files -g "*.rockspec"}')
+        shift 1
+        continue
         ;;
       (rockpack)
         alts+=(':rock file:{_files -g "*.src.rock"}')
+        shift 1
+        continue
         ;;
       (external)
         alts+=(':external rock:')
+        shift 1
+        continue
         ;;
       (installed)
-        alts+=(':local rock:__luarocks_installed_rocks')
+        tree="$2"
+        alts+=(":local rock:{__luarocks_installed_rocks ${tree}}")
+        if [[ -z "${tree}" ]]; then
+          shift
+        else
+          shift 2
+        fi
+        continue
         ;;
     esac
+    shift
+    continue
   done
   _alternative ${alts[@]}
 }
@@ -250,7 +306,7 @@ local doc_command_options=(
 _luarocks_doc(){
   _arguments \
     "${doc_command_options[@]}" \
-    '1: :{__luarocks_rock "installed"}'
+    '1: :{__luarocks_rock "installed" '"${opt_args[--tree]}"'}'
 }
 # }}}
 # {{{ `download` command
@@ -383,7 +439,7 @@ local remove_command_options=(
 _luarocks_remove(){
   _arguments -A "-*" \
     "${remove_command_options[@]}" \
-    '1: :{__luarocks_rock "installed"}' \
+    '1: :{__luarocks_rock "installed" '"${opt_args[--tree]}"'}' \
     '2:: :{__luarocks_rock_version "installed"}'
 }
 # }}}
@@ -418,7 +474,7 @@ local show_command_options=(
 _luarocks_show(){
   _arguments \
     "${show_command_options[@]}" \
-    '1: :{__luarocks_rock "installed"}'
+    "1: :{__luarocks_rock 'installed' "${opt_args[--tree]}"}"
 }
 # }}}
 # {{{ `unpack` command
-- 
2.17.0


  parent reply	other threads:[~2018-05-26 15:07 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-26 15:06 [PATCH 00/25] *** Add completion for luarocks *** doron.behar
2018-05-26 15:06 ` [PATCH 01/25] Add variables for all commands and options doron.behar
2018-05-26 15:06 ` [PATCH 02/25] Remove architecture related option completion doron.behar
2018-05-26 15:06 ` [PATCH 03/25] Add marker style comments doron.behar
2018-05-26 15:06 ` [PATCH 04/25] Remove variables and use their contents directly doron.behar
2018-05-26 15:06 ` [PATCH 05/25] Add curcontext case for every subcommand doron.behar
2018-05-26 15:06 ` [PATCH 06/25] Use better naming scheme for common helpers doron.behar
2018-05-26 15:06 ` [PATCH 07/25] Write better sub commands comments doron.behar
2018-05-26 15:06 ` [PATCH 08/25] Add helpers section doron.behar
2018-05-26 15:06 ` [PATCH 09/25] Make *all* helpers functions begin with __luarocks doron.behar
2018-05-26 15:06 ` [PATCH 10/25] Write all simple sub commands completions doron.behar
2018-05-26 15:06 ` [PATCH 11/25] General internal conventions sync doron.behar
2018-05-26 15:06 ` [PATCH 12/25] Finish helper `__luarocks_lua_versions` doron.behar
2018-05-26 15:06 ` [PATCH 13/25] General cleanup doron.behar
2018-05-26 15:06 ` [PATCH 14/25] Finish `_luarocks_doc` and `_luarocks_config` doron.behar
2018-05-26 15:06 ` [PATCH 15/25] Expand __luarocks_rock_version so it accpets args doron.behar
2018-05-26 15:06 ` [PATCH 16/25] Finish completions for purge and new_version doron.behar
2018-05-26 15:06 ` [PATCH 17/25] Write a better comment for last TODO doron.behar
2018-05-26 15:06 ` [PATCH 18/25] Make cache policy function safer doron.behar
2018-05-26 15:06 ` [PATCH 19/25] Fix git tag completion by autoloading _git doron.behar
2018-05-26 15:06 ` [PATCH 20/25] Use a generic sub command completer doron.behar
2018-05-26 15:06 ` [PATCH 21/25] Use 2 spaces instead of tabs doron.behar
2018-05-26 15:06 ` [PATCH 22/25] Use +functions[] for all helpers doron.behar
2018-05-26 15:06 ` [PATCH 23/25] Improve `___luarocks_installed_rocks_cache_policy` doron.behar
2018-05-26 15:06 ` doron.behar [this message]
2018-05-26 15:06 ` [PATCH 25/25] Consider `--tree` in versions completion doron.behar
2018-05-26 15:37 ` [PATCH 00/25] *** Add completion for luarocks *** Eitan Adler
2018-05-26 16:09   ` Doron Behar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180526150634.15683-25-doron.behar@gmail.com \
    --to=doron.behar@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).