PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

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: 44
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: 1 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: 10 m 45 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
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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT