Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH 10/25] Write all simple sub commands completions.



From: Doron Behar <doron.behar@xxxxxxxxx>

Most importantly, write (using `_cache_` functions) helpers that
complete installed rocks with their descriptions generated with
`luarocks show <>`.
---
 Completion/Unix/Command/_luarocks | 125 ++++++++++++++++++++++++++++--
 1 file changed, 119 insertions(+), 6 deletions(-)

diff --git a/Completion/Unix/Command/_luarocks b/Completion/Unix/Command/_luarocks
index de296a084..4f3271360 100644
--- a/Completion/Unix/Command/_luarocks
+++ b/Completion/Unix/Command/_luarocks
@@ -59,20 +59,102 @@ __luarocks_lua_version(){
 	_values -s , 
 }
 # }}}
-# {{{ helper: rockspec file / rockpack (.src.rock file) / external according to demand
+# {{{ helper: installed rocks cache policy
+___luarocks_installed_rocks_cache_policy(){
+	# the number of seconds since 1970-01-01 the manifest file was modified
+	local manifest_last_date_modified="$(date -r ~/.luarocks/lib/luarocks/rocks-5.3/manifest +%s 2>/dev/null)"
+	# the number of seconds since 1970-01-01 the cache file was modified
+	local cache_last_date_modified="$(date -r $1 +%s 2>/dev/null)"
+	if [[ ! -z ${cache_last_date_modified} && ! -z ${manifest_last_date_modified} ]]; then
+		# if the manifest file is newer then the cache:
+		if [ ${manifest_last_date_modified} -ge ${cache_last_date_modified} ]; then
+			(( 1 ))
+		else
+			(( 0 ))
+		fi
+	fi
+}
+# }}}
+# {{{ helper: installed rocks
+__luarocks_installed_rocks(){
+	local update_policy ret=1
+	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 _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]} | 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
+}
+# }}}
+# {{{ helper: rocks wrapper
+# Used to complete one or more of the followings:
+# - .rockspec file
+# - .src.rock file
+# - external rock
 __luarocks_rock(){
-	local -a arguments=()
+	local -a alts=()
 	for arg in "$@"; do
 		case $arg in
-			(file)
-				arguments+=(':rock file:{_files -g "*.rockspec"}')
+			(rockspec)
+				alts+=(':rock file:{_files -g "*.rockspec"}')
+				;;
+			(rockpack)
+				alts+=(':rock file:{_files -g "*.src.rock"}')
 				;;
 			(external)
-				arguments+=(':external rock:')
+				alts+=(':external rock:')
+				;;
+			(installed)
+				alts+=(':local rock:__luarocks_installed_rocks')
 				;;
 		esac
 	done
-	_alternative ${arguments[@]}
+	_alternative ${alts[@]}
 }
 # }}}
 
@@ -91,6 +173,10 @@ local build_command_options=(
 	$option_deps_mode
 )
 _luarocks_build(){
+	_arguments -A "-*" \
+		"${build_command_options[@]}" \
+		'1: :{__luarocks_rock "rockspec" "external"}' \
+		'2:: :__luarocks_rock_version'
 }
 # }}}
 # {{{ `config` command
@@ -127,12 +213,17 @@ local download_command_options=(
 	'--arch=[Download rock for a specific architecture]:ARCH:'
 )
 _luarocks_download(){
+	_arguments -A "-*" \
+		"${download_command_options[@]}" \
+		'1: :{__luarocks_rock "external"}' \
+		'2:: :__luarocks_rock_version'
 }
 # }}}
 # {{{ `help` command
 # arguments:
 # must: luarocks sub command
 _luarocks_help(){
+	_arguments '1: :__luarocks_command'
 }
 # }}}
 # {{{ `install` command
@@ -141,12 +232,14 @@ _luarocks_help(){
 # - optional: version
 # NOTE: it receives the same argument as the build command and it accepts the same options as well
 _luarocks_install(){
+	_luarocks_build
 }
 # }}}
 # {{{ `lint` command
 # arguments:
 # must: rockspec file (first and last)
 _luarocks_lint(){
+	_arguments '1:ROCKSPEC_FILE:{__luarocks_rock "rockspec"}'
 }
 # }}}
 # {{{ `list` command
@@ -156,6 +249,7 @@ local list_command_options=(
 	'--porcelain[Produce machine-friendly output]'
 )
 _luarocks_list(){
+	_arguments "${list_command_options[@]}"
 }
 # }}}
 # {{{ `make` command
@@ -163,6 +257,7 @@ _luarocks_list(){
 # - optional: rockspec file
 # NOTE: it's options were already described above.
 _luarocks_make(){
+	_arguments '1:: :{__luarocks_rock "rockspec"}'
 }
 # }}}
 # {{{ `new_version` command
@@ -181,6 +276,7 @@ _luarocks_new_version(){
 # - must: .rockspec file / external rock
 # - optional: version
 _luarocks_pack(){
+	_luarocks_build
 }
 # }}}
 # {{{ `path` command
@@ -193,6 +289,7 @@ local path_command_options=(
 	'--lr-bin[Exports the system path (not formatted as shell command)]'
 )
 _luarocks_path(){
+	_arguments "${path_command_options[@]}"
 }
 # }}}
 # {{{ `purge` command
@@ -217,6 +314,10 @@ local remove_command_options=(
 	$option_force_fast
 )
 _luarocks_remove(){
+	_arguments -A "-*" \
+		"${remove_command_options[@]}" \
+		'1: :{__luarocks_rock "installed"}' \
+		'2:: :__luarocks_rock_version'
 }
 # }}}
 # {{{ `search` command
@@ -228,6 +329,9 @@ local search_command_options=(
 	'--all[List all contents of the server that are suitable to this platform, do not filter by name]'
 )
 _luarocks_search(){
+	_arguments \
+		"${search_command_options[@]}" \
+		'*:SEARCH QUERY:'
 }
 # }}}
 # {{{ `show` command
@@ -243,6 +347,9 @@ local show_command_options=(
 	'--rock-dir[data directory of the installed rock]'
 )
 _luarocks_show(){
+	_arguments \
+		"${show_command_options[@]}" \
+		'1: :{__luarocks_rock "installed"}'
 }
 # }}}
 # {{{ `unpack` command
@@ -253,6 +360,9 @@ local unpack_command_options=(
 	'--force[Unpack files even if the output directory already exists]'
 )
 _luarocks_unpack(){
+	_arguments \
+		"${unpack_command_options[@]}" \
+		'1: :{__luarocks_rock "rockpack" "external"}'
 }
 # }}}
 # {{{ `upload` command
@@ -264,6 +374,9 @@ local upload_command_options=(
 	'--force[Replace existing rockspec if the same revision of a module already exists]'
 )
 _luarocks_upload(){
+	_arguments \
+		"${upload_command_options[@]}" \
+		'1: :{__luarocks_rock "rockspec"}'
 }
 # }}}
 # {{{ `write_rockspec` command
-- 
2.17.0



Messages sorted by: Reverse Date, Date, Thread, Author