Skip to content

Commit 77f3524

Browse files
authored
Merge pull request #241 from wnlen/dev
optimize: show fixed progress bar for subscription download
2 parents 4b467fd + fae7a75 commit 77f3524

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

scripts/core/config.sh

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,78 @@ download_candidate_fetch() {
576576
"$url"
577577
}
578578

579+
subscription_fake_progress_bar() {
580+
local pid="$1"
581+
local width=72
582+
local percent=3
583+
local filled bar
584+
585+
while kill -0 "$pid" 2>/dev/null; do
586+
percent=$((percent + 5))
587+
[ "$percent" -gt 94 ] && percent=94
588+
589+
filled=$((percent * width / 100))
590+
bar="$(printf '%*s' "$filled" '' | tr ' ' '#')"
591+
592+
printf '\r%-72s %3d.0%%' "$bar" "$percent"
593+
sleep 0.2
594+
done
595+
}
596+
597+
subscription_fake_progress_done() {
598+
printf '\r######################################################################## 100.0%%\n'
599+
}
600+
601+
download_subscription_fetch_quiet() {
602+
local url="$1"
603+
local out="$2"
604+
local ua="${__CLASH_DOWNLOAD_UA:-}"
605+
606+
curl_download \
607+
--silent \
608+
--show-error \
609+
--fail \
610+
--location \
611+
--connect-timeout "$(download_connect_timeout)" \
612+
--max-time "$(download_max_time)" \
613+
--retry 1 \
614+
${ua:+-A "$ua"} \
615+
--output "$out" \
616+
"$url"
617+
}
618+
619+
download_subscription_file() {
620+
local url="$1"
621+
local out="$2"
622+
local fetch_tmp pid rc
623+
624+
mkdir -p "$(dirname "$out")"
625+
rm -f "$out" 2>/dev/null || true
626+
627+
fetch_tmp="$(mktemp)"
628+
rm -f "$fetch_tmp" 2>/dev/null || true
629+
630+
ui_download "正在下载:subscription"
631+
632+
download_subscription_fetch_quiet "$url" "$fetch_tmp" &
633+
pid="$!"
634+
635+
subscription_fake_progress_bar "$pid"
636+
637+
wait "$pid"
638+
rc="$?"
639+
640+
if [ "$rc" -eq 0 ]; then
641+
subscription_fake_progress_done
642+
mv -f "$fetch_tmp" "$out"
643+
return 0
644+
fi
645+
646+
printf '\n'
647+
rm -f "$fetch_tmp" 2>/dev/null || true
648+
return "$rc"
649+
}
650+
579651
download_subscription_yaml() {
580652
local url="$1"
581653
local out_file="$2"
@@ -596,10 +668,9 @@ download_subscription_yaml() {
596668
require_subscription_fetch_allowed "$fetch_reason" "$url"
597669

598670
__CLASH_DOWNLOAD_UA="$(subconverter_subscription_user_agent)" \
599-
download_file \
671+
download_subscription_file \
600672
"$url" \
601-
"$out_file" \
602-
"subscription"
673+
"$out_file"
603674

604675
subscription_cache_store "$url" "$fmt" "$out_file" "$url"
605676
;;

0 commit comments

Comments
 (0)