diff --git a/examples/progress-step-format.php b/examples/progress-step-format.php new file mode 100644 index 0000000..a79965d --- /dev/null +++ b/examples/progress-step-format.php @@ -0,0 +1,60 @@ +tick(); + usleep(100000); +} +$progress->finish(); + +echo "\n"; + +// Example 2: Step-based format (current/total) +echo "Example 2: Step-based format (current/total)\n"; +$progress = new \cli\progress\Bar( + 'Step format', + 10, + 100, + '{:msg} {:current}/{:total} [' // Custom formatMessage with current/total +); +for ($i = 0; $i < 10; $i++) { + $progress->tick(); + usleep(100000); +} +$progress->finish(); + +echo "\n"; + +// Example 3: Custom format combining steps and percentage +echo "Example 3: Custom format combining steps and percentage\n"; +$progress = new \cli\progress\Bar( + 'Mixed format', + 50, + 100, + '{:msg} {:current}/{:total} ({:percent}%) [' // Both current/total and percent +); +for ($i = 0; $i < 50; $i++) { + $progress->tick(); + usleep(50000); +} +$progress->finish(); + +echo "\n"; + +// Example 4: Large numbers with step format +echo "Example 4: Large numbers with step format\n"; +$progress = new \cli\progress\Bar( + 'Processing items', + 1000, + 100, + '{:msg} {:current}/{:total} [' +); +for ($i = 0; $i < 1000; $i += 50) { + $progress->tick(50); + usleep(20000); +} +$progress->finish(); diff --git a/lib/cli/progress/Bar.php b/lib/cli/progress/Bar.php index e800509..43f21b5 100644 --- a/lib/cli/progress/Bar.php +++ b/lib/cli/progress/Bar.php @@ -31,6 +31,30 @@ class Bar extends Progress { protected $_formatTiming = '] {:elapsed} / {:estimated}'; protected $_format = '{:msg}{:bar}{:timing}'; + /** + * Instantiates a Progress Bar. + * + * @param string $msg The text to display next to the Notifier. + * @param int $total The total number of ticks we will be performing. + * @param int $interval The interval in milliseconds between updates. + * @param string|null $formatMessage Optional format string for the message portion. + * @param string|null $formatTiming Optional format string for the timing portion. + * @param string|null $format Optional format string for the overall display. + */ + public function __construct($msg, $total, $interval = 100, $formatMessage = null, $formatTiming = null, $format = null) { + parent::__construct($msg, $total, $interval); + + if ($formatMessage !== null) { + $this->_formatMessage = $formatMessage; + } + if ($formatTiming !== null) { + $this->_formatTiming = $formatTiming; + } + if ($format !== null) { + $this->_format = $format; + } + } + /** * Prints the progress bar to the screen with percent complete, elapsed time * and estimated total time. @@ -49,11 +73,13 @@ public function display($finish = false) { $percent = str_pad(floor($_percent * 100), 3); $msg = $this->_message; - $msg = Streams::render($this->_formatMessage, compact('msg', 'percent')); + $current = $this->current(); + $total = $this->total(); + $msg = Streams::render($this->_formatMessage, compact('msg', 'percent', 'current', 'total')); $estimated = $this->formatTime($this->estimated()); $elapsed = str_pad($this->formatTime($this->elapsed()), strlen($estimated)); - $timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated')); + $timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated', 'current', 'total', 'percent')); $size = Shell::columns(); $size -= strlen($msg . $timing); @@ -65,7 +91,7 @@ public function display($finish = false) { // substr is needed to trim off the bar cap at 100% $bar = substr(str_pad($bar, $size, ' '), 0, $size); - Streams::out($this->_format, compact('msg', 'bar', 'timing')); + Streams::out($this->_format, compact('msg', 'bar', 'timing', 'current', 'total', 'percent')); } /**