新增 apm launch 相关自动补全能力

This commit is contained in:
2026-03-15 12:05:06 +08:00
parent 62e91970ed
commit 5f98197b1c

View File

@@ -37,6 +37,7 @@ _apm()
# see if the user selected a command already
local COMMANDS=(
"ssaudit"
"launch"
"list"
"search"
"show" "showsrc"
@@ -185,7 +186,7 @@ find_directories_without_ace_env() {
# 查找所有直接子目录排除包含ace-env子目录的目录
while IFS= read -r -d '' dir; do
if [[ -d "$dir" ]] && [[ ! -d "$dir/files/ace-env" ]]; then
result+=("$(basename $dir)")
result+=("$(basename "$dir")")
fi
done < <(find "$base_dir" -maxdepth 1 -type d ! -path "$base_dir" -print0 2>/dev/null)
@@ -196,52 +197,107 @@ find_directories_without_ace_env() {
fi
return 1
}
function apm_run_compgen(){
result=$(find_directories_without_ace_env "$primary_dir")
if [[ -n "$result" ]]; then
echo "$result"
else
result=$(find_directories_without_ace_env "$fallback_dir")
function apm_run_compgen(){
result=$(find_directories_without_ace_env "$primary_dir")
if [[ -n "$result" ]]; then
echo "$result"
else
echo ""
result=$(find_directories_without_ace_env "$fallback_dir")
if [[ -n "$result" ]]; then
echo "$result"
else
echo ""
fi
fi
fi
}
# 获取当前命令的参数位置
get_arg_position() {
local cmd="$1"
local pos=0
local found_cmd=0
for (( i=1; i < ${#words[@]}; i++ )); do
if [[ $found_cmd -eq 0 ]]; then
if [[ "${words[i]}" == "$cmd" ]]; then
found_cmd=1
fi
else
# 跳过选项参数(以-开头)
if [[ "${words[i]}" != -* ]]; then
((pos++))
fi
fi
done
echo $pos
}
# specific command arguments
if [[ -n $command ]]; then
# 获取参数位置
local arg_pos=$(get_arg_position "$command")
case $command in
remove|purge|autoremove)
# Debian system
COMPREPLY=( $( compgen -W "$(ls /var/lib/apm/apm/files/ace-env/var/lib/apm/ )" $cur ) )
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(ls /var/lib/apm/apm/files/ace-env/var/lib/apm/ )" "$cur" ) )
fi
return 0
;;
show|list|download|changelog|depends|rdepends)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) )
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) )
fi
return 0
;;
install)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) )
if [[ "$cur" == ./* || "$cur" == /* ]]; then
_filedir "deb"
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) )
if [[ "$cur" == ./* || "$cur" == /* ]]; then
_filedir "deb"
fi
fi
return 0
;;
source|build-dep|showsrc|policy)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) $( apt-cache dumpavail -o Dir::Cache="/var/lib/aptss/" | \
command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) )
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) $( apt-cache dumpavail -o Dir::Cache="/var/lib/aptss/" | \
command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) )
fi
return 0
;;
run|sandbox-run|bwrap-run)
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) )
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) )
# 第二个及以后参数匹配文件
elif [[ $arg_pos -ge 2 ]]; then
_filedir
fi
return 0
;;
launch)
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) )
# 第二个及以后参数匹配文件
elif [[ $arg_pos -ge 2 ]]; then
_filedir
fi
return 0
;;
ssaudit)
# ssaudit 命令总是匹配文件
_filedir
return 0
;;
esac
@@ -256,4 +312,4 @@ fi
} &&
complete -F _apm apm
# ex: ts=4 sw=4 et filetype=sh
# ex: ts=4 sw=4 et filetype=sh