refactor: optimize terminal progress bar library
refactor: optimize terminal progress bar library - simplify terminal cursor and scroll-region handling - replace echo/let with printf and Bash arithmetic - localize function variables - centralize progress-bar rendering - validate and clamp progress percentages - handle narrow terminal widths safely - improve Ctrl-C cleanup and exit status - retain existing public API - expand inline documentation
This commit is contained in:
1 parent
778490f090
commit
081210d3ae
2 files changed
+237
-173
No files matched your search
@@ -1,173 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Usage:
|
|
||||||
# Source this script
|
|
||||||
# enable_trapping <- optional to clean up properly if user presses ctrl-c
|
|
||||||
# setup_scroll_area <- create empty progress bar
|
|
||||||
# draw_progress_bar 10 <- advance progress bar
|
|
||||||
# draw_progress_bar 40 <- advance progress bar
|
|
||||||
# block_progress_bar 45 <- turns the progress bar yellow to indicate some action is requested from the user
|
|
||||||
# draw_progress_bar 90 <- advance progress bar
|
|
||||||
# destroy_scroll_area <- remove progress bar
|
|
||||||
|
|
||||||
|
|
||||||
# Constants
|
|
||||||
CODE_SAVE_CURSOR="\033[s"
|
|
||||||
CODE_RESTORE_CURSOR="\033[u"
|
|
||||||
CODE_CURSOR_IN_SCROLL_AREA="\033[1A"
|
|
||||||
COLOR_FG="\e[30m"
|
|
||||||
COLOR_BG="\e[42m"
|
|
||||||
COLOR_BG_BLOCKED="\e[43m"
|
|
||||||
RESTORE_FG="\e[39m"
|
|
||||||
RESTORE_BG="\e[49m"
|
|
||||||
|
|
||||||
# Variables
|
|
||||||
PROGRESS_BLOCKED="false"
|
|
||||||
TRAPPING_ENABLED="false"
|
|
||||||
TRAP_SET="false"
|
|
||||||
|
|
||||||
setup_scroll_area() {
|
|
||||||
# If trapping is enabled, we will want to activate it whenever we setup the scroll area and remove it when we break the scroll area
|
|
||||||
if [ "$TRAPPING_ENABLED" = "true" ]; then
|
|
||||||
trap_on_interrupt
|
|
||||||
fi
|
|
||||||
|
|
||||||
lines=$(tput lines)
|
|
||||||
let lines=$lines-1
|
|
||||||
# Scroll down a bit to avoid visual glitch when the screen area shrinks by one row
|
|
||||||
echo -en "\n"
|
|
||||||
|
|
||||||
# Save cursor
|
|
||||||
echo -en "$CODE_SAVE_CURSOR"
|
|
||||||
# Set scroll region (this will place the cursor in the top left)
|
|
||||||
echo -en "\033[0;${lines}r"
|
|
||||||
|
|
||||||
# Restore cursor but ensure its inside the scrolling area
|
|
||||||
echo -en "$CODE_RESTORE_CURSOR"
|
|
||||||
echo -en "$CODE_CURSOR_IN_SCROLL_AREA"
|
|
||||||
|
|
||||||
# Start empty progress bar
|
|
||||||
draw_progress_bar 0
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy_scroll_area() {
|
|
||||||
lines=$(tput lines)
|
|
||||||
# Save cursor
|
|
||||||
echo -en "$CODE_SAVE_CURSOR"
|
|
||||||
# Set scroll region (this will place the cursor in the top left)
|
|
||||||
echo -en "\033[0;${lines}r"
|
|
||||||
|
|
||||||
# Restore cursor but ensure its inside the scrolling area
|
|
||||||
echo -en "$CODE_RESTORE_CURSOR"
|
|
||||||
echo -en "$CODE_CURSOR_IN_SCROLL_AREA"
|
|
||||||
|
|
||||||
# We are done so clear the scroll bar
|
|
||||||
clear_progress_bar
|
|
||||||
|
|
||||||
# Scroll down a bit to avoid visual glitch when the screen area grows by one row
|
|
||||||
echo -en "\n\n"
|
|
||||||
|
|
||||||
# Once the scroll area is cleared, we want to remove any trap previously set. Otherwise, ctrl+c will exit our shell
|
|
||||||
if [ "$TRAP_SET" = "true" ]; then
|
|
||||||
trap - INT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
draw_progress_bar() {
|
|
||||||
percentage=$1
|
|
||||||
lines=$(tput lines)
|
|
||||||
let lines=$lines
|
|
||||||
# Save cursor
|
|
||||||
echo -en "$CODE_SAVE_CURSOR"
|
|
||||||
|
|
||||||
# Move cursor position to last row
|
|
||||||
echo -en "\033[${lines};0f"
|
|
||||||
|
|
||||||
# Clear progress bar
|
|
||||||
tput el
|
|
||||||
|
|
||||||
# Draw progress bar
|
|
||||||
PROGRESS_BLOCKED="false"
|
|
||||||
print_bar_text $percentage
|
|
||||||
|
|
||||||
# Restore cursor position
|
|
||||||
echo -en "$CODE_RESTORE_CURSOR"
|
|
||||||
}
|
|
||||||
|
|
||||||
block_progress_bar() {
|
|
||||||
percentage=$1
|
|
||||||
lines=$(tput lines)
|
|
||||||
let lines=$lines
|
|
||||||
# Save cursor
|
|
||||||
echo -en "$CODE_SAVE_CURSOR"
|
|
||||||
|
|
||||||
# Move cursor position to last row
|
|
||||||
echo -en "\033[${lines};0f"
|
|
||||||
|
|
||||||
# Clear progress bar
|
|
||||||
tput el
|
|
||||||
|
|
||||||
# Draw progress bar
|
|
||||||
PROGRESS_BLOCKED="true"
|
|
||||||
print_bar_text $percentage
|
|
||||||
|
|
||||||
# Restore cursor position
|
|
||||||
echo -en "$CODE_RESTORE_CURSOR"
|
|
||||||
}
|
|
||||||
|
|
||||||
clear_progress_bar() {
|
|
||||||
lines=$(tput lines)
|
|
||||||
let lines=$lines
|
|
||||||
# Save cursor
|
|
||||||
echo -en "$CODE_SAVE_CURSOR"
|
|
||||||
|
|
||||||
# Move cursor position to last row
|
|
||||||
echo -en "\033[${lines};0f"
|
|
||||||
|
|
||||||
# clear progress bar
|
|
||||||
tput el
|
|
||||||
|
|
||||||
# Restore cursor position
|
|
||||||
echo -en "$CODE_RESTORE_CURSOR"
|
|
||||||
}
|
|
||||||
|
|
||||||
print_bar_text() {
|
|
||||||
local percentage=$1
|
|
||||||
local cols=$(tput cols)
|
|
||||||
let bar_size=$cols-17
|
|
||||||
|
|
||||||
local color="${COLOR_FG}${COLOR_BG}"
|
|
||||||
if [ "$PROGRESS_BLOCKED" = "true" ]; then
|
|
||||||
color="${COLOR_FG}${COLOR_BG_BLOCKED}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Prepare progress bar
|
|
||||||
let complete_size=($bar_size*$percentage)/100
|
|
||||||
let remainder_size=$bar_size-$complete_size
|
|
||||||
progress_bar=$(echo -ne "["; echo -en "${color}"; printf_new "#" $complete_size; echo -en "${RESTORE_FG}${RESTORE_BG}"; printf_new "." $remainder_size; echo -ne "]");
|
|
||||||
|
|
||||||
# Print progress bar
|
|
||||||
echo -ne " Progress ${percentage}% ${progress_bar}"
|
|
||||||
}
|
|
||||||
|
|
||||||
enable_trapping() {
|
|
||||||
TRAPPING_ENABLED="true"
|
|
||||||
}
|
|
||||||
|
|
||||||
trap_on_interrupt() {
|
|
||||||
# If this function is called, we setup an interrupt handler to cleanup the progress bar
|
|
||||||
TRAP_SET="true"
|
|
||||||
trap cleanup_on_interrupt INT
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup_on_interrupt() {
|
|
||||||
destroy_scroll_area
|
|
||||||
exit
|
|
||||||
}
|
|
||||||
|
|
||||||
printf_new() {
|
|
||||||
str=$1
|
|
||||||
num=$2
|
|
||||||
v=$(printf "%-${num}s" "$str")
|
|
||||||
echo -ne "${v// /$str}"
|
|
||||||
}
|
|
||||||
Executable
+237
@@ -0,0 +1,237 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Terminal Progress Bar Library
|
||||||
|
#
|
||||||
|
# Description:
|
||||||
|
# Provides a reusable progress bar that occupies the last terminal row
|
||||||
|
# while allowing normal command output to continue scrolling above it.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# Source this file from another Bash script:
|
||||||
|
#
|
||||||
|
# source ./progress_bar.sh
|
||||||
|
#
|
||||||
|
# Then:
|
||||||
|
#
|
||||||
|
# enable_trapping
|
||||||
|
# setup_scroll_area
|
||||||
|
#
|
||||||
|
# draw_progress_bar 10
|
||||||
|
# draw_progress_bar 40
|
||||||
|
#
|
||||||
|
# # Indicates that user interaction/action is required.
|
||||||
|
# block_progress_bar 45
|
||||||
|
#
|
||||||
|
# draw_progress_bar 90
|
||||||
|
# destroy_scroll_area
|
||||||
|
#
|
||||||
|
# Functions:
|
||||||
|
# enable_trapping
|
||||||
|
# Enable Ctrl-C cleanup for the progress bar.
|
||||||
|
#
|
||||||
|
# setup_scroll_area
|
||||||
|
# Create the terminal scrolling area and display an empty progress bar.
|
||||||
|
#
|
||||||
|
# draw_progress_bar <percentage>
|
||||||
|
# Draw a normal green progress bar.
|
||||||
|
#
|
||||||
|
# block_progress_bar <percentage>
|
||||||
|
# Draw a yellow progress bar to indicate that user action is required.
|
||||||
|
#
|
||||||
|
# clear_progress_bar
|
||||||
|
# Clear the progress bar without destroying the scroll area.
|
||||||
|
#
|
||||||
|
# destroy_scroll_area
|
||||||
|
# Restore the terminal scrolling region and remove the progress bar.
|
||||||
|
#
|
||||||
|
# Notes:
|
||||||
|
# - Bash is required.
|
||||||
|
# - Requires `tput` and a terminal supporting ANSI escape sequences.
|
||||||
|
# - This script is intended to be sourced, not executed directly.
|
||||||
|
#
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Terminal escape sequences
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
readonly CODE_SAVE_CURSOR=$'\033[s'
|
||||||
|
readonly CODE_RESTORE_CURSOR=$'\033[u'
|
||||||
|
readonly CODE_CURSOR_IN_SCROLL_AREA=$'\033[1A'
|
||||||
|
|
||||||
|
readonly COLOR_FG=$'\e[30m'
|
||||||
|
readonly COLOR_BG=$'\e[42m'
|
||||||
|
readonly COLOR_BG_BLOCKED=$'\e[43m'
|
||||||
|
readonly RESTORE_FG=$'\e[39m'
|
||||||
|
readonly RESTORE_BG=$'\e[49m'
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# State
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
PROGRESS_BLOCKED=false
|
||||||
|
TRAPPING_ENABLED=false
|
||||||
|
TRAP_SET=false
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Internal helpers
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_terminal_rows() {
|
||||||
|
tput lines
|
||||||
|
}
|
||||||
|
|
||||||
|
_terminal_columns() {
|
||||||
|
tput cols
|
||||||
|
}
|
||||||
|
|
||||||
|
_move_to_progress_row() {
|
||||||
|
local rows
|
||||||
|
rows=$(_terminal_rows)
|
||||||
|
|
||||||
|
printf '%s' "${CODE_SAVE_CURSOR}"
|
||||||
|
printf '\033[%d;0f' "$rows"
|
||||||
|
tput el
|
||||||
|
}
|
||||||
|
|
||||||
|
_restore_cursor() {
|
||||||
|
printf '%s' "${CODE_RESTORE_CURSOR}"
|
||||||
|
}
|
||||||
|
|
||||||
|
_validate_percentage() {
|
||||||
|
local percentage=$1
|
||||||
|
|
||||||
|
if ! [[ "$percentage" =~ ^[0-9]+$ ]]; then
|
||||||
|
percentage=0
|
||||||
|
elif (( percentage > 100 )); then
|
||||||
|
percentage=100
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf '%d' "$percentage"
|
||||||
|
}
|
||||||
|
|
||||||
|
_print_repeated() {
|
||||||
|
local character=$1
|
||||||
|
local count=$2
|
||||||
|
|
||||||
|
(( count <= 0 )) && return
|
||||||
|
|
||||||
|
printf '%*s' "$count" '' | tr ' ' "$character"
|
||||||
|
}
|
||||||
|
|
||||||
|
_print_bar() {
|
||||||
|
local percentage
|
||||||
|
local columns
|
||||||
|
local bar_size
|
||||||
|
local complete_size
|
||||||
|
local remainder_size
|
||||||
|
local background
|
||||||
|
|
||||||
|
percentage=$(_validate_percentage "$1")
|
||||||
|
columns=$(_terminal_columns)
|
||||||
|
|
||||||
|
# 17 characters are reserved for:
|
||||||
|
# " Progress XXX% []"
|
||||||
|
bar_size=$((columns - 17))
|
||||||
|
|
||||||
|
# Prevent negative printf field widths on very small terminals.
|
||||||
|
(( bar_size < 1 )) && bar_size=1
|
||||||
|
|
||||||
|
complete_size=$((bar_size * percentage / 100))
|
||||||
|
remainder_size=$((bar_size - complete_size))
|
||||||
|
|
||||||
|
if [[ "$PROGRESS_BLOCKED" == true ]]; then
|
||||||
|
background=$COLOR_BG_BLOCKED
|
||||||
|
else
|
||||||
|
background=$COLOR_BG
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf ' Progress %3d%% [' "$percentage"
|
||||||
|
printf '%s%s' "$COLOR_FG" "$background"
|
||||||
|
_print_repeated '#' "$complete_size"
|
||||||
|
printf '%s%s' "$RESTORE_FG" "$RESTORE_BG"
|
||||||
|
_print_repeated '.' "$remainder_size"
|
||||||
|
printf ']'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Public API
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
setup_scroll_area() {
|
||||||
|
if [[ "$TRAPPING_ENABLED" == true ]]; then
|
||||||
|
trap_on_interrupt
|
||||||
|
fi
|
||||||
|
|
||||||
|
local rows
|
||||||
|
rows=$(_terminal_rows)
|
||||||
|
(( rows > 1 )) || return 1
|
||||||
|
|
||||||
|
# Create a line for the progress bar and avoid terminal resize glitches.
|
||||||
|
printf '\n'
|
||||||
|
|
||||||
|
# Reserve the final terminal row for the progress bar.
|
||||||
|
printf '%s' "$CODE_SAVE_CURSOR"
|
||||||
|
printf '\033[0;%dr' "$((rows - 1))"
|
||||||
|
|
||||||
|
# Restore the cursor inside the scrolling region.
|
||||||
|
printf '%s%s' "$CODE_RESTORE_CURSOR" "$CODE_CURSOR_IN_SCROLL_AREA"
|
||||||
|
|
||||||
|
draw_progress_bar 0
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy_scroll_area() {
|
||||||
|
local rows
|
||||||
|
rows=$(_terminal_rows)
|
||||||
|
|
||||||
|
# Restore the full terminal scrolling region.
|
||||||
|
printf '%s' "$CODE_SAVE_CURSOR"
|
||||||
|
printf '\033[0;%dr' "$rows"
|
||||||
|
printf '%s%s' "$CODE_RESTORE_CURSOR" "$CODE_CURSOR_IN_SCROLL_AREA"
|
||||||
|
|
||||||
|
clear_progress_bar
|
||||||
|
|
||||||
|
# Allow the terminal to resume normal scrolling.
|
||||||
|
printf '\n\n'
|
||||||
|
|
||||||
|
if [[ "$TRAP_SET" == true ]]; then
|
||||||
|
trap - INT
|
||||||
|
TRAP_SET=false
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_progress_bar() {
|
||||||
|
PROGRESS_BLOCKED=false
|
||||||
|
|
||||||
|
_move_to_progress_row
|
||||||
|
_print_bar "$1"
|
||||||
|
_restore_cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
block_progress_bar() {
|
||||||
|
PROGRESS_BLOCKED=true
|
||||||
|
|
||||||
|
_move_to_progress_row
|
||||||
|
_print_bar "$1"
|
||||||
|
_restore_cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_progress_bar() {
|
||||||
|
_move_to_progress_row
|
||||||
|
_restore_cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_trapping() {
|
||||||
|
TRAPPING_ENABLED=true
|
||||||
|
}
|
||||||
|
|
||||||
|
trap_on_interrupt() {
|
||||||
|
TRAP_SET=true
|
||||||
|
trap cleanup_on_interrupt INT
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup_on_interrupt() {
|
||||||
|
destroy_scroll_area
|
||||||
|
exit 130
|
||||||
|
}
|
||||||
Reference in new issue
Block a user