Skip to content

Commit d099d2c

Browse files
authored
fix(cli): poll install download progress instead of tracing bytes (anomalyco#55586)
1 parent 97c2019 commit d099d2c

1 file changed

Lines changed: 20 additions & 49 deletions

File tree

‎install‎

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,6 @@ check_version() {
243243
fi
244244
}
245245

246-
unbuffered_sed() {
247-
if echo | sed -u -e "" >/dev/null 2>&1; then
248-
sed -nu "$@"
249-
elif echo | sed -l -e "" >/dev/null 2>&1; then
250-
sed -nl "$@"
251-
else
252-
local pad="$(printf "\n%512s" "")"
253-
sed -ne "s/$/\\${pad}/" "$@"
254-
fi
255-
}
256-
257246
print_progress() {
258247
local bytes="$1"
259248
local length="$2"
@@ -283,52 +272,34 @@ download_with_progress() {
283272
exec 4>/dev/null
284273
fi
285274

286-
local tmp_dir=${TMPDIR:-/tmp}
287-
local basename="${tmp_dir}/opencode_install_$$"
288-
local tracefile="${basename}.trace"
289-
290-
rm -f "$tracefile"
291-
mkfifo "$tracefile"
275+
local headers="${output}.headers"
292276

293277
# Hide cursor
294278
printf "\033[?25l" >&4
295279

296-
trap "trap - RETURN; rm -f \"$tracefile\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
280+
trap "trap - RETURN; rm -f \"$headers\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
297281

298-
(
299-
curl --trace-ascii "$tracefile" -fsL -o "$output" "$url"
300-
) &
282+
# Poll the file size on a timer so rendering cost stays fixed no matter how many bytes arrive.
283+
# Read the size with ls because busybox wc -c reads the whole file.
284+
curl -fsL -D "$headers" -o "$output" "$url" &
301285
local curl_pid=$!
302286

303-
unbuffered_sed \
304-
-e 'y/ACDEGHLNORTV/acdeghlnortv/' \
305-
-e '/^0000: content-length:/p' \
306-
-e '/^<= recv data/p' \
307-
"$tracefile" | \
308-
{
309-
local length=0
310-
local bytes=0
311-
312-
while IFS=" " read -r -a line; do
313-
[ "${#line[@]}" -lt 2 ] && continue
314-
local tag="${line[0]} ${line[1]}"
315-
316-
if [ "$tag" = "0000: content-length:" ]; then
317-
length="${line[2]}"
318-
length=$(echo "$length" | tr -d '\r')
319-
bytes=0
320-
elif [ "$tag" = "<= recv" ]; then
321-
local size="${line[3]}"
322-
bytes=$(( bytes + size ))
323-
if [ "$length" -gt 0 ]; then
324-
print_progress "$bytes" "$length"
325-
fi
326-
fi
327-
done
328-
}
287+
local length bytes
288+
while kill -0 "$curl_pid" 2>/dev/null; do
289+
if [ -f "$output" ]; then
290+
length=$(grep -i '^content-length:' "$headers" 2>/dev/null | tail -n 1 | tr -dc '0-9' || true)
291+
bytes=$(ls -ln "$output" | awk '{print $5}')
292+
print_progress "$bytes" "${length:-0}"
293+
fi
294+
sleep 0.1 2>/dev/null || sleep 1
295+
done
329296

330-
wait $curl_pid
331-
local ret=$?
297+
local ret=0
298+
wait "$curl_pid" || ret=$?
299+
if [ "$ret" -eq 0 ]; then
300+
bytes=$(ls -ln "$output" | awk '{print $5}')
301+
print_progress "$bytes" "$bytes"
302+
fi
332303
echo "" >&4
333304
return $ret
334305
}

0 commit comments

Comments
 (0)