5 * Wrapper around \Symfony\Component\Console\Helper\ProgressBar
7 class ProgressIndicator
12 * @var \Symfony\Component\Console\Helper\ProgressBar
14 protected $progressBar;
17 * @var \Symfony\Component\Console\Output\OutputInterface
24 protected $progressIndicatorRunning = false;
29 protected $autoDisplayInterval = 0;
34 protected $cachedSteps = 0;
39 protected $totalSteps = 0;
44 protected $progressBarDisplayed = false;
47 * @var \Robo\Contract\TaskInterface
52 * @param \Symfony\Component\Console\Helper\ProgressBar $progressBar
53 * @param \Symfony\Component\Console\Output\OutputInterface $output
55 public function __construct($progressBar, \Symfony\Component\Console\Output\OutputInterface $output)
57 $this->progressBar = $progressBar;
58 $this->output = $output;
62 * @param int $interval
64 public function setProgressBarAutoDisplayInterval($interval)
66 if ($this->progressIndicatorRunning) {
69 $this->autoDisplayInterval = $interval;
75 public function hideProgressIndicator()
77 $result = $this->progressBarDisplayed;
78 if ($this->progressIndicatorRunning && $this->progressBarDisplayed) {
79 $this->progressBar->clear();
80 // Hack: progress indicator does not reset cursor to beginning of line on 'clear'
81 $this->output->write("\x0D");
82 $this->progressBarDisplayed = false;
87 public function showProgressIndicator()
89 if ($this->progressIndicatorRunning && !$this->progressBarDisplayed && isset($this->progressBar)) {
90 $this->progressBar->display();
91 $this->progressBarDisplayed = true;
92 $this->advanceProgressIndicatorCachedSteps();
97 * @param bool $visible
99 public function restoreProgressIndicator($visible)
102 $this->showProgressIndicator();
107 * @param int $totalSteps
108 * @param \Robo\Contract\TaskInterface $owner
110 public function startProgressIndicator($totalSteps, $owner)
112 if (!isset($this->progressBar)) {
116 $this->progressIndicatorRunning = true;
117 if (!isset($this->owner)) {
118 $this->owner = $owner;
120 $this->totalSteps = $totalSteps;
121 $this->autoShowProgressIndicator();
125 public function autoShowProgressIndicator()
127 if (($this->autoDisplayInterval < 0) || !isset($this->progressBar) || !$this->output->isDecorated()) {
130 if ($this->autoDisplayInterval <= $this->getExecutionTime()) {
131 $this->autoDisplayInterval = -1;
132 $this->progressBar->start($this->totalSteps);
133 $this->showProgressIndicator();
140 public function inProgress()
142 return $this->progressIndicatorRunning;
146 * @param \Robo\Contract\TaskInterface $owner
148 public function stopProgressIndicator($owner)
150 if ($this->progressIndicatorRunning && ($this->owner === $owner)) {
155 protected function cleanup()
157 $this->progressIndicatorRunning = false;
159 if ($this->progressBarDisplayed) {
160 $this->progressBar->finish();
161 // Hack: progress indicator does not always finish cleanly
162 $this->output->writeln('');
163 $this->progressBarDisplayed = false;
169 * Erase progress indicator and ensure it never returns. Used
170 * only during error handlers or to permanently remove the progress bar.
172 public function disableProgressIndicator()
175 // ProgressIndicator is shared, so this permanently removes
176 // the program's ability to display progress bars.
177 $this->progressBar = null;
183 public function advanceProgressIndicator($steps = 1)
185 $this->cachedSteps += $steps;
186 if ($this->progressIndicatorRunning) {
187 $this->autoShowProgressIndicator();
188 // We only want to call `advance` if the progress bar is visible,
189 // because it always displays itself when it is advanced.
190 if ($this->progressBarDisplayed) {
191 return $this->advanceProgressIndicatorCachedSteps();
196 protected function advanceProgressIndicatorCachedSteps()
198 $this->progressBar->advance($this->cachedSteps);
199 $this->cachedSteps = 0;