Skip to content

Commit 631cf35

Browse files
committed
feat(completion): add bash and zsh completions
1 parent 24eeddc commit 631cf35

5 files changed

Lines changed: 484 additions & 1 deletion

File tree

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mark_install_plan || true
3030
mark_install_port_plan || true
3131

3232
install_clashctl_entry
33+
install_clashctl_completion
3334
install_shell_alias_entry
3435
install_runtime_entry
3536
install_local_dashboard_assets

scripts/core/clashctl.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PROJECT_DIR="$(cd "$(dirname "$SCRIPT_PATH")/../.." && pwd)"
77
source "$PROJECT_DIR/scripts/core/common.sh"
88
source "$PROJECT_DIR/scripts/core/runtime.sh"
99
source "$PROJECT_DIR/scripts/core/config.sh"
10+
source "$PROJECT_DIR/scripts/core/completion.sh"
1011
source "$PROJECT_DIR/scripts/core/proxy.sh"
1112
source "$PROJECT_DIR/scripts/core/update.sh"
1213
source "$PROJECT_DIR/scripts/init/systemd.sh"
@@ -42,6 +43,7 @@ Usage:
4243
status 🔍️ 查看状态总览
4344
boot 🚦 管理开机代理接管
4445
log/logs 📜 查看日志
46+
completion 💡 导出 Bash / Zsh 补全脚本
4547
4648
💡 更多高级能力:clashctl help advanced
4749
EOF
@@ -71,6 +73,7 @@ usage_advanced() {
7173
boot proxy on|off|status 📜 仅管理开机代理保持
7274
upgrade 🚀 升级当前或指定内核
7375
update 🔄 更新项目代码
76+
completion bash|zsh 💡 导出 Shell 补全脚本
7477
dev reset 🧪 恢复到安装前状态(保留项目目录和已下载文件)
7578
7679
📌 Advanced Examples:
@@ -2976,6 +2979,7 @@ cmd_ui_help_summary() {
29762979
printf ' %-18s %s\n' "clashctl sub" "🧩 订阅高级管理(启用 / 禁用 / 重命名 / 删除)"
29772980
printf ' %-18s %s\n' "clashctl upgrade" "🚀 升级当前或指定内核"
29782981
printf ' %-18s %s\n' "clashctl update" "🔄 更新项目代码"
2982+
printf ' %-18s %s\n' "clashctl completion" "💡 导出 Bash / Zsh 补全脚本"
29792983
echo "📜 日志"
29802984
printf ' %-18s %s\n' "clashctl doctor" "🩺 诊断面板"
29812985
printf ' %-18s %s\n' "clashctl log/logs" "📜 查看日志"
@@ -7275,6 +7279,7 @@ case "$cmd" in
72757279
proxy) cmd_proxy "$@" ;;
72767280
upgrade) cmd_upgrade "$@" ;;
72777281
update) cmd_update "$@" ;;
7282+
completion) cmd_completion "$@" ;;
72787283
start-direct) cmd_start_direct "$@" ;;
72797284
stop-direct) cmd_stop_direct "$@" ;;
72807285
restart-direct) cmd_restart_direct "$@" ;;

scripts/core/common.sh

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,30 @@ alias_source_file() {
23072307
echo "$PROJECT_DIR/scripts/core/alias.sh"
23082308
}
23092309

2310+
completion_dir() {
2311+
if [ "$INSTALL_SCOPE" = "system" ]; then
2312+
echo "/etc/profile.d"
2313+
else
2314+
echo "$HOME/.config/clash-for-linux"
2315+
fi
2316+
}
2317+
2318+
bash_completion_entry_file() {
2319+
if [ "$INSTALL_SCOPE" = "system" ]; then
2320+
echo "$(completion_dir)/clash-for-linux-completion.bash"
2321+
else
2322+
echo "$(completion_dir)/completion.bash"
2323+
fi
2324+
}
2325+
2326+
zsh_completion_entry_file() {
2327+
if [ "$INSTALL_SCOPE" = "system" ]; then
2328+
echo "$(completion_dir)/clash-for-linux-completion.zsh"
2329+
else
2330+
echo "$(completion_dir)/completion.zsh"
2331+
fi
2332+
}
2333+
23102334
clashctl_entry_target() {
23112335
echo "$(command_install_dir)/clashctl"
23122336
}
@@ -2411,12 +2435,14 @@ cleanup_legacy_shell_entries() {
24112435
}
24122436

24132437
install_shell_alias_entry() {
2414-
local profile_file alias_file shell_rc
2438+
local profile_file alias_file shell_rc bash_completion_file zsh_completion_file
24152439

24162440
cleanup_legacy_shell_entries
24172441

24182442
profile_file="$(profile_entry_file)"
24192443
alias_file="$(alias_source_file)"
2444+
bash_completion_file="$(bash_completion_entry_file)"
2445+
zsh_completion_file="$(zsh_completion_entry_file)"
24202446

24212447
mkdir -p "$(dirname "$profile_file")"
24222448
[ -f "$alias_file" ] || die "未找到 alias 脚本:$alias_file"
@@ -2430,6 +2456,18 @@ if [ -n "\${BASH_VERSION:-}" ] && [ -z "\${CLASH_FOR_LINUX_SHELL_LOADED:-}" ]; t
24302456
export CLASH_FOR_LINUX_SHELL_LOADED="1"
24312457
source "$alias_file"
24322458
fi
2459+
2460+
case "\$-" in
2461+
*i*)
2462+
if [ -n "\${BASH_VERSION:-}" ] && [ -z "\${CLASH_FOR_LINUX_BASH_COMPLETION_LOADED:-}" ] && [ -f "$bash_completion_file" ]; then
2463+
export CLASH_FOR_LINUX_BASH_COMPLETION_LOADED="1"
2464+
source "$bash_completion_file"
2465+
elif [ -n "\${ZSH_VERSION:-}" ] && [ -z "\${CLASH_FOR_LINUX_ZSH_COMPLETION_LOADED:-}" ] && [ -f "$zsh_completion_file" ]; then
2466+
export CLASH_FOR_LINUX_ZSH_COMPLETION_LOADED="1"
2467+
source "$zsh_completion_file"
2468+
fi
2469+
;;
2470+
esac
24332471
EOF
24342472
chmod +x "$profile_file"
24352473

@@ -2440,6 +2478,21 @@ EOF
24402478
install_alias_command_wrappers
24412479
}
24422480

2481+
install_clashctl_completion() {
2482+
local bash_target zsh_target
2483+
2484+
bash_target="$(bash_completion_entry_file)"
2485+
zsh_target="$(zsh_completion_entry_file)"
2486+
2487+
mkdir -p "$(dirname "$bash_target")"
2488+
mkdir -p "$(dirname "$zsh_target")"
2489+
2490+
bash "$PROJECT_DIR/scripts/core/clashctl.sh" completion bash > "$bash_target"
2491+
bash "$PROJECT_DIR/scripts/core/clashctl.sh" completion zsh > "$zsh_target"
2492+
2493+
chmod +x "$bash_target" "$zsh_target" 2>/dev/null || true
2494+
}
2495+
24432496
remove_clashctl_entry() {
24442497
rm -f "$(clashctl_entry_target)" "$(clashctl_bin_entry_target)" 2>/dev/null || true
24452498
remove_alias_command_wrappers
@@ -2461,6 +2514,10 @@ remove_shell_alias_entry() {
24612514
fi
24622515
}
24632516

2517+
remove_clashctl_completion() {
2518+
rm -f "$(bash_completion_entry_file)" "$(zsh_completion_entry_file)" 2>/dev/null || true
2519+
}
2520+
24642521
install_has_subscription() {
24652522
[ -n "$(subscription_url 2>/dev/null || true)" ]
24662523
}

0 commit comments

Comments
 (0)