PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 27th, 2004, 10:20 AM
errolbert errolbert is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Oxford, MS USA
Posts: 2 errolbert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post PHP CLI Progress Indicator

I don't know if this is useful to anyone else, but I saw on the Perl forums someone else was looking for a solution to this . I finally had to break out my ASCII tables to figure out \010 is the backspace character I needed.

Anyway, if anyone is interested, I've written a simple progess bar for PHP command line interface (CLI) scripts. I've posted it, including comments, below.

PHP Code:
function progressBar($current$total$label)
{
    
// This function assumes that you start with completion of 0%.
    
    // If the first time you call this function is with 1%
    // completion, you will delete the last 106 characters of
    // output from your program.
    
    // If this is the case, simply call this function before with
    // a hard coded 0.
    
    // check to see if this is the first go-round
    
if ($current == 0)
    {
        
// this is the first time so output the progess bar label
        
if ($label == "")
            echo 
"Progress: ";
        else if (
$label != "none")
            echo 
$label;
        
        
// start the bar with a nice edge
        
echo "|";
    }
    else
    {
        
// this isn't the first time so remove the previous progress bar
        
for ($place 106$place >= 0$place--)
        {
            
// echo a backspace to remove the previous character
            
echo "\010";
        }
    }
    
    
// output the progess bar as it should be
    
for ($place 0$place <= 100$place++)
    {
        
// output stars if we're finished through this point
        // or spaces if not
        
if ($place <= ($current $total 100))
            echo 
"*";
        else
            echo 
" ";
    }
    
    
// end the bar with a nice edge and a label
    
echo "| 100%";
    
    
// check to see if this is the last go-round
    
if ($current == $total)
    {
        
// this is the end of the progress bar, output an end of line
        
echo "\n";
    }


Reply With Quote
  #2  
Old May 27th, 2004, 10:31 AM
draelon draelon is offline
Dissident
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2003
Location: New York
Posts: 1,671 draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 8 h 8 m 13 sec
Reputation Power: 46
nice, but who uses php in command line? hehe
Comments on this post
kicken disagrees: I do
Noodles23 disagrees: So do I!
__________________
Draelon


PHP Manual :: MySQL Manual :: How to Ask Questions the Smart Way
=======================================================

Reply With Quote
  #3  
Old August 5th, 2009, 01:10 PM
mikiroro mikiroro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 2 mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 m 13 sec
Reputation Power: 0
Not bad, but I've improved it.

PHP Code:
function progressBar($current=0$total=100$size=50)
{
  
// first call must have $current=0,
  // otherwise you'll delete some last
  // part of your's app output
    
  // percent indicator must be four characters, if shorter, add some spaces
  
$perc = ($current/$total)*100;
  for(
$i=strlen($perc); $i<=4$i++)
    
$perc ' '.$perc;

  
$total_size $size $i 3;

  
// if it's not first go, remove the previous bar
  
if($current 0)
  {
    for(
$place $total_size$place 0$place--)
    {
      
// echo a backspace (hex:08) to remove the previous character
      
echo "\x08";
    }
  }
    
  
// output the progess bar as it should be
  
for($place 0$place <= $size$place++)
  {
    
// output green spaces if we're finished through this point
    // or grey spaces if not
    
if($place <= ($current $total $size))
      echo 
' ';
    else
      echo 
' ';
  }
    
  
// end a bar with a percent indicator
  
echo " $perc%";
    
  if(
$current == $total)
  {
    
// if it's the end, add a new line
    
echo "\n";
  }



@draelon: I do
Comments on this post
Noodles23 agrees: Nice work

Reply With Quote
  #4  
Old July 26th, 2010, 03:09 PM
smokes2345 smokes2345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 1 smokes2345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 54 sec
Reputation Power: 0
Another update

I know this is a fairly old thread but someone might stumble upon it as I have recently. Here is another version of mikiroro's bar with further improvments.
PHP Code:
function progressBar($current=0$total=100$label=""$size=50) {

    
//Don't have to call $current=0
    //Bar status is stored between calls
    
static $bars;
    if(!isset(
$bars[$label])) {
        
$new_bar TRUE;
        
fputs(STDOUT,"$label Progress:\n");
    }
    if(
$current == $bars[$label]) return 0;

    
$perc round(($current/$total)*100,2);        //Percentage round off for a more clean, consistent look
    
for($i=strlen($perc); $i<=4$i++) $perc ' '.$perc;    // percent indicator must be four characters, if shorter, add some spaces

    
$total_size $size $i 3;
    
// if it's not first go, remove the previous bar
    
if(!$new_bar) {
        for(
$place $total_size$place 0$place--) echo "\x08";    // echo a backspace (hex:08) to remove the previous character
    
}
    
    
$bars[$label]=$current//saves bar status for next call
    // output the progess bar as it should be
    
for($place 0$place <= $size$place++) {
        if(
$place <= ($current $total $size)) echo ' ';    // output green spaces if we're finished through this point
        
else echo ' ';                    // or grey spaces if not
    
}

    
// end a bar with a percent indicator
    
echo " $perc%";

    if(
$current == $total) {
        echo 
"\n";        // if it's the end, add a new line
        
unset($bars[$label]);
    }


Reply With Quote
  #5  
Old July 29th, 2010, 04:56 AM
mikiroro mikiroro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 2 mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level)mikiroro User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 m 13 sec
Reputation Power: 0
Instead of adding spaces by loop, you can use str_pad() function.
Comments on this post
fubes2000 disagrees: 6-year old threads do not need this many bumps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP CLI Progress Indicator


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap