mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Merge pull request #4834 from pi-hole/fix/spinner
Only execute spinner function if there is a tty
This commit is contained in:
commit
f59749b1c3
1 changed files with 31 additions and 21 deletions
|
@ -1346,32 +1346,42 @@ check_database_integrity() {
|
|||
}
|
||||
|
||||
# Show a text spinner during a long process run
|
||||
#
|
||||
spinner(){
|
||||
local PID=$! # PID of the most recent background process
|
||||
local spin="/-\|"
|
||||
local start=0
|
||||
local elapsed=0
|
||||
local i=1
|
||||
# Show the spinner only if there is a tty
|
||||
if tty -s; then
|
||||
# PID of the most recent background process
|
||||
_PID=$!
|
||||
_spin="/-\|"
|
||||
_start=0
|
||||
_elapsed=0
|
||||
_i=1
|
||||
|
||||
start=$(date +%s) # Start the counter
|
||||
# Start the counter
|
||||
_start=$(date +%s)
|
||||
|
||||
tput civis > /dev/tty # Hide the cursor
|
||||
trap 'tput cnorm > /dev/tty' EXIT # ensures cursor is visible again, in case of premature exit
|
||||
# Hide the cursor
|
||||
tput civis > /dev/tty
|
||||
|
||||
while [ -d /proc/$PID ]; do
|
||||
elapsed=$(( $(date +%s) - start ))
|
||||
# print the spinner only on screen (tty) - use hours only if needed
|
||||
if [ "$elapsed" -lt 3600 ]; then
|
||||
printf "\r${spin:i++%${#spin}:1} %02d:%02d" $((elapsed/60)) $((elapsed%60)) >"$(tty)"
|
||||
else
|
||||
printf "\r${spin:i++%${#spin}:1} %02d:%02d:%02d" $((elapsed/3600)) $(((elapsed/60)%60)) $((elapsed%60)) >"$(tty)"
|
||||
fi
|
||||
sleep 0.25
|
||||
done
|
||||
# ensures cursor is visible again, in case of premature exit
|
||||
trap 'tput cnorm > /dev/tty' EXIT
|
||||
|
||||
printf "\r" >"$(tty)" # Return to the begin of the line after completion (the spinner will be overwritten)
|
||||
tput cnorm > /dev/tty # Restore cursor visibility
|
||||
while [ -d /proc/$_PID ]; do
|
||||
_elapsed=$(( $(date +%s) - _start ))
|
||||
# use hours only if needed
|
||||
if [ "$_elapsed" -lt 3600 ]; then
|
||||
printf "\r${_spin:_i++%${#_spin}:1} %02d:%02d" $((_elapsed/60)) $((_elapsed%60)) >"$(tty)"
|
||||
else
|
||||
printf "\r${_spin:_i++%${#_spin}:1} %02d:%02d:%02d" $((_elapsed/3600)) $(((_elapsed/60)%60)) $((_elapsed%60)) >"$(tty)"
|
||||
fi
|
||||
sleep 0.25
|
||||
done
|
||||
|
||||
# Return to the begin of the line after completion (the spinner will be overwritten)
|
||||
printf "\r" >"$(tty)"
|
||||
|
||||
# Restore cursor visibility
|
||||
tput cnorm > /dev/tty
|
||||
fi
|
||||
}
|
||||
|
||||
obfuscated_pihole_log() {
|
||||
|
|
Loading…
Reference in a new issue