From 5f98197b1cbeb054484099265fb4c0c6b609e39e Mon Sep 17 00:00:00 2001 From: shenmo Date: Sun, 15 Mar 2026 12:05:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20apm=20launch=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E8=87=AA=E5=8A=A8=E8=A1=A5=E5=85=A8=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/usr/share/bash-completion/completions/apm | 106 +++++++++++++----- 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/src/usr/share/bash-completion/completions/apm b/src/usr/share/bash-completion/completions/apm index 237f5e2..1c63639 100755 --- a/src/usr/share/bash-completion/completions/apm +++ b/src/usr/share/bash-completion/completions/apm @@ -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 \ No newline at end of file