Skip to content

Instantly share code, notes, and snippets.

@WayneSan
Last active December 17, 2015 02:48
Show Gist options
  • Select an option

  • Save WayneSan/5538108 to your computer and use it in GitHub Desktop.

Select an option

Save WayneSan/5538108 to your computer and use it in GitHub Desktop.

Revisions

  1. WayneSan revised this gist May 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion progress_bar.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/sh
    #!/bin/bash

    #### Preprocessing ####
    if [ "$1" == "--color" ]; then COLOR="TRUE"; shift; fi
  2. WayneSan renamed this gist May 8, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. WayneSan created this gist May 8, 2013.
    38 changes: 38 additions & 0 deletions progess_bar.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/bin/sh

    #### Preprocessing ####
    if [ "$1" == "--color" ]; then COLOR="TRUE"; shift; fi

    if [ $# -lt 2 ]; then
    echo "Usage: $0 [--color] TITLE PROGRESS [MAXIMUM]"
    exit
    fi

    if [ $# -ge 3 ] && [ $3 -gt 0 ]; then MAX=$3; else MAX=100; fi

    PRG=$2
    if [ $PRG -gt $MAX ]; then
    echo "Error: PROGRESS needs smaller than MAXIMUM."
    exit
    fi


    #### Main Program ####
    echo -en '\r'
    echo -n "$1: "

    LI=('\' '|' '/' '-')
    let MAXL=`echo $MAX | wc -m`-1
    let COLS=`tput cols`-`echo $1 | wc -m`-$((MAXL * 2))-23
    let PEC=(PRG*100)/MAX

    echo -n '[ '
    PRGBAR=`printf '%*s' $((PEC * COLS / 100)) | tr ' ' '#'`
    [ "$COLOR" == "TRUE" ] && echo -en "\033[1;3$(($PRG % 8))m"
    printf "%-${COLS}.${COLS}s" "$PRGBAR${LI[$PRG % 4]}"
    [ "$COLOR" == "TRUE" ] && echo -en "\033[m"
    #echo -n " ] $PEC%"
    #echo -n " ($PRG/$MAX)... "
    printf " ] %3s%% (%${MAXL}s/%${MAXL}s)... " "$PEC" "$PRG" "$MAX"

    [ $PRG -ge $MAX ] && echo 'done.'