lib/progress.sh

809 B · 34 lines · bash

1#!/usr/bin/env bash
2# Shared download progress bar for install.sh and brt -update.
3
4brt_progress_bar() {
5 local current="$1"
6 local total="$2"
7 local label="${3:-}"
8 local width=32
9 local pct=0
10 local filled=0
11 local empty=$width
12
13 if (( total > 0 )); then
14 pct=$(( current * 100 / total ))
15 filled=$(( current * width / total ))
16 empty=$(( width - filled ))
17 fi
18
19 label="${label:0:24}"
20
21 local bar empty_bar
22 bar=$(printf "%${filled}s" "" | tr ' ' '#')
23 empty_bar=$(printf "%${empty}s" "" | tr ' ' '-')
24
25 printf '\r\033[K \033[36m[\033[0m%s%s\033[36m]\033[0m %3d%% (%d/%d) %-24s' \
26 "$bar" "$empty_bar" "$pct" "$current" "$total" "$label"
27 if (( current >= total )); then printf '\n'; fi
28}
29
30# install.sh uses this name before brt exists
31progress_bar() {
32 brt_progress_bar "$@"
33}
34