Skip to content

Commit 3d6a523

Browse files
committed
feat: add single-line progress for subscription add flow
1 parent c7e6421 commit 3d6a523

2 files changed

Lines changed: 76 additions & 10 deletions

File tree

scripts/core/clashctl.sh

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5110,27 +5110,42 @@ tun_runtime_pid() {
51105110

51115111
tun_process_capability_text() {
51125112
local backend="${1:-unknown}"
5113-
local pid cap_eff cap_value net_admin net_raw
5113+
local pid=""
5114+
local cap_eff=""
5115+
local cap_value=0
5116+
local net_admin="no"
5117+
local net_raw="no"
51145118

51155119
pid="$(tun_runtime_pid "$backend" 2>/dev/null || true)"
51165120
if [ -z "${pid:-}" ] || [ ! -r "/proc/$pid/status" ]; then
51175121
echo "未读取到运行中 mihomo 进程能力"
51185122
return 0
51195123
fi
51205124

5121-
cap_eff="$(sed -nE 's/^CapEff:[[:space:]]*([0-9a-fA-F]+)$/\1/p' "/proc/$pid/status" 2>/dev/null | head -n 1)"
5125+
cap_eff="$(
5126+
sed -nE 's/^CapEff:[[:space:]]*([0-9a-fA-F]+)$/\1/p' "/proc/$pid/status" 2>/dev/null \
5127+
| head -n 1
5128+
)"
5129+
51225130
if [ -z "${cap_eff:-}" ]; then
51235131
echo "pid=$pid,未读取到 CapEff"
51245132
return 0
51255133
fi
51265134

5135+
case "$cap_eff" in
5136+
*[!0-9a-fA-F]*)
5137+
echo "pid=$pid,CapEff 格式异常:$cap_eff"
5138+
return 0
5139+
;;
5140+
esac
5141+
51275142
cap_value=$((16#$cap_eff))
5128-
net_admin="no"
5129-
net_raw="no"
5130-
if [ $((cap_value & (1 << 12))) -ne 0 ]; then
5143+
5144+
if (( cap_value & (1 << 12) )); then
51315145
net_admin="yes"
51325146
fi
5133-
if [ $((cap_value & (1 << 13))) -ne 0 ]; then
5147+
5148+
if (( cap_value & (1 << 13) )); then
51345149
net_raw="yes"
51355150
fi
51365151

@@ -5139,16 +5154,29 @@ tun_process_capability_text() {
51395154

51405155
tun_process_has_cap_net_admin() {
51415156
local backend="${1:-unknown}"
5142-
local pid cap_eff cap_value
5157+
local pid=""
5158+
local cap_eff=""
5159+
local cap_value=0
51435160

51445161
pid="$(tun_runtime_pid "$backend" 2>/dev/null || true)"
51455162
[ -n "${pid:-}" ] && [ -r "/proc/$pid/status" ] || return 2
51465163

5147-
cap_eff="$(sed -nE 's/^CapEff:[[:space:]]*([0-9a-fA-F]+)$/\1/p' "/proc/$pid/status" 2>/dev/null | head -n 1)"
5164+
cap_eff="$(
5165+
sed -nE 's/^CapEff:[[:space:]]*([0-9a-fA-F]+)$/\1/p' "/proc/$pid/status" 2>/dev/null \
5166+
| head -n 1
5167+
)"
5168+
51485169
[ -n "${cap_eff:-}" ] || return 2
51495170

5171+
case "$cap_eff" in
5172+
*[!0-9a-fA-F]*)
5173+
return 2
5174+
;;
5175+
esac
5176+
51505177
cap_value=$((16#$cap_eff))
5151-
if [ $((cap_value & (1 << 12))) -ne 0 ]; then
5178+
5179+
if (( cap_value & (1 << 12) )); then
51525180
return 0
51535181
fi
51545182

@@ -5975,9 +6003,20 @@ cmd_add() {
59756003

59766004
sub_fmt="$(detect_subscription_format "$sub_url")"
59776005

6006+
ui_progress_line 5 "正在保存订阅..."
6007+
59786008
set_subscription "$sub_url" "$sub_fmt" "$sub_name" "false"
6009+
6010+
ui_progress_line 65 "正在设为当前订阅..."
6011+
59796012
set_active_subscription "$sub_name"
5980-
apply_runtime_change_after_config_mutation
6013+
6014+
ui_progress_line 85 "正在应用配置..."
6015+
6016+
apply_runtime_change_after_config_mutation >/dev/null
6017+
6018+
ui_progress_done "订阅添加完成"
6019+
59816020
print_add_feedback "$sub_name" "$sub_url"
59826021
cmd_ls
59836022
}

scripts/core/common.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ ui_kv() {
119119
printf '%s %s:%s\n' "$icon" "$key" "$value"
120120
}
121121

122+
ui_progress_line() {
123+
local percent="$1"
124+
local message="$2"
125+
126+
if [ -t 2 ]; then
127+
printf '\r\033[K⏳ [%3s%%] %s' "$percent" "$message" >&2
128+
else
129+
printf '⏳ [%3s%%] %s\n' "$percent" "$message" >&2
130+
fi
131+
}
132+
133+
ui_progress_done() {
134+
local message="${1:-完成}"
135+
136+
if [ -t 2 ]; then
137+
printf '\r\033[K✅ [100%%] %s\n' "$message" >&2
138+
else
139+
printf '✅ [100%%] %s\n' "$message" >&2
140+
fi
141+
}
142+
143+
ui_progress_clear() {
144+
if [ -t 2 ]; then
145+
printf '\r\033[K' >&2
146+
fi
147+
}
148+
122149
install_phase_begin() {
123150
local icon="$1"
124151
local text="$2"

0 commit comments

Comments
 (0)