Skip to content

Instantly share code, notes, and snippets.

@x42en
Created August 4, 2022 12:52
Show Gist options
  • Save x42en/7a87c9800a41e82fe9bfbc32a0aa7335 to your computer and use it in GitHub Desktop.
Save x42en/7a87c9800a41e82fe9bfbc32a0aa7335 to your computer and use it in GitHub Desktop.
HTOP like color bar - BASH progress bar
#!/bin/bash
ansi() { printf "\e[${1}m${*:2}\e[0m"; }
bold() { ansi 1 "$@"; }
dgreen() { ansi "1;32" "$@"; }
green() { ansi "1;92" "$@"; }
yellow() { ansi "1;93" "$@"; }
orange() { ansi "38;5;214" "$@"; }
red() { ansi "1;91" "$@"; }
lblue() { ansi "1;96" "$@"; }
gray() { ansi 90 "$@"; }
progressBar() {
# Convert float to int
current="${1%.*}"
# Use 100 as default total
total=${2:-100}
total="${total%.*}"
# Default bar width if not set
barwidth="${3:-40}"
# car='█'
car='|'
# Get percent of task done
let progress=${current}*100/${total}
let realized=(${progress}*${barwidth})/100
# Define steps
let step1=${barwidth}/3
let step2=${barwidth}*3/5
let step3=${barwidth}*9/10
for c in $(seq 1 $barwidth);
do
# Only display done part
if [[ ${c} -gt ${realized} ]]; then
printf ' '
# On any other case display colorized '#'
elif [[ ${c} -lt ${step1} ]]; then
dgreen $car
elif [[ ${c} -lt ${step2} ]]; then
green $car
elif [[ ${c} -lt ${step3} ]]; then
yellow $car
# elif [[ ${c} -lt ${step4} ]]; then
# bold `orange $car`
else
red $car
fi
done
gray " ${progress}%%"
}
echo '[+] Progress bar with percent value'
progressBar 68
echo ''
echo '[+] Progress bar with max set by user'
progressBar 184 213
echo ''
echo '[+] Progress bar width set by user'
progressBar 93 100 150
echo ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment