Skip to content

Commit

Permalink
generic progress bar for download utility
Browse files Browse the repository at this point in the history
  • Loading branch information
rockerbacon committed May 4, 2024
1 parent b835fd8 commit 65025ea
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions utils/download.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# usage: download.sh url
# downloads resource at url to the current working directory.
# usage: download.sh URL DST
# downloads resource at URL to DST

function log_error() {
echo "ERROR:" "$@" >&2
Expand All @@ -22,6 +22,11 @@ elif [ -z "$out_file" ]; then
exit 1
fi

progress_bar() {
stdbuf -o0 tr '[:cntrl:]' '\n' \
| grep --color=never --line-buffered -oE '[0-9\.]+%' \
| zenity --progress --auto-kill --auto-close --text "$1"
}

# We default to wget because it seems like a better choice for this sort of work -
# doesn't overwrite files by default, better progress bar, downloads resources that
Expand All @@ -48,17 +53,14 @@ log_info "downloading '$url' to '$out_file'"
case "$download_backend" in
"wget")
wget "$url" --progress=dot --no-verbose --show-progress -O "$out_file" 2>&1 \
| grep --line-buffered --color=never -oE '[0-9]+%' \
| zenity --progress --auto-kill --auto-close --text="$progress_text"
| progress_bar "$progress_text"
;;

"curl")
redirected_url=$(curl -ILs -o /dev/null -w '%{url_effective}' "$url")

curl "$redirected_url" -o "$out_file" -# 2>&1 \
| tr '\r' '\n' \
| grep --line-buffered --color=never -oE '[0-9]+\.[0-9]+%' \
| zenity --progress --auto-kill --auto-close --text="$progress_text"
| progress_bar "$progress_text"
;;
esac

Expand Down

0 comments on commit 65025ea

Please sign in to comment.