#!/bin/bash -
# ------------------------------------------------------------------------------
# progressbar.sh
# ==============
#
# Scope Native
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License GNU General Public License (GPL) 2.0
# https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------
set -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'xtrace' -o 'errexit'
# ------------------------------------------------------------------------------
COUNT=123
# ------------------------------------------------------------------------------
PRINTF=('/usr/local/bin/gprintf')
STTY=('/usr/local/bin/gstty')
# ------------------------------------------------------------------------------
# Current dimension of the terminal (rows, columns)
read -r 'x' 'y' <<<"$("${STTY[@]}" 'size')"
# Get the length of the progress bar
bar_lenght=$((y - 12))
# Do something for 'COUNT' times e.g convert 'count' files
for ((i = 1; i <= COUNT; i++)); do
# Print the percentage and the progress bar
"${PRINTF[@]}" "\r%3d%% |%-${bar_lenght}s|" \
$((100 * i / COUNT)) \
$("${PRINTF[@]}" '=%.s' $(eval echo "{1..$((bar_lenght * i / COUNT))}"))
done
echo ' done'
# ------------------------------------------------------------------------------
exit 0