src/CliLib.php
/**
     * Display a progress bar in the CLI. This will dynamically take up the full width of the 
     * terminal and if you keep calling this function, it will appear animated as the progress bar
     * keeps writing over the top of itself.
     * @param float $percentage - the percentage completed.
     * @param int $numDecimalPlaces - the number of decimal places to show for percentage output string
     */
Display a progress bar in the CLI. This will dynamically take up the full width of the
terminal and if you keep calling this function, it will appear animated as the progress bar
keeps writing over the top of itself.
@param float $percentage - the percentage completed.
@param int $numDecimalPlaces - the number of decimal places to show for percentage output string
Display a progress bar in the CLI . This will dynamically take up the full width of the terminal and if you keep calling this function it will appear animated as the progress bar keeps writing over the top of itself . @param float $percentage - the percentage completed . @param int $numDecimalPlaces - the number of decimal places to show for percentage output string
public static function showProgressBar($percentage, int $numDecimalPlaces)
    {
        $percentageStringLength = 4;
        if ($numDecimalPlaces > 0)
        {
            $percentageStringLength += ($numDecimalPlaces + 1);
        }
        $percentageString = number_format($percentage, $numDecimalPlaces) . '%';
        $percentageString = str_pad($percentageString, $percentageStringLength, " ", STR_PAD_LEFT);
        $percentageStringLength += 3; 
        $terminalWidth = `tput cols`;
        $barWidth = $terminalWidth - ($percentageStringLength) - 2; 
        $numBars = round(($percentage) / 100 * ($barWidth));
        $numEmptyBars = $barWidth - $numBars;
        $barsString = '[' . str_repeat("=", ($numBars)) . str_repeat(" ", ($numEmptyBars)) . ']';
        echo "($percentageString) " . $barsString . "\r";
    }
public static function showProgressBar ( $ percentage , int $ numDecimalPlaces ) { $ percentageStringLength = 4 ; if ( $ numDecimalPlaces > 0 ) { $ percentageStringLength += ( $ numDecimalPlaces + 1 ) ; } $ percentageString = number_format ( $ percentage , $ numDecimalPlaces ) . '%' ; $ percentageString = str_pad ( $ percentageString , $ percentageStringLength , " " , STR_PAD_LEFT ) ; $ percentageStringLength += 3 ; $ terminalWidth = ` tput cols ` ; $ barWidth = $ terminalWidth - ( $ percentageStringLength ) - 2 ; $ numBars = round ( ( $ percentage ) / 100 * ( $ barWidth ) ) ; $ numEmptyBars = $ barWidth - $ numBars ; $ barsString = '[' . str_repeat ( " = " , ( $ numBars ) ) . str_repeat ( " " , ( $ numEmptyBars ) ) . ']' ; echo " ( $ percentageString )  " . $ barsString . " \r " ; }
Display a progress bar in the CLI.
Display a progress bar in the CLI .
// add 2 for () and a space before bar starts. // subtract 2 for [] around bar