Skip to content

Instantly share code, notes, and snippets.

@ConnerWill
Created September 3, 2022 02:29
Show Gist options
  • Save ConnerWill/b08353dfab58c4b4ee7df53b362feaab to your computer and use it in GitHub Desktop.
Save ConnerWill/b08353dfab58c4b4ee7df53b362feaab to your computer and use it in GitHub Desktop.
Bash Progress Bar
#!/bin/env bash
### FUNCTION ###{{{
function bash_progress_bar(){
## Localize Variables ##{{{
local \
messege \
bar \
color \
bar_color_fg_setup \
bar_color_bg_setup \
bar_color \
bar_color_reset \
sleep_time \
left
##}}}
## Define Variables ##{{{
## Define Config Variables ##{{{
sleep_time=0.02
color="93m" #White:"255m" #Red:"196m" #Blue:"21m" #Yellow:"190m" #Lime:"46m" #Magenta:"201m"
messege_color='\e[0;1;38;5;255m' #Magenta Background Bold White Foreground \e[0;1;38;5;255;48;5;201m
progress_color='\e[0;38;5;7m' #White Background Bold Magenta Foreground \e[0;1;38;5;201;48;5;255m
##}}}
## Define Static Variables ##{{{
[[ -z "${messege}" ]] && messege="${*}"
bar=''
bar_color_fg_setup="\e[0;38;5;$color"
bar_color_bg_setup="\e[48;5;$color"
bar_color="${bar_color_fg_setup}${bar_color_bg_setup}"
bar_color_reset='\e[0m'
color_reset='\e[0m'
##}}}
##}}}
## Help Menu ##{{{
## Define Help Menu Variables ##{{{
help_color_title='\e[0;1;4;38;5;4m'
help_color_command='\e[0;38;5;10m'
help_color_option='\e[0;3;38;5;11m'
help_color_option_sep='\e[0;38;5;7m'
help_color_description='\e[0;3;38;5;7m'
help_color_dollar='\e[0;38;5;8m'
##}}}
## Display Help Menu ##{{{
if [[ "${messege}" == "-h" ]] || [[ "${messege}" == "--help" ]]; then
printf "
${help_color_title}NAME${color_reset}
${help_color_command}%s${color_reset}
${help_color_title}USAGE${color_reset}
${help_color_command}%s${color_reset} ${help_color_option_sep}[${help_color_option}-h${help_color_option_sep}|${help_color_option}--help${help_color_option_sep}]${color_reset} ${help_color_option_sep}[${help_color_option}messege${help_color_option_sep}]${color_reset}
${help_color_title}EXAMPLE${color_reset}
${help_color_description}Show progress bar with no messege
${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset}
${bar_color}█████████████${bar_color_reset} ${progress_color}19%%${bar_color_reset}
${help_color_title}EXAMPLE${color_reset}
${help_color_description}Show progress bar with the messege: 'Loading'
${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset} ${help_color_option}\"Loading\"${color_reset}
${messege_color}Loading:${bar_color_reset}${bar_color}█████████████████████████${bar_color_reset} ${progress_color}46%%${bar_color_reset}
${help_color_title}EXAMPLE${color_reset}
${help_color_description}Show progress bar with the messege: 'Current Progress' using no quotes
${help_color_dollar}\$${color_reset} ${help_color_command}%s${color_reset} ${help_color_option}Current Progress${color_reset}
${messege_color}Current Progress:${bar_color_reset}${bar_color}█████████████████████████████████████████████████${bar_color_reset} ${progress_color}91%%${bar_color_reset}
\n" "${0}" "${0}" "${0}" "${0}" "${0}"
return
fi
##}}}
##}}}
## Define Messege Variables ##{{{
[[ -n "${messege}" ]] && messege="${*}:"
##}}}
## Set Traps ##{{{
trap 'printf "\e[?25h\e[2K\r"; return' SIGHUP SIGINT SIGQUIT SIGABRT
##}}}
## Start Line/Cursor Effects ##{{{
printf "\e[?25l" ## Hide Cursor
##}}}
## Progress Bar Loop ##{{{
for (( x=0; x <= 100; x++ )); do
sleep $sleep_time ## Sleep
bar="${bar} " ## Redefine Bar
left="$(( 100 - x ))" ## Calculate/Define Left
printf "\r${messege_color}%s${bar_color}%s${bar_color_reset}" "${messege}" "${bar}" ## Print Bar
printf " %${left}s" ## Print Left
printf "${progress_color}%s%%${bar_color_reset}" "${x}" ## Print Left
done
##}}}
## End Line/Cursor Effects ##{{{
printf "\e[?25h" ## Restore Cursor
printf "\n" ## New Line
##}}}
}
###}}}
### CALL FUNCTION ###{{{
## Call 'progress_bar' Function ##{{{
bash_progress_bar "${@}"
##}}}
###}}}
### HELP ###{{{
#
# NAME
#
# ./bash_progress_bar.sh
#
#
# USAGE
#
# ./bash_progress_bar.sh [-h|--help] [messege]
#
#
# EXAMPLE
#
# Show progress bar with no messege
#
# $ ./bash_progress_bar.sh
#
# █████████████ 19%
#
#
# EXAMPLE
#
# Show progress bar with the messege: 'Loading'
#
# $ ./bash_progress_bar.sh "Loading"
#
# Loading:█████████████████████████ 46%
#
#
# EXAMPLE
#
# Show progress bar with the messege: 'Current Progress' using no quotes
#
# $ ./bash_progress_bar.sh Current Progress
#
# Current Progress:█████████████████████████████████████████████████ 91%
#
###}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment