#!/usr/bin/env bash
# shellcheck disable=2317
PING_ITER=16
PROGRESS_BAR_CHAR=(█ ▒)

command() {
    local -r host=${1?"first parameter must be an host"}
    local -r iteration=${2?"second parameter must be an iteration number"}
    local -r cmd=(ping -c "${iteration}" "${host}")

    "${cmd[@]}"
}

draw_progressbar() {
    local -r progress=${1?"progress is mandatory"}
    local -r total=${2?"total is mandatory"}
    local progress_segment
    local todo_segment

    printf -v progress_segment "%${progress}s" ""
    printf -v todo_segment "%$((total - progress))s" ""
    printf >&2 "%s%s\r" \
        "${progress_segment// /${PROGRESS_BAR_CHAR[0]}}" \
        "${todo_segment// /${PROGRESS_BAR_CHAR[1]}}"

}

parse_output() {
    while read -r line; do
        if [[ "$line" =~ icmp_seq=([[:digit:]]{1,}).*time=(.*) ]]; then
            draw_progressbar "${BASH_REMATCH[1]}" "$PING_ITER"
        fi
    done
}

main() {
    command "aquilenet.fr" "$PING_ITER" > >(parse_output)
}

main
exit 0
# [...]
