├── .gitattributes ├── LICENSE ├── README.md └── loadbar /.gitattributes: -------------------------------------------------------------------------------- 1 | *.gif export-ignore 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Naivecoders 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Loadbar: a shell progress bar 2 | ============================= 3 | 4 | Loadbar is a POSIX shell script that display a progress bar on your terminal. 5 | It reads values from stdin and compute a percentage (assuming max value is 100 if no option passed, see below). 6 | 7 | It support these different options: 8 | 9 | - `-t [VALUE]`, `--title [VALUE]`: print a label before the progress bar 10 | - `-m [VALUE]`, `--max [VALUE}`: define the maximum value to read to consider the progress 100% 11 | - `-p`, `--percentage`: display the percentage of the progression 12 | - `-v`, `--value`: display the value of the progression 13 | - `-f [CHAR]`, `--fill [CHAR]`: pick a different character to use for the fill part of the progress bar 14 | - `-b [CHAR]`, `--background [CHAR]`: pick a different character to use for the background part of the progress bar 15 | - `-n`, `--no-color`: do not use any color for the display bar 16 | - `-s`, `--size`: define the size (text included) of the bar. Take full length if not defined. 17 | - `-h`, `--help`: display help 18 | 19 | ![demonstration](./loadbar_demo.gif) 20 | -------------------------------------------------------------------------------- /loadbar: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | trap "tput cnorm" EXIT 4 | tput civis 5 | 6 | tput sc 7 | trap "tput rc; tput ed" WINCH 8 | 9 | print_usage() { 10 | cat <&1 29 | exit 1 30 | } 31 | } 32 | 33 | parse_args() { 34 | while getopts ":t:m:s:f:b:pvnh" option "$@"; do 35 | case "${option}" in 36 | t) title="${OPTARG}" ;; 37 | m) max="${OPTARG}" ;; 38 | s) size="${OPTARG}" ;; 39 | p) show_percentage=true ;; 40 | v) show_value=true ;; 41 | f) fill_character="${OPTARG}" ;; 42 | b) background_character="${OPTARG}" ;; 43 | n) show_no_color=true ;; 44 | h) 45 | print_usage 46 | exit 0 47 | ;; 48 | *) 49 | echo "Invalid arg: ${option}" >&2 50 | print_usage 51 | exit 1 52 | ;; 53 | esac 54 | done 55 | } 56 | 57 | parse_long() { 58 | i=0 59 | args_count=$# 60 | while [ $i -lt $args_count ]; do 61 | i=$((i + 1)) 62 | case "$1" in 63 | --title) 64 | title="$2" 65 | shift 2 66 | i=$((i += 1)) continue 67 | ;; 68 | --title=*) 69 | title="${1#*=}" 70 | shift 71 | continue 72 | ;; 73 | --max-value) 74 | max="$2" 75 | shift 2 76 | i=$((i += 1)) 77 | continue 78 | ;; 79 | --max-value=*) 80 | max="${1#*=}" 81 | shift 82 | continue 83 | ;; 84 | --size) 85 | size="$2" 86 | shift 2 87 | i=$((i += 1)) 88 | continue 89 | ;; 90 | --size=*) 91 | size="${1#*=}" 92 | shift 93 | continue 94 | ;; 95 | --fill) 96 | fill_character="$2" 97 | shift 2 98 | i=$((i += 1)) 99 | continue 100 | ;; 101 | --fill=*) 102 | fill_character="$2" 103 | shift 104 | i=$((i += 1)) 105 | continue 106 | ;; 107 | --background) 108 | background_character="$2" 109 | shift 2 110 | i=$((i += 1)) 111 | continue 112 | ;; 113 | --background=*) 114 | background_character="${1#*=}" 115 | shift 2 116 | i=$((i += 1)) 117 | continue 118 | ;; 119 | --percentage) 120 | show_percentage=true 121 | shift 122 | continue 123 | ;; 124 | --value) 125 | show_value=true 126 | shift 127 | continue 128 | ;; 129 | --no-color) 130 | show_no_color=true 131 | shift 132 | continue 133 | ;; 134 | --help) 135 | print_usage 136 | exit 0 137 | continue 138 | ;; 139 | *) 140 | set -- "$@" "$1" 141 | shift 142 | ;; 143 | esac 144 | done 145 | parse_args "$@" 146 | } 147 | 148 | init_values() { 149 | [ -z "${title:-}" ] && title="" || title="$title " 150 | [ -z "${show_percentage:-}" ] && show_percentage='' 151 | [ -z "${show_value:-}" ] && show_value='' 152 | title_size="${#title}" 153 | max=${max:-100} 154 | value_progress=" [${max}/${max}]" 155 | value_progress_size=${#value_progress} 156 | percentage=" [100%]" 157 | percentage_size=${#percentage} 158 | fill_character=${fill_character:-█} 159 | background_character=${background_character:-░} 160 | fill_color=$(tput setaf 2) 161 | reset_color=$(tput sgr0) 162 | } 163 | 164 | validate_values() { 165 | [ ! "${#fill_character}" -eq 1 ] && [ ! "$(printf "%s" "$fill_character" | wc -m)" -eq 1 ] && echo "Invalid character ${fill_character}: Fill charachter should be of size 1" >&2 && exit 1 166 | [ ! ${#background_character} -eq 1 ] && [ ! "$(printf "%s" "$background_character" | wc -m)" -eq 1 ] && echo "Invalid character '${background_character}': Background character should be of size 1" >&2 && exit 1 167 | return 0 168 | } 169 | 170 | is_number() { 171 | printf '%d' "${1}" >/dev/null 2>&1 172 | } 173 | 174 | validate_value() { 175 | is_number "$1" && [ "$1" -le "$max" ] && [ "$1" -ge 0 ] 176 | } 177 | 178 | compute_bar_size() { 179 | bar_size=${size:-$(tput cols)} 180 | [ -z "$title" ] || bar_size=$((bar_size - title_size)) 181 | [ -z "$show_percentage" ] || bar_size=$((bar_size - percentage_size)) 182 | [ -z "$show_value" ] || bar_size=$((bar_size - value_progress_size)) 183 | } 184 | 185 | draw_character_on() { 186 | printf "%s" "$(printf ":%${2}s:" | sed -e "s/ /${1}/g" -e 's/://g')" 187 | } 188 | 189 | display_bar() { 190 | while read -r value; do 191 | validate_value $value || continue 192 | compute_bar_size 193 | [ -n "$title" ] && printf "%s" "$title" 194 | [ -z "$show_no_color" ] && printf "%b" "${fill_color}" 195 | percentage=$((value * 100 / max)) 196 | fill_size=$((bar_size * percentage / 100)) 197 | draw_character_on "${fill_character}" "$fill_size" 198 | empty_size=$((bar_size - fill_size)) 199 | draw_character_on "${background_character}" "${empty_size}" 200 | [ -z "$show_no_color" ] && printf "%b" "${reset_color}" 201 | [ -n "$show_percentage" ] && printf "%${percentage_size}s" "[${percentage}%]" 202 | [ -n "$show_value" ] && printf "%${value_progress_size}s" " [${value}/${max}]" 203 | printf "\r" 204 | done 205 | } 206 | 207 | parse_long "$@" 208 | init_values 209 | validate_values 210 | display_bar 211 | echo 212 | --------------------------------------------------------------------------------