Skip to content

Commit

Permalink
add clean_progress_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jul 31, 2024
1 parent 559b85a commit d34fab9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
54 changes: 43 additions & 11 deletions __frzr-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,42 @@ clean_progress() {
local scale=$1
local postfix=$2
local last_value=$scale
local start_value=${3:-0}
while IFS= read -r line || [[ -n $line ]]; do
value=$(( ${line}*${scale}/100 + ${start_value} ))
local offset_value=${3:-0}

while IFS= read -r line || [[ -n "$line" ]]; do
# 检查输入行是否为有效数字
if ! [[ "$line" =~ ^[0-9]+$ ]]; then
continue
fi
value=$(( ${line}*${scale}/100 ))
if [ "$last_value" != "$value" ]; then
value=$(( ${value} + ${offset_value} ))
echo ${value}${postfix}
last_value=$value
fi
done
}

clean_progress_loop() {
local max_progress=$1
local current_loop=$2
local loop_count=$3
local postfix=$4
local last_value=$max_progress

every_progress=$((max_progress / loop_count))
start_progress=$((SER * every_progress))
end_progress=$(((SER + 1) * every_progress))
scale=$((end_progress - start_progress))

while IFS= read -r line || [[ -n "$line" ]]; do
# 检查输入行是否为有效数字
if ! [[ "$line" =~ ^[0-9]+$ ]]; then
continue
fi
value=$(( ${line}*${scale}/100 ))
if [ "$last_value" != "$value" ]; then
value=$(( ${value} + ${start_progress} ))
echo ${value}${postfix}
last_value=$value
fi
Expand Down Expand Up @@ -701,41 +733,41 @@ main() {
if [[ -x "$(command -v aria2c)" ]];then
if [ $FRZR_STEAM_PROGRESS -eq 1 ]; then
max_progress=91
start_progress=$((SER * max_progress / TOTAL))

aria2c --connect-timeout=10 --file-allocation=none -x 16 -s 16 \
--checksum=sha-256="${CHECKSUM}" --auto-file-renaming=false --allow-overwrite=true \
--console-log-level=warn --stderr=true -o "${FILE_NAME}" -d "${MOUNT_PATH}" "${SUB_IMG_URL}" 2>&1 | \
grep --line-buffered -oP '\d+(?=%)' | clean_progress $max_progress % $start_progress
grep --line-buffered -oP '\d+(?=%)' | clean_progress_loop $max_progress $SER $TOTAL %
elif [ -z ${SHOW_UI} ]; then
echo "downloading ${FILE_NAME}..."
aria2c --connect-timeout=10 --file-allocation=none -x 16 -s 16 \
--checksum=sha-256="${CHECKSUM}" --auto-file-renaming=false --allow-overwrite=true \
--console-log-level=warn -o "${FILE_NAME}" -d "${MOUNT_PATH}" "${SUB_IMG_URL}"
else
max_progress=100
start_progress=$((SER * max_progress / TOTAL))

aria2c --connect-timeout=10 --file-allocation=none -x 16 -s 16 \
--checksum=sha-256="${CHECKSUM}" --auto-file-renaming=false --allow-overwrite=true \
--console-log-level=warn --stderr=true -o "${FILE_NAME}" -d "${MOUNT_PATH}" "${SUB_IMG_URL}" 2>&1 | \
grep --line-buffered -oP '\d+(?=%)' | clean_progress $max_progress '' $start_progress | \
grep --line-buffered -oP '\d+(?=%)' | clean_progress_loop $max_progress $SER $TOTAL | \
whiptail --gauge "下载系统镜像 (${NAME}) " 10 50 0
fi
else
if [ $FRZR_STEAM_PROGRESS -eq 1 ]; then
max_progress=91
start_progress=$((SER * max_progress / TOTAL))

curl --connect-timeout 10 --http1.1 -# -L -o "${SUB_IMG_FILE}" -C - "${SUB_IMG_URL}" 2>&1 | \
stdbuf -oL tr '\r' '\n' | grep --line-buffered -oP '[0-9]*+(?=.[0-9])' | \
clean_progress $max_progress % $start_progress
clean_progress_loop $max_progress $SER $TOTAL %
elif [ -z ${SHOW_UI} ]; then
echo "downloading ${FILE_NAME}..."
curl --connect-timeout 10 --http1.1 -L -o "${SUB_IMG_FILE}" -C - "${SUB_IMG_URL}"
else
max_progress=100
start_progress=$((SER * max_progress / TOTAL))

curl --connect-timeout 10 --http1.1 -# -L -o "${SUB_IMG_FILE}" -C - "${SUB_IMG_URL}" 2>&1 | \
stdbuf -oL tr '\r' '\n' | grep --line-buffered -oP '[0-9]*+(?=.[0-9])' | \
clean_progress $max_progress '' $start_progress | \
clean_progress_loop $max_progress $SER $TOTAL | \
whiptail --gauge "下载系统镜像 (${NAME})" 10 50 0
fi
fi
Expand Down
15 changes: 14 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CHANNEL="${1:-stable}"
IMG_LIST_URL=$(echo $RELEASES | get_img_url "${CHANNEL}")
echo "IMG_LIST_URL: $IMG_LIST_URL"

BASE_URL=$(dirname "$(echo \"${IMG_LIST_URL}\" | cut -d' ' -f1)")
BASE_URL=$(dirname "$(echo ${IMG_LIST_URL} | cut -d' ' -f1)")
echo "BASE_URL: $BASE_URL"

IMG_LIST_STR="$(curl --http1.1 -L -s --connect-timeout 5 -m 15 ${BASE_URL}/sha256sum.txt)"
Expand Down Expand Up @@ -45,5 +45,18 @@ for CHECKSUM in $CHECKSUM_LIST; do
FILENAME=$(echo "$IMG_LIST_STR" | grep $CHECKSUM | awk '{print $2}')
echo "Downloading $FILENAME , SER=$SER"

max_progress=91
every_progress=$((max_progress / TOTAL))
start_progress=$((SER * every_progress))
end_progress=$(((SER + 1) * every_progress))
scale=$((end_progress - start_progress))

echo "max_progress: $max_progress, start_progress $start_progress"

input="0\n25\n50\n75\n100"
# echo -e $input | clean_progress $scale "%" $start_progress

echo -e $input | clean_progress_loop $max_progress $SER $TOTAL "%"

SER=$((SER+1))
done
25 changes: 25 additions & 0 deletions test/test_clean_progress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /bin/bash

source ../__frzr-deploy

set -e

# 测试函数
echo "Testing clean_progress function..."

CHECKSUM_LIST="123
456
2
789"

SER=0
TOTAL=$(echo "$CHECKSUM_LIST" | wc -l)

max_progress=200
for CHECKSUM in $CHECKSUM_LIST; do
echo "Downloading SER=$SER"
input="0\n10\n25\n50\n75\n100"
echo -e $input | clean_progress_loop $max_progress $SER $TOTAL "%"

SER=$((SER+1))
done

0 comments on commit d34fab9

Please sign in to comment.