#!/usr/bin/env bash
# Shared download progress bar for install.sh and brt -update.

brt_progress_bar() {
  local current="$1"
  local total="$2"
  local label="${3:-}"
  local width=32
  local pct=0
  local filled=0
  local empty=$width

  if (( total > 0 )); then
    pct=$(( current * 100 / total ))
    filled=$(( current * width / total ))
    empty=$(( width - filled ))
  fi

  label="${label:0:24}"

  local bar empty_bar
  bar=$(printf "%${filled}s" "" | tr ' ' '#')
  empty_bar=$(printf "%${empty}s" "" | tr ' ' '-')

  printf '\r\033[K  \033[36m[\033[0m%s%s\033[36m]\033[0m %3d%% (%d/%d) %-24s' \
    "$bar" "$empty_bar" "$pct" "$current" "$total" "$label"
  if (( current >= total )); then printf '\n'; fi
}

# install.sh uses this name before brt exists
progress_bar() {
  brt_progress_bar "$@"
}
