Only execute spinner function if there is a tty

Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
This commit is contained in:
RD WebDesign 2022-07-24 17:52:17 -03:00
parent c7108a632c
commit 8d1f286f30
No known key found for this signature in database
GPG key ID: AE3C7FC910687F33

View file

@ -1348,30 +1348,33 @@ check_database_integrity() {
# Show a text spinner during a long process run # Show a text spinner during a long process run
# #
spinner(){ spinner(){
local PID=$! # PID of the most recent background process # Show the spinner only if there is a tty
local spin="/-\|" if tty -s; then
local start=0 local PID=$! # PID of the most recent background process
local elapsed=0 local spin="/-\|"
local i=1 local start=0
local elapsed=0
local i=1
start=$(date +%s) # Start the counter start=$(date +%s) # Start the counter
tput civis > /dev/tty # Hide the cursor tput civis > /dev/tty # Hide the cursor
trap 'tput cnorm > /dev/tty' EXIT # ensures cursor is visible again, in case of premature exit trap 'tput cnorm > /dev/tty' EXIT # ensures cursor is visible again, in case of premature exit
while [ -d /proc/$PID ]; do while [ -d /proc/$PID ]; do
elapsed=$(( $(date +%s) - start )) elapsed=$(( $(date +%s) - start ))
# print the spinner only on screen (tty) - use hours only if needed # use hours only if needed
if [ "$elapsed" -lt 3600 ]; then if [ "$elapsed" -lt 3600 ]; then
printf "\r${spin:i++%${#spin}:1} %02d:%02d" $((elapsed/60)) $((elapsed%60)) >"$(tty)" printf "\r${spin:i++%${#spin}:1} %02d:%02d" $((elapsed/60)) $((elapsed%60)) >"$(tty)"
else else
printf "\r${spin:i++%${#spin}:1} %02d:%02d:%02d" $((elapsed/3600)) $(((elapsed/60)%60)) $((elapsed%60)) >"$(tty)" printf "\r${spin:i++%${#spin}:1} %02d:%02d:%02d" $((elapsed/3600)) $(((elapsed/60)%60)) $((elapsed%60)) >"$(tty)"
fi fi
sleep 0.25 sleep 0.25
done done
printf "\r" >"$(tty)" # Return to the begin of the line after completion (the spinner will be overwritten) printf "\r" >"$(tty)" # Return to the begin of the line after completion (the spinner will be overwritten)
tput cnorm > /dev/tty # Restore cursor visibility tput cnorm > /dev/tty # Restore cursor visibility
fi
} }
obfuscated_pihole_log() { obfuscated_pihole_log() {