Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Bash progress wheel (See related posts)

// print a progress wheel

   1  
   2  #!/bin/bash
   3  #
   4  # anim.sh
   5  # print a progress wheel while test_pad returns 0
   6  #
   7  # $ANIM  : wheel
   8  # $PAD   : on what $ANIM moves
   9  # $SLEEP : timer
  10  #
  11  # Author : Nicolas Marciniak <nmarciniak@gmail.com>
  12  
  13  
  14  
  15  function test_pad()
  16  {
  17          return 0
  18  }
  19  
  20  function reverse()
  21  {
  22          string=$1
  23          len=$(echo -n $string | wc -c)
  24          while test $len -gt 0
  25          do
  26                  rev=$rev$(echo $string | cut -c $len)
  27                  len=$(( len - 1 ))
  28          done
  29          echo $rev
  30  }
  31  
  32  ANIM="//--\\||"
  33  PAD="                                                                  "
  34  SLEEP="0.1"
  35  
  36  ANIM_L="$(reverse $ANIM)"
  37  PAD_LN=${#PAD}
  38  ANIM_LN=${#ANIM}
  39  u=0; re=0; l=0
  40  
  41  while test_pad; do
  42          if [ $re -eq 1 ]; then L=$ANIM_L; else L=$ANIM; fi
  43          if [ $re -eq 1 ]; then p=$(( $PAD_LN - $u )); else p=$u; fi
  44          echo -ne "${PAD:0:$p} ${L:$l:1} ${PAD:$p:$PAD_LN} \r"
  45          if [ $u -eq $PAD_LN ]; then u=0; if [ $re -eq 1 ]; then re=0; else re=1; fi; fi
  46          u=$(( u + 1 )); l=$(( l + 1 ))
  47          if [ $l -eq $ANIM_LN ]; then l=0; fi
  48          sleep $SLEEP
  49  done
  50  exit 0
  51  
  52  

You need to create an account or log in to post comments to this site.


Click here to browse all 5521 code snippets

Related Posts