Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line progress bar in PHP

Tags:

php

I am currently trying to add a progress bar to a command line script and I've tried various solutions (including Zend and Console_ProgressBar). The problem they both have in common is that the progress bar doesn't stick at the bottom of the window because during the script, new lines and other information is outputted.

Is there any way to keep the progress bar at the bottom of the terminal but still be able to output other information while the script is running?

[Edit]

I figured it out:

Instead of outputting directly to STDOUT I am actually grabbing the output inside a variable, I erase the screen with echo chr(27) . '[2J' and then output to STDOUT the contents of the variable and then append my progress bar.

Hope that makes sense :)

like image 215
Andrei Serdeliuc ॐ Avatar asked Jan 23 '10 18:01

Andrei Serdeliuc ॐ


People also ask

What is PHP CLI command?

PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.

Can we write command line script in PHP?

Yes, we can create a command-line PHP script as we do for web script, but with few little tweaks. We won't be using any kind of HTML tags in command-line scripting, as the output is not going to be rendered in a web browser, but displayed in the DOS prompt / Shell prompt.


1 Answers

This is nice progress bar for cli:

http://snipplr.com/view/29548/

<?php  /*  Copyright (c) 2010, dealnews.com, Inc. All rights reserved.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:   * Redistributions of source code must retain the above copyright notice,    this list of conditions and the following disclaimer.  * Redistributions in binary form must reproduce the above copyright    notice, this list of conditions and the following disclaimer in the    documentation and/or other materials provided with the distribution.  * Neither the name of dealnews.com, Inc. nor the names of its contributors    may be used to endorse or promote products derived from this software    without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   */  /**  * show a status bar in the console  *   * <code>  * for($x=1;$x<=100;$x++){  *   *     show_status($x, 100);  *   *     usleep(100000);  *                             * }  * </code>  *  * @param   int     $done   how many items are completed  * @param   int     $total  how many items are to be done total  * @param   int     $size   optional size of the status bar  * @return  void  *  */  function show_status($done, $total, $size=30) {      static $start_time;      // if we go over our bound, just ignore it     if($done > $total) return;      if(empty($start_time)) $start_time=time();     $now = time();      $perc=(double)($done/$total);      $bar=floor($perc*$size);      $status_bar="\r[";     $status_bar.=str_repeat("=", $bar);     if($bar<$size){         $status_bar.=">";         $status_bar.=str_repeat(" ", $size-$bar);     } else {         $status_bar.="=";     }      $disp=number_format($perc*100, 0);      $status_bar.="] $disp%  $done/$total";      $rate = ($now-$start_time)/$done;     $left = $total - $done;     $eta = round($rate * $left, 2);      $elapsed = $now - $start_time;      $status_bar.= " remaining: ".number_format($eta)." sec.  elapsed: ".number_format($elapsed)." sec.";      echo "$status_bar  ";      flush();      // when done, send a newline     if($done == $total) {         echo "\n";     }  }  ?> 
like image 143
user956584 Avatar answered Sep 28 '22 12:09

user956584