PHP Classes

PHP CLI Progress Bar: Show a progress bar in a console terminal

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 77 All time: 10,134 This week: 206Up
Version License PHP version Categories
php_component_cli_pr 1.0GNU Lesser Genera...5PHP 5, Tools, Console
Description 

Author

This class can show a progress bar in a console terminal.

It can display a progress bar in a way that it shows the number of steps that were performed for a task divided in given number of smaller tasks.

The class can advance to the next step until all the steps are done and it updates the ways the progress bar appears to reflect the proportion of steps that were done compared to the total number steps.

Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 150195 in Germany Germany
Week rank: 112 Up4 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Example

#!/bin/php
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-05-24
 */

require_once __DIR__ . '/../vendor/autoload.php';

$numberOfItems = [
   
3,6,9,12,150
];

echo
'+-----------------------------+' . PHP_EOL;
echo
'| Example of the progress bar |' . PHP_EOL;
echo
'+-----------------------------+' . PHP_EOL;
echo
'This demo will demonstrate ' . count($numberOfItems) . ' progress bars.' . PHP_EOL;
echo
'The number of items will be ' . implode(', ', $numberOfItems) . '.' . PHP_EOL;
echo
'The current update interval is 1 second.' . PHP_EOL;
echo
PHP_EOL;
echo
'Press CTRL-C to stop this demonstration.' . PHP_EOL;
echo
PHP_EOL;

$progressBar = new \Net\Bazzline\Component\Cli\ProgressBar\ProgressBar();

foreach (
$numberOfItems as $totalNumberOfItems) {
   
$items = [];
   
$startTime = microtime(true);

    for (
$iterator = 0; $iterator < $totalNumberOfItems; ++$iterator) {
       
$items[] = $iterator;
    }
   
$totalNumberOfItems = count($items);

   
$progressBar->setTotalSteps($totalNumberOfItems);

    foreach (
$items as $key => $item) {
       
//$progressBar->update($key);
       
$progressBar->forward();
       
sleep(1);
    }

   
$totalTime = microtime(true) - $startTime;

    echo
PHP_EOL;
    echo
'total number of items: ' . $totalNumberOfItems . PHP_EOL;
    echo
'total time: ' . (int) $totalTime . ' seconds' . PHP_EOL;
}


Details

Progress Bar for PHP CLI Scripts / Shell Environment

This project aims to deliver a easy to use php component for displaying a progress bar in the shell environment.

The current change log can be found here.

Usage

//assuming $items contains an array of items
$progressBar = new \Net\Bazzline\Component\Cli\ProgressBar\ProgressBar();

$progressBar->setTotalSteps(count($items));

foreach ($items as $item) {
    //do something with the item
    $progressBar->forward();
}
//to be on the save side, tell the progress bar that the show is over ;-)
$progressBar->isFinished();

Example

cd <project root>
./example/run.php

+-----------------------------+
| Example of the progress bar |
+-----------------------------+
This demo will demonstrate 5 progress bars.
The number of items will be 3, 6, 9, 12, 150.
The current update interval is 1 second.

Press CTRL-C to stop this demonstration

========================================================================>

Links (other good projects)

  • https://github.com/levi-putna/phpcli
  • https://github.com/guiguiboy/PHP-CLI-Progress-Bar
  • https://nixshell.wordpress.com/2009/09/08/get-the-width-of-the-terminal/

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it :-D.


  Files folder image Files  
File Role Description
Files folder imageexample (1 file)
Files folder imagesource (1 file)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file run.php Example Example script

  Files folder image Files  /  source  
File Role Description
  Plain text file ProgressBar.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:77
This week:0
All time:10,134
This week:206Up