Skip to content

Console_ProgressBar

StefansArya edited this page Nov 12, 2020 · 4 revisions

To use this feature you need to create new ProgressBar object;

$bar = new \Scarlets\Console\ProgressBar();

// It's better to hide the caret/cursor
\Scarlets\Console::hideCursor();

After creating new progress bar object, you could append many bar inside of it.
But usually the windows CLI can't reload the caret position and console would write the progress bar on the new line instead rewriting the last bar. In this case you need to clear the console first or scroll the console to the bottom.

Adding bar

The first parameter is the bar title, and the second parameter is max value of that bar.
This function will return index number that can be used for updating bar value.

add($title, $maxValue)

$index = $bar->add('Yahoo', 100);

Updating bar value

The first parameter is the bar index, and the second parameter is the new value of that bar.
The third parameter is used if you want to append text in the middle of the bar.
The fourth parameter is used if you want to append text after the bar was rendered.

update($index, $newValue, $barText = '', $endText = '')

for($i=0; $i <= 100; $i++){
    $bar->update($index, $i, "$i/100", rand(100, 200).' KB/s');
}

cli_progress_bar

Modify bar style

There are 3 customizable part for the bar design:

  • Completed bar
  • On progress bar
  • Waiting bar

Every part can be designed with Console::style and with custom symbol.
These part already have default design, so you don't need to customize it if you prefer.

Completed bar

The first parameter can be the custom character and leave the second parameter empty.
You could also use text styling by passing style format on first parameter and custom character on second parameter, the style format must have '%s' for the custom character placement.

setCompletedStyle($customCharacter);

setCompletedStyle($styleText, $customCharacter);

$bar->setCompletedStyle("<cyan>%s</cyan>", "#");

On progress bar

This bar is located between completed and waiting bar. The usage is similar with above.

$bar->setProgressStyle("\xF0\x9F\x8D\xBA");

Waiting bar

This bar is located after the progress bar or after the completed bar.
Usually it's filled with empty space.

$bar->setWaitingStyle(" ");

Customize properties

These property is defined by default and can be edited if you prefer.

Property Description Default
endOfLine End of line character "\n"
barLength Bar's default character length 26
manual If you set this to true, calling $bar->update will return the bar itself false

Example:

$bar->barLength = 30;

Let's play around

\Scarlets\Console::customStyle([
  'r1'=>[
    'replacement'=>["","","","","","","","","","",""]
  ],
  'r2'=>[
    'replacement'=>["","","","","","","","","","",""]
  ],
]);

$bar = new \Scarlets\Console\ProgressBar();

// It's better to hide the caret/cursor
\Scarlets\Console::hideCursor();

$bar->setWaitingStyle("<r1/>");
$bar->setProgressStyle("🍎"); // Try it with apple on your try
$bar->setCompletedStyle('<cyan>%s</cyan>', "<r2/>");

$index = $bar->add('Yahoo', 100);
for($i=0; $i <= 100; $i++){
    $bar->update($index, $i, "$i/100", rand(100, 200).' KB/s');
}
\Scarlets\Console::showCursor();

1565461199224