spinner fix

This commit is contained in:
Andrei Smaliak 2026-06-06 08:13:27 +04:00
parent d25c213346
commit 3f613e9851
3 changed files with 31 additions and 23 deletions

View File

@ -36,22 +36,24 @@ spinner() {
local elapsed=0
local last_heartbeat=0
local spinner_tty=0
local spinner_tty_path=""
start_ts="$(date +%s)"
if [[ -w /dev/tty ]]; then
spinner_tty_path="$(tty 2>/dev/null || true)"
if [[ -n "$spinner_tty_path" && "$spinner_tty_path" != "not a tty" && -w "$spinner_tty_path" ]]; then
spinner_tty=1
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput civis >/dev/tty 2>/dev/null || true
tput civis >"$spinner_tty_path" 2>/dev/null || true
fi
while kill -0 "$pid" 2>/dev/null; do
i=$(((i + 1) % 4))
elapsed=$(( $(date +%s) - start_ts ))
if [[ "$spinner_tty" -eq 1 ]]; then
printf '\r[%c] %s... %ss' "${spin:$i:1}" "$msg" "$elapsed" >/dev/tty
printf '\r[%c] %s... %ss' "${spin:$i:1}" "$msg" "$elapsed" >"$spinner_tty_path"
fi
if (( elapsed >= last_heartbeat + 15 )); then
log "HEARTBEAT: ${msg} still running (${elapsed}s)"
@ -61,20 +63,20 @@ spinner() {
done
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput cnorm >/dev/tty 2>/dev/null || true
tput cnorm >"$spinner_tty_path" 2>/dev/null || true
fi
wait "$pid"
local rc=$?
if [[ "$rc" -eq 0 ]]; then
if [[ "$spinner_tty" -eq 1 ]]; then
printf '\r[OK] %s. \n' "$msg" >/dev/tty
printf '\r[OK] %s. \n' "$msg" >"$spinner_tty_path"
else
echo "[OK] $msg"
fi
else
if [[ "$spinner_tty" -eq 1 ]]; then
printf '\r[FAIL:%s] %s. \n' "$rc" "$msg" >/dev/tty
printf '\r[FAIL:%s] %s. \n' "$rc" "$msg" >"$spinner_tty_path"
else
echo "[FAIL:$rc] $msg"
fi

View File

@ -464,22 +464,24 @@ spinner() {
local elapsed=0
local last_heartbeat=0
local spinner_tty=0
local spinner_tty_path=""
start_ts="$(date +%s)"
if [[ -w /dev/tty ]]; then
spinner_tty_path="$(tty 2>/dev/null || true)"
if [[ -n "$spinner_tty_path" && "$spinner_tty_path" != "not a tty" && -w "$spinner_tty_path" ]]; then
spinner_tty=1
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput civis >/dev/tty 2>/dev/null || true
tput civis >"$spinner_tty_path" 2>/dev/null || true
fi
while kill -0 "$pid" 2>/dev/null; do
i=$(((i+1) % 4))
elapsed=$(( $(date +%s) - start_ts ))
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >/dev/tty
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >"$spinner_tty_path"
fi
if (( elapsed >= last_heartbeat + 15 )); then
log "HEARTBEAT: ${msg} still running (${elapsed}s)"
@ -489,10 +491,10 @@ spinner() {
done
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r%-80s\r" "" >/dev/tty
printf "\r%-80s\r" "" >"$spinner_tty_path"
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput cnorm >/dev/tty 2>/dev/null || true
tput cnorm >"$spinner_tty_path" 2>/dev/null || true
fi
}
@ -507,6 +509,7 @@ run_long() {
local elapsed=0
local last_heartbeat=0
local spinner_tty=0
local spinner_tty_path=""
local timed_out=0
shift 2
@ -516,12 +519,13 @@ run_long() {
pid=$!
start_ts="$(date +%s)"
if [[ -w /dev/tty ]]; then
spinner_tty_path="$(tty 2>/dev/null || true)"
if [[ -n "$spinner_tty_path" && "$spinner_tty_path" != "not a tty" && -w "$spinner_tty_path" ]]; then
spinner_tty=1
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput civis >/dev/tty 2>/dev/null || true
tput civis >"$spinner_tty_path" 2>/dev/null || true
fi
while kill -0 "$pid" 2>/dev/null; do
@ -529,7 +533,7 @@ run_long() {
elapsed=$(( $(date +%s) - start_ts ))
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >/dev/tty
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >"$spinner_tty_path"
fi
if (( elapsed >= last_heartbeat + 15 )); then
@ -550,10 +554,10 @@ run_long() {
done
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r%-80s\r" "" >/dev/tty
printf "\r%-80s\r" "" >"$spinner_tty_path"
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput cnorm >/dev/tty 2>/dev/null || true
tput cnorm >"$spinner_tty_path" 2>/dev/null || true
fi
if wait "$pid"; then

View File

@ -233,6 +233,7 @@ run_long() {
local elapsed=0
local last_heartbeat=0
local spinner_tty=0
local spinner_tty_path=""
local timed_out=0
shift 2
@ -242,12 +243,13 @@ run_long() {
pid=$!
start_ts="$(date +%s)"
if [[ -w /dev/tty ]]; then
spinner_tty_path="$(tty 2>/dev/null || true)"
if [[ -n "$spinner_tty_path" && "$spinner_tty_path" != "not a tty" && -w "$spinner_tty_path" ]]; then
spinner_tty=1
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput civis >/dev/tty 2>/dev/null || true
tput civis >"$spinner_tty_path" 2>/dev/null || true
fi
while kill -0 "$pid" 2>/dev/null; do
@ -255,7 +257,7 @@ run_long() {
elapsed=$(( $(date +%s) - start_ts ))
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >/dev/tty
printf "\r[%c] %s... %ss" "${spin:$i:1}" "$msg" "$elapsed" >"$spinner_tty_path"
fi
if (( elapsed >= last_heartbeat + 15 )); then
@ -276,10 +278,10 @@ run_long() {
done
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r%-80s\r" "" >/dev/tty
printf "\r%-80s\r" "" >"$spinner_tty_path"
fi
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
tput cnorm >/dev/tty 2>/dev/null || true
tput cnorm >"$spinner_tty_path" 2>/dev/null || true
fi
wait "$pid"
@ -290,13 +292,13 @@ run_long() {
if [[ "$rc" -eq 0 ]]; then
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r[OK] %s. \n" "$msg" >/dev/tty
printf "\r[OK] %s. \n" "$msg" >"$spinner_tty_path"
else
echo "[OK] $msg"
fi
else
if [[ "$spinner_tty" -eq 1 ]]; then
printf "\r[FAIL:%s] %s. \n" "$rc" "$msg" >/dev/tty
printf "\r[FAIL:%s] %s. \n" "$rc" "$msg" >"$spinner_tty_path"
else
echo "[FAIL:$rc] $msg"
fi