Posted By

iTony on 03/10/10


Tagged

php command cli line progress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

deanhouseholder
Priestd09


Command line progress bar with PHP


 / Published in: PHP
 

URL: http://brian.moonspot.net/php-progress-bar

  1. <?php
  2.  
  3. /*
  4.  
  5. Copyright (c) 2010, dealnews.com, Inc.
  6. All rights reserved.
  7.  
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10.  
  11.  * Redistributions of source code must retain the above copyright notice,
  12.   this list of conditions and the following disclaimer.
  13.  * Redistributions in binary form must reproduce the above copyright
  14.   notice, this list of conditions and the following disclaimer in the
  15.   documentation and/or other materials provided with the distribution.
  16.  * Neither the name of dealnews.com, Inc. nor the names of its contributors
  17.   may be used to endorse or promote products derived from this software
  18.   without specific prior written permission.
  19.  
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31.  
  32.  */
  33.  
  34. /**
  35.  * show a status bar in the console
  36.  *
  37.  * <code>
  38.  * for($x=1;$x<=100;$x++){
  39.  *
  40.  * show_status($x, 100);
  41.  *
  42.  * usleep(100000);
  43.  *
  44.  * }
  45.  * </code>
  46.  *
  47.  * @param int $done how many items are completed
  48.  * @param int $total how many items are to be done total
  49.  * @param int $size optional size of the status bar
  50.  * @return void
  51.  *
  52.  */
  53.  
  54. function show_status($done, $total, $size=30) {
  55.  
  56. static $start_time;
  57.  
  58. // if we go over our bound, just ignore it
  59. if($done > $total) return;
  60.  
  61. if(empty($start_time)) $start_time=time();
  62. $now = time();
  63.  
  64. $perc=(double)($done/$total);
  65.  
  66. $bar=floor($perc*$size);
  67.  
  68. $status_bar="\r[";
  69. $status_bar.=str_repeat("=", $bar);
  70. if($bar<$size){
  71. $status_bar.=">";
  72. $status_bar.=str_repeat(" ", $size-$bar);
  73. } else {
  74. $status_bar.="=";
  75. }
  76.  
  77. $disp=number_format($perc*100, 0);
  78.  
  79. $status_bar.="] $disp% $done/$total";
  80.  
  81. $rate = ($now-$start_time)/$done;
  82. $left = $total - $done;
  83. $eta = round($rate * $left, 2);
  84.  
  85. $elapsed = $now - $start_time;
  86.  
  87. $status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec.";
  88.  
  89. echo "$status_bar ";
  90.  
  91. flush();
  92.  
  93. // when done, send a newline
  94. if($done == $total) {
  95. echo "\n";
  96. }
  97.  
  98. }
  99.  
  100. ?>

Report this snippet  

You need to login to post a comment.