diff --git a/src/install.sh b/src/install.sh index 946f45b..1e937c3 100644 --- a/src/install.sh +++ b/src/install.sh @@ -154,28 +154,25 @@ else SIZE=0 REASON="" PROGRESS=() - DOTBYTES=2097152 + OUTPUT="" LOG=$(mktemp) [[ "${URL,,}" == *"_72806.pat" ]] && SIZE=361010261 [[ "${URL,,}" == *"_69057.pat" ]] && SIZE=363837333 [[ "${URL,,}" == *"_42218.pat" ]] && SIZE=379637760 - # Check if output is to interactive TTY or redirected to docker log + # Use Wget's progress bar in a terminal and progress.sh in container logs. if [ -t 1 ]; then - PROGRESS=( --progress=bar:noscroll ) + PROGRESS=( --show-progress --progress=bar:noscroll ) else - if (( SIZE > 0 )); then - DOTBYTES=$(( (SIZE + 199) / 200 )) - fi - PROGRESS=( --progress=dot --execute "dotbytes=$DOTBYTES" ) + OUTPUT="log" fi - /run/progress.sh "$PAT" "$SIZE" "$MSG ([P])..." & + /run/progress.sh "$PAT" "$SIZE" "$MSG ([P])..." "$OUTPUT" & { LC_ALL=C wget "$URL" -O "$PAT" --no-verbose --no-check-certificate \ - --timeout=30 --no-http-keep-alive --show-progress "${PROGRESS[@]}" \ + --timeout=30 --no-http-keep-alive "${PROGRESS[@]}" \ --output-file="$LOG" rc=$? } || : diff --git a/src/progress.sh b/src/progress.sh index c46e1f0..a26c35b 100644 --- a/src/progress.sh +++ b/src/progress.sh @@ -3,43 +3,112 @@ set -Eeuo pipefail info="/run/shm/msg.html" -escape () { - local s - s=${1//&/\&} - s=${s///\>} - s=${s//'"'/\"} - printf -- %s "$s" +escape() { + + local s + + s=${1//&/\&} + s=${s///\>} + s=${s//'"'/\"} + + printf -- %s "$s" + return 0 +} + +getBytes() { + + local path="$1" + local bytes + + if [ ! -s "$path" ] && [ ! -d "$path" ]; then + echo "0" return 0 + fi + + bytes=$(du -sb "$path" 2>/dev/null | cut -f1) || bytes="0" + + echo "$bytes" + return 0 +} + +printPercentProgress() { + + local percent="$1" + + while (( next_percent <= percent && next_percent <= 100 )); do + printf '%s%% ' "$next_percent" + printed="Y" + next_percent=$((next_percent + 10)) + done + + return 0 +} + +printSizeProgress() { + + local bytes="$1" + local size + + while (( bytes >= next_bytes )); do + size=$(numfmt --to=iec --suffix=B "$next_bytes" | sed -r 's/([A-Z])/ \1/') || size="${next_bytes} bytes" + printf '%s ' "$size" + printed="Y" + next_bytes=$((next_bytes + 536870912)) + done + + return 0 +} + +finishLogProgress() { + + if [[ "$output" == "log" && "$printed" == "Y" ]]; then + printf '\n' + fi + + return 0 } path="$1" total="$2" body=$(escape "$3") +output="${4:-}" + +printed="N" +next_percent=10 +next_bytes=536870912 + +trap finishLogProgress EXIT if [[ "$body" == *"..." ]]; then body="

${body::-3}

" fi -while true -do +while true; do + + bytes=$(getBytes "$path") - if [ ! -s "$path" ] && [ ! -d "$path" ]; then - bytes="0" - else - bytes=$(du -sb "$path" 2>/dev/null | cut -f1) || bytes="0" - fi - if (( bytes > 4096 )); then - if [ -z "$total" ] || [[ "$total" == "0" ]] || [ "$bytes" -gt "$total" ]; then + if [ -z "$total" ] || [[ "$total" == "0" ]] || (( bytes > total )); then size=$(numfmt --to=iec --suffix=B "$bytes" | sed -r 's/([A-Z])/ \1/') || size="${bytes} bytes" + + if [[ "$output" == "log" ]]; then + printSizeProgress "$bytes" + fi else - size="$(echo "$bytes" "$total" | awk '{printf "%.1f", $1 * 100 / $2}')" - size="$size%" + progress=$((bytes * 1000 / total)) + (( progress > 1000 )) && progress=1000 + + percent=$((progress / 10)) + printf -v size '%d.%d%%' "$((progress / 10))" "$((progress % 10))" + + if [[ "$output" == "log" ]]; then + printPercentProgress "$percent" + fi fi - [[ "$size" != "0.0%" ]] && echo "${body//(\[P\])/($size)}"> "$info" + + [[ "$size" != "0.0%" ]] && echo "${body//(\[P\])/($size)}" > "$info" fi sleep 1 & wait $! - done