vsleep: Improve handling of terminal resizing while running

parent 692b1d28
2019-03-11:
- vsleep: Fix length calculation for time fields
- vsleep: Improve handling of terminal resizing while running
2019-03-07:
- Make vsleep/group_by work with non-GNU awk
......
......@@ -186,9 +186,9 @@ do
sleep_times+=( "${sleep_time}:${parsed}" )
done
# cache of terminal "things"
tput_columns="$( tput cols )" # number of columns in terminal
tput_clean_eol="$( tput el )" # magic sequence to clear data to end of line
# Recalculate length of fields on first call, and on SIGWINCH event
trap 'recalc_lengths=1' SIGWINCH
recalc_lengths=1
## Main sleeping loop
for sleep_spec in "${sleep_times[@]}"
......@@ -203,24 +203,32 @@ do
# What is the max length that will be needed to represent time done/todo
time_len="$( nice_time_len "${sleep_time}" )"
# How long can we use progress bar
progress_len=$(( "${tput_columns}" - ${#sleep_label} - 2 - ${time_len} - 1 - 4 - 2 - 2 - 4 - 1 - ${time_len} - 1 ))
if (( ${progress_len} > 0 ))
then
progress_format="\r${sleep_label}: %${time_len}s/%3d%% [%-${progress_len}s] %3d%%/%${time_len}s${tput_clean_eol}"
tick="$( awk -v "s=${sleep_time}" -v "p=${progress_len}" 'BEGIN {t=s / (p * 2.1 ); if (t>1) t=1; printf "%.2f\n", t }' )"
else
progress_len=0
progress_format="\r${sleep_label}: %${time_len}s/%3d%% :: %3d%%/%${time_len}s${tput_clean_eol}"
tick=1
fi
while true
do
# (re)Calculate lengths of fields on first run and on SIGWINCH event.
if (( ${recalc_lengths} == 1 ))
then
tput_columns="$( tput cols )" # number of columns in terminal
tput_clean_eol="$( tput el )" # magic sequence to clear data to end of line
# How long can we use progress bar
progress_len=$(( "${tput_columns}" - ${#sleep_label} - 2 - ${time_len} - 1 - 4 - 2 - 2 - 4 - 1 - ${time_len} - 1 ))
if (( ${progress_len} > 0 ))
then
progress_format="\r${sleep_label}: %${time_len}s/%3d%% [%-${progress_len}s] %3d%%/%${time_len}s${tput_clean_eol}"
tick="$( awk -v "s=${sleep_time}" -v "p=${progress_len}" 'BEGIN {t=s / (p * 2.1 ); if (t>1) t=1; printf "%.2f\n", t }' )"
else
progress_len=0
progress_format="\r${sleep_label}: %${time_len}s/%3d%% :: %3d%%/%${time_len}s${tput_clean_eol}"
tick=1
fi
recalc_lengths=0
fi
export done=0
show_progress "${start_at}" "${sleep_time}" "${progress_format}" "${progress_len}"
(( ${done} == 100 )) && break
sleep "${tick}"
done
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment