Command/ProgressContainerAwareCommand.php
changeset 42 0e57c730bb18
equal deleted inserted replaced
39:b403086580f7 42:0e57c730bb18
       
     1 <?php
       
     2 /*
       
     3  * This file is part of the WikiTagBundle package.
       
     4  *
       
     5  * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     6  *
       
     7  * For the full copyright and license information, please view the LICENSE
       
     8  * file that was distributed with this source code.
       
     9  */
       
    10 namespace IRI\Bundle\WikiTagBundle\Command;
       
    11 
       
    12 use Symfony\Component\Console\Output\OutputInterface;
       
    13 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
       
    14 
       
    15 abstract class ProgressContainerAwareCommand extends ContainerAwareCommand
       
    16 {
       
    17     protected function showProgress(OutputInterface $output, $current, $total, $label, $width)
       
    18     {
       
    19         $percent = (floatval($current)/floatval($total)) * 100.0;
       
    20         $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
       
    21         $spaces = $width - $marks;
       
    22         
       
    23         $status_bar="\r[";
       
    24         $status_bar.=str_repeat("=", $marks);
       
    25         if($marks<$width){
       
    26             $status_bar.=">";
       
    27             $status_bar.=str_repeat(" ", $spaces);
       
    28         } else {
       
    29             $status_bar.="=";
       
    30         }
       
    31         
       
    32         $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
       
    33         
       
    34         $label = str_pad(substr($label,0,50), 50, " ");
       
    35         $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
       
    36         
       
    37         $status_bar.="] $disp%  $current_str/$total : $label";
       
    38         
       
    39         $output->write("$status_bar  ");
       
    40         
       
    41         if($current == $total) {
       
    42             $output->writeln("");
       
    43         }
       
    44     }
       
    45     
       
    46 }