#!/bin/bash # Progress Indicator Library for ROM Groomer # Provides comprehensive progress tracking for long-running operations # Global progress state PROGRESS_CURRENT_STEP=0 PROGRESS_TOTAL_STEPS=0 PROGRESS_CURRENT_OPERATION="" PROGRESS_START_TIME=0 PROGRESS_LAST_UPDATE=0 PROGRESS_SPINNER_PID=0 PROGRESS_BAR_WIDTH=50 PROGRESS_UPDATE_INTERVAL=0.1 # Unicode characters for progress display PROGRESS_FILLED="█" PROGRESS_EMPTY="░" PROGRESS_SPINNER_CHARS=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") # ANSI color codes PROGRESS_GREEN="\033[32m" PROGRESS_BLUE="\033[34m" PROGRESS_YELLOW="\033[33m" PROGRESS_RED="\033[31m" PROGRESS_CYAN="\033[36m" PROGRESS_RESET="\033[0m" PROGRESS_BOLD="\033[1m" # Check if progress indicators are available check_progress_support() { # Check if we're in a terminal that supports ANSI if [[ -t 1 ]] && command -v tput >/dev/null 2>&1; then return 0 fi return 1 } # Initialize progress system progress_init() { if check_progress_support; then PROGRESS_AVAILABLE="true" # Hide cursor for cleaner display printf "\033[?25l" # Set up cleanup trap with better signal handling trap 'progress_cleanup; exit 130' EXIT INT TERM # Make sure signals are not ignored trap - QUIT return 0 else PROGRESS_AVAILABLE="false" return 1 fi } # Cleanup progress system progress_cleanup() { if [[ $PROGRESS_AVAILABLE == "true" ]]; then # Stop any running spinner progress_spinner_stop # Show cursor again printf "\033[?25h" # Clear any remaining progress line printf "\r\033[K" # Move to next line for clean exit printf "\n" fi } # Calculate time difference in seconds progress_time_diff() { local start_time=$1 local current_time=$(date +%s) echo $((current_time - start_time)) } # Format duration as human readable progress_format_duration() { local seconds=$1 local hours=$((seconds / 3600)) local mins=$(((seconds % 3600) / 60)) local secs=$((seconds % 60)) if [[ $hours -gt 0 ]]; then printf "%dh %dm %ds" $hours $mins $secs elif [[ $mins -gt 0 ]]; then printf "%dm %ds" $mins $secs else printf "%ds" $secs fi } # Calculate ETA based on current progress progress_calculate_eta() { local current=$1 local total=$2 local elapsed=$3 # Validate inputs are numeric if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$total" =~ ^[0-9]+$ ]] || ! [[ "$elapsed" =~ ^[0-9]+$ ]]; then echo "N/A" return fi if [[ $current -eq 0 ]]; then echo "calculating..." return fi local rate=$(echo "scale=6; $current / $elapsed" | bc -l 2>/dev/null || awk "BEGIN{print $current / $elapsed}") local remaining=$((total - current)) local eta=$(echo "scale=0; $remaining / $rate" | bc -l 2>/dev/null || awk "BEGIN{printf \"%.0f\", $remaining / $rate}") progress_format_duration $eta } # Calculate processing rate progress_calculate_rate() { local current=$1 local elapsed=$2 # Validate inputs are numeric if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$elapsed" =~ ^[0-9]+$ ]]; then echo "0.0/s" return fi if [[ $elapsed -eq 0 ]]; then echo "0.0/s" return fi local rate=$(echo "scale=1; $current / $elapsed" | bc -l 2>/dev/null || awk "BEGIN{printf \"%.1f\", $current / $elapsed}") echo "${rate}/s" } # Show determinate progress bar progress_bar() { local current=$1 local total=$2 local label="$3" local extra_info="$4" if [[ $PROGRESS_AVAILABLE != "true" ]]; then return fi # Validate numeric inputs if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$total" =~ ^[0-9]+$ ]]; then # Invalid numeric inputs - skip progress display return fi # Calculate percentage local percent=0 if [[ $total -gt 0 ]]; then percent=$(echo "scale=1; ($current * 100) / $total" | bc -l 2>/dev/null || awk "BEGIN{printf \"%.1f\", ($current * 100) / $total}") fi # Calculate filled and empty segments local filled_width=$(echo "scale=0; ($current * $PROGRESS_BAR_WIDTH) / $total" | bc -l 2>/dev/null || awk "BEGIN{printf \"%.0f\", ($current * $PROGRESS_BAR_WIDTH) / $total}") local empty_width=$((PROGRESS_BAR_WIDTH - filled_width)) # Build progress bar local bar="" for ((i=0; i/dev/null wait $PROGRESS_SPINNER_PID 2>/dev/null PROGRESS_SPINNER_PID=0 printf "\r\033[K" # Clear spinner line fi } # Multi-step progress tracking progress_steps_init() { local total_steps=$1 PROGRESS_TOTAL_STEPS=$total_steps PROGRESS_CURRENT_STEP=0 PROGRESS_START_TIME=$(date +%s) if [[ $PROGRESS_AVAILABLE == "true" ]]; then echo -e "${PROGRESS_BOLD}Starting multi-step process (${total_steps} steps)${PROGRESS_RESET}" fi } # Update step progress progress_step() { local step_num=$1 local total_steps=$2 local step_name="$3" local status="$4" # "active", "complete", "error" if [[ $PROGRESS_AVAILABLE != "true" ]]; then return fi local icon="●" local color="$PROGRESS_BLUE" case "$status" in "complete") icon="✓" color="$PROGRESS_GREEN" ;; "error") icon="❌" color="$PROGRESS_RED" ;; "active") icon="●" color="$PROGRESS_YELLOW" ;; esac printf "${color}Step %d/%d: %s %s${PROGRESS_RESET}\n" \ "$step_num" "$total_steps" "$icon" "$step_name" } # File operation progress progress_file_operation() { local operation="$1" # "Copying", "Moving", "Deleting" local current=$2 local total=$3 local source_file="$4" local dest_file="$5" if [[ $PROGRESS_AVAILABLE != "true" ]]; then return fi local label="$operation files" local extra_info="" # Show current file being processed if [[ -n "$source_file" ]]; then local filename=$(basename "$source_file") if [[ ${#filename} -gt 40 ]]; then filename="${filename:0:37}..." fi extra_info="$filename" fi progress_bar "$current" "$total" "$label" "$extra_info" } # Batch operation progress with summary progress_batch_start() { local operation_name="$1" local total_items=$2 PROGRESS_BATCH_NAME="$operation_name" PROGRESS_BATCH_TOTAL=$total_items PROGRESS_BATCH_SUCCESS=0 PROGRESS_BATCH_FAILED=0 PROGRESS_BATCH_CURRENT=0 PROGRESS_START_TIME=$(date +%s) if [[ $PROGRESS_AVAILABLE == "true" ]]; then echo -e "${PROGRESS_BOLD}Starting ${operation_name} (${total_items} items)${PROGRESS_RESET}" fi } # Update batch progress progress_batch_update() { local item_name="$1" local status="$2" # "success", "failed", "processing" case "$status" in "success") ((PROGRESS_BATCH_SUCCESS++)) ((PROGRESS_BATCH_CURRENT++)) ;; "failed") ((PROGRESS_BATCH_FAILED++)) ((PROGRESS_BATCH_CURRENT++)) ;; "processing") # Don't increment counters for processing status ;; esac if [[ $PROGRESS_AVAILABLE == "true" ]]; then local extra_info="$item_name" if [[ ${#extra_info} -gt 40 ]]; then extra_info="${extra_info:0:37}..." fi # Ensure we have valid numeric values before calling progress_bar local current=${PROGRESS_BATCH_CURRENT:-0} local total=${PROGRESS_BATCH_TOTAL:-0} progress_bar "$current" "$total" "$PROGRESS_BATCH_NAME" "$extra_info" fi } # Finish batch operation with summary progress_batch_finish() { local final_status="$1" # "completed", "failed", "cancelled" if [[ $PROGRESS_AVAILABLE == "true" ]]; then printf "\n" # Move to next line after progress bar local elapsed=$(progress_time_diff $PROGRESS_START_TIME) local duration=$(progress_format_duration $elapsed) case "$final_status" in "completed") printf "${PROGRESS_GREEN}✓ %s completed in %s${PROGRESS_RESET}\n" \ "$PROGRESS_BATCH_NAME" "$duration" ;; "failed") printf "${PROGRESS_RED}❌ %s failed after %s${PROGRESS_RESET}\n" \ "$PROGRESS_BATCH_NAME" "$duration" ;; "cancelled") printf "${PROGRESS_YELLOW}⚠ %s cancelled after %s${PROGRESS_RESET}\n" \ "$PROGRESS_BATCH_NAME" "$duration" ;; esac # Show summary printf " ${PROGRESS_GREEN}Success: %d${PROGRESS_RESET}" "$PROGRESS_BATCH_SUCCESS" if [[ $PROGRESS_BATCH_FAILED -gt 0 ]]; then printf " | ${PROGRESS_RED}Failed: %d${PROGRESS_RESET}" "$PROGRESS_BATCH_FAILED" fi printf " | Total: %d\n" "$PROGRESS_BATCH_TOTAL" fi } # Simple percentage progress (for compatibility) progress_percent() { local current=$1 local total=$2 local label="$3" progress_bar "$current" "$total" "$label" } # Initialize progress system when sourced if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then # Script is being sourced progress_init fi