Improve zsh completions

This commit is contained in:
Logicer 2024-09-24 23:23:25 +10:00
parent 09b9d8231e
commit a82b472a80
No known key found for this signature in database
GPG key ID: 9D2BF0916A992A5E

View file

@ -16,24 +16,24 @@ _mas() {
;; ;;
home|info|open|vendor) home|info|open|vendor)
_arguments -C \ _arguments -C \
'1:availableIDs:__mas_list_available_ids' \ '1:availableIDs:__mas_list_installed_ids' \
&& ret=0 && ret=0
;; ;;
install) install)
_arguments -C \ _arguments -C \
'--force[Force reinstall]' \ '--force[Force reinstall]' \
'*:availableIDs:__mas_list_available_ids' \ '*:availableIDs:' \
&& ret=0 && ret=0
;; ;;
purchase) purchase)
_arguments -C \ _arguments -C \
'*:availableIDs:__mas_list_available_ids' \ '*:availableIDs:' \
&& ret=0 && ret=0
;; ;;
lucky) lucky)
_arguments -C \ _arguments -C \
'--force[Force reinstall]' \ '--force[Force reinstall]' \
'1:name:__mas_list_available' \ '1:name:__mas_list_available_names' \
&& ret=0 && ret=0
;; ;;
reset) reset)
@ -44,7 +44,7 @@ _mas() {
search) search)
_arguments -C \ _arguments -C \
'--price[Show price of found apps]' \ '--price[Show price of found apps]' \
'1:name:__mas_list_available' \ '1:name:__mas_list_available_names' \
&& ret=0 && ret=0
;; ;;
signin) signin)
@ -103,58 +103,66 @@ __mas_subcommands() {
_describe -t commands 'command' commands "$@" _describe -t commands 'command' commands "$@"
} }
__mas_list_available_ids() { __mas_list_available_names() {
_alternative \ _alternative \
'searchedIDs:apps:__mas_search_ids' \
'installedIDs:apps:__mas_list_installed_ids'
}
__mas_list_available() {
_alternative \
'ids:apps:__mas_list_available_ids' \
'searchedNames:apps:__mas_search_names' \ 'searchedNames:apps:__mas_search_names' \
'installedNames:apps:__mas_list_installed_names' 'installedNames:apps:__mas_list_installed_names'
} }
__mas_search_ids() { # Autocomplete for the names returned by searching for the current word
[[ -z "$words[-1]" ]] && return
local -a searchIDs=(${(f)"$(__mas_filter_ids "$(mas search $words[-1] 2>/dev/null)")"})
_describe -t searchIDs 'search' searchIDs "$@"
}
__mas_search_names() { __mas_search_names() {
[[ -z "$words[-1]" ]] && return # Don't search if no query has been entered
[[ -z "${(Q)words[-1]}" ]] && return
local -a searchNames=(${(f)"$(__mas_filter_names "$(mas search $words[-1] 2>/dev/null)")"}) local -a searchNames=("${(f)"$(__mas_filter_names "$(mas search ${(Q)words[-1]} 2>/dev/null)")"}")
_describe -t searchNames 'search' searchNames "$@" _describe -t searchNames 'search' searchNames "$@"
} }
__mas_list_outdated() { # Autocomplete for the names of installed apps
local -a outdated=(${(f)"$(__mas_filter_ids "$(mas outdated 2>/dev/null)")"} ${(f)"$(__mas_filter_names "$(mas outdated 2>/dev/null)")"})
_describe -t outdated 'outdated' outdated "$@"
}
__mas_list_installed_ids() {
local -a installedIDs=(${(f)"$(__mas_filter_ids "$(mas list 2>/dev/null)")"})
_describe -t installedIDs 'installed' installedIDs "$@"
}
__mas_list_installed_names() { __mas_list_installed_names() {
local -a installedNames=(${(f)"$(__mas_filter_names "$(mas list 2>/dev/null)")"}) local -a installedNames=("${(f)"$(__mas_filter_names "$(mas list 2>/dev/null)")"}")
_describe -t installedNames 'installed' installedNames "$@" _describe -t installedNames 'installed' installedNames "$@"
} }
# Autocomplete for the ids of installed apps
__mas_list_installed_ids() {
local -A installedApps=("${(f)"$(__mas_filter_descriptive_ids "$(mas list 2>/dev/null)")"}")
local -a installedIDs=("${(f)"$(printf '%s:%s\n' "${(@kv)installedApps}")"}")
_describe -t installedIDs 'installed' installedIDs "$@"
}
# Autocomplete for the ids or names of installed apps
__mas_list_outdated() {
local -A unfilteredOutdated=(
"${(f)"$(__mas_filter_descriptive_ids "$(mas outdated 2>/dev/null)")"}"
)
# Exclude apps which have already been stated
local -a previousApps=(${(Q@)words:1})
local -a outdated=()
for id name in ${(kv)unfilteredOutdated}; do
local -a searchValues=( "$id" "$name" )
[[ ${#previousApps:*searchValues} == 0 ]] && outdated+=( "$id:$name" "$name" )
done
_describe -t outdated 'outdated' outdated "$@"
}
# Extract app names
__mas_filter_names() { __mas_filter_names() {
__mas_strip_price "$1" | sed -nEe 's/^[[:space:]]*[0-9\-]+[[:space:]]+//pg' local -A apps=("${(f)$(__mas_filter_descriptive_ids "$1")}")
printf "${(F)apps}"
} }
__mas_filter_ids() { # Extract app ids as an alternating, new-line seperated list of id and name, designed to be used to create an associative array.
__mas_strip_price "$1" | sed -nEe 's/^[[:space:]]*([0-9\-]+)[[:space:]]*(.*)/\1:\2/pg' __mas_filter_descriptive_ids() {
printf ${(QF)${(*fqqq)"$(__mas_isolate_id_and_name "$1")"}/ ##/\\n}
} }
__mas_strip_price() { # Remove leading spaces and the trailing version from the list of apps returned by mas
echo "$1" | sed -nEe 's/[[:space:]]+\(.*\).*$//pg' | sed 's/:/\\:/g' __mas_isolate_id_and_name() {
printf ${(*F)${${(*@)${(f)1}%% ##\([^(]##}//:/\\:}## ##}
} }
if [[ $zsh_eval_context[-1] == loadautofunc ]]; then if [[ $zsh_eval_context[-1] == loadautofunc ]]; then