Repository navigation
fix(installer): keep download progress inside the terminal (ROCMAI-770) #847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5dd0add
03d2341
bf5c4c2
55ca399
bf4a918
1eeb286
18328e2
f0696b3
1269b46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,13 +70,85 @@ ok() { | |
| printf '%s\n' "${C_OK}::${C_RESET} $*" | ||
| } | ||
|
|
||
| # Width to draw a download progress bar at, or empty when no bar should be | ||
| # drawn. curl sizes its bar from COLUMNS whenever that variable parses to a | ||
| # sane number, in preference to asking the terminal, and then draws a line | ||
| # exactly that wide. A COLUMNS left over from a wider window, routine when a | ||
| # terminal is resized after the shell starts, therefore makes every refresh run | ||
| # past the right margin and wrap; the carriage return ending each frame then | ||
| # returns to the start of the wrapped row rather than the bar's, so each redraw | ||
| # paints a row lower and one download arrives as a stack of partial bars with | ||
| # its percentage torn across two rows. Measuring the terminal here and passing | ||
| # that width down puts the real one back in charge, and reserving the last | ||
| # column keeps the line clear of the margin if the measurement is off by one. | ||
| progress_bar_width() { | ||
| width="" | ||
| if command -v stty >/dev/null 2>&1; then | ||
| # fd 2 is what fetch() gates the bar on, and the only descriptor still | ||
| # attached to the terminal under `curl ... | sh`, where stdin is the pipe. | ||
| width="$(stty size <&2 2>/dev/null | awk '{print $2}')" | ||
| fi | ||
| # A terminal whose window size was never set (a freshly opened pty, say) | ||
| # reports 0. That means "unmeasured", not "extremely narrow", so fall through | ||
| # to the other sources rather than treating it as a width and dropping the | ||
| # bar that curl would otherwise have drawn at its own default. | ||
| [ "${width}" = "0" ] && width="" | ||
| if [ -z "${width}" ] && command -v tput >/dev/null 2>&1; then | ||
| # Read fd 2 here too: tput otherwise sizes from its own stdin, which under | ||
| # `curl ... | sh` is the pipe, making ncurses report its 80-column default | ||
| # instead of the terminal. COLUMNS is blanked because ncurses consults it | ||
| # first, and would hand back the stale value this function exists to escape. | ||
| width="$(COLUMNS='' tput cols <&2 2>/dev/null || true)" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking (standing from my earlier review at f0696b3, code here is unchanged): the comment on lines 97-101 justifies redirecting |
||
| fi | ||
| [ "${width}" = "0" ] && width="" | ||
| if [ -z "${width}" ]; then | ||
| width="${COLUMNS:-80}" | ||
| fi | ||
| case "${width}" in | ||
| "" | *[!0-9]*) width=80 ;; | ||
| *) : ;; | ||
| esac | ||
| # Strip leading zeros. A zero-padded value such as 080 passes the digit check | ||
| # above but is an octal literal to POSIX arithmetic, which dash rejects | ||
| # outright ("Illegal number: 080"). bash's 10# prefix would force base 10 but | ||
| # is not POSIX, and dash rejects that too, so do it by hand. | ||
| while :; do | ||
| case "${width}" in | ||
| 0[0-9]*) width="${width#0}" ;; | ||
| *) break ;; | ||
| esac | ||
| done | ||
| # A bare 0 reaching here came from the environment rather than a measurement, | ||
| # so treat it as unmeasured like the terminal sources above. | ||
| [ "${width}" = "0" ] && width=80 | ||
| # curl ignores a COLUMNS of 20 or less and sizes the bar itself, which would | ||
| # reintroduce the overrun this function exists to prevent. Report no width so | ||
| # the caller downloads quietly instead. | ||
| if [ "${width}" -le 21 ]; then | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: a |
||
| return 0 | ||
| fi | ||
| printf '%s\n' "$((width - 1))" | ||
| } | ||
|
|
||
| # Set once the narrow-terminal notice below has been printed, so three | ||
| # downloads do not repeat it. | ||
| narrow_terminal_reported=0 | ||
|
|
||
| fetch() { | ||
| url="$1" | ||
| output="$2" | ||
| failure_message="${3:-failed to download ${url}}" | ||
| if command -v curl >/dev/null 2>&1; then | ||
| bar_width="" | ||
| if [ -t 2 ]; then | ||
| curl -fL --progress-bar "${url}" -o "${output}" || { | ||
| bar_width="$(progress_bar_width)" | ||
| if [ -z "${bar_width}" ] && [ "${narrow_terminal_reported}" -eq 0 ]; then | ||
| info "terminal too narrow for a progress bar, downloading quietly" | ||
| narrow_terminal_reported=1 | ||
| fi | ||
| fi | ||
| if [ -n "${bar_width}" ]; then | ||
| COLUMNS="${bar_width}" curl -fL --progress-bar "${url}" -o "${output}" || { | ||
| rm -f "${output}" | ||
| fail "${failure_message}" | ||
| } | ||
|
|
@@ -88,7 +160,12 @@ fetch() { | |
| fi | ||
| elif command -v wget >/dev/null 2>&1; then | ||
| if [ -t 2 ]; then | ||
| wget --show-progress -O "${output}" "${url}" || { | ||
| # --show-progress alone leaves wget's full request logging in place, so | ||
| # the bar is interleaved with resolver and redirect chatter. -nv drops | ||
| # that chatter and keeps the bar, which wget already holds inside the | ||
| # terminal width. Not -q, which would also swallow wget's reason for a | ||
| # failure and leave only this script's generic message. | ||
| wget -nv --show-progress -O "${output}" "${url}" || { | ||
| rm -f "${output}" | ||
| fail "${failure_message}" | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking (comment-accuracy only, confidence 75): the comment above says tput "sizes from its own stdin," which is why the
<&2redirect is said to be needed. Reproduced locally against ncursestput(6.4):tput colsreads the winsize from fd 2 if it's a tty, else fd 1, regardless of any<&Nredirect applied to the invocation — a simulatedcurl|shlayout (fd0=pipe, fd1/fd2=tty) returned the correct width with no<&2redirect at all, andtput cols <&1with fd2 as the tty still reported fd2's size. So the fix still works (this is harmless today), but the stated mechanism for why the redirect matters appears to be wrong for this tput implementation, and could mislead someone who tries to "simplify" based on the explanation, or assumes the same trick transfers to a different introspection tool. Might be worth softening the comment's claim or re-verifying against the busybox/non-ncursestputvariants this script may also run under.