10000 Simplification, fix bug in ProgressBar, improve comments and naming · pierredup/symfony@327abc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 327abc3

Browse files
committed
Simplification, fix bug in ProgressBar, improve comments and naming
1 parent 6425192 commit 327abc3

File tree

4 files changed

+42
-117
lines changed

4 files changed

+42
-117
lines changed

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ private function overwrite($message)
473473
if ($this->overwrite) {
474474
if (!$this->firstRun) {
475475
if ($this->output instanceof ConsoleSectionOutput) {
476-
$this->output->clear(1);
476+
$this->output->clear($this->formatLineCount + 1);
477477
} else {
478478
// Move the cursor to the beginning of the line
479479
$this->output->write("\x0D");

src/Symfony/Component/Console/Output/ConsoleOutput.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
3434
*/
3535
private $stderr;
3636

37-
private $outputReference;
37+
private $consoleSectionOutputs = [];
3838

3939
/**
4040
* Constructor.
@@ -53,8 +53,6 @@ public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = nu
5353
if (null === $decorated) {
5454
$this->setDecorated($actualDecorated && $this->stderr->isDecorated());
5555
}
56-
57-
$this->outputReference = new OutputSectionReference();
5856
}
5957

6058
/**
@@ -64,7 +62,7 @@ public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = nu
6462
*/
6563
public function section()
6664
{
67-
return new ConsoleSectionOutput($this->getStream(), $this->outputReference, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
65+
return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
6866
}
6967

7068
/**

src/Symfony/Component/Console/Output/ConsoleSectionOutput.php

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717

1818
/**
1919
* @author Pierre du Plessis <pdples@gmail.com>
20+
* @author Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
2021
*/
2122
class ConsoleSectionOutput extends StreamOutput
2223
{
23-
public $content;
24+
public $content = '';
2425
public $lines = 0;
2526

26-
private $sectionReference;
27+
private $sections;
2728
private $terminal;
2829

29-
public function __construct($stream, OutputSectionReference $sectionReference, $verbosity, $decorated, OutputFormatterInterface $formatter)
30+
public function __construct($stream, array &$sections, $verbosity, $decorated, OutputFormatterInterface $formatter)
3031
{
3132
parent::__construct($stream, $verbosity, $decorated, $formatter);
32-
$this->sectionReference = $sectionReference;
33-
$this->sectionReference->addRef($this);
33+
array_unshift($sections, $this);
34+
$this->sections = &$sections;
3435
$this->terminal = new Terminal();
3536
}
3637

@@ -45,20 +46,14 @@ public function clear($lines = null)
4546
return;
4647
}
4748

48-
$number = $lines ?: $this->lines;
49-
50-
$buffer = $this->sectionReference->calculateBuffer($this);
51-
$buffer += $number;
49+
if (!$lines) {
50+
$lines = $this->lines;
51+
}
5252

5353
$this->content = '';
54-
$this->lines = $this->lines - $number;
54+
$this->lines -= $lines;
5555

56-
if ($buffer > 0) {
57-
$this->moveUp($buffer);
58-
$this->clearOutput();
59-
}
60-
61-
$this->printOutput();
56+
parent::doWrite($this->popStreamContentUntilCurrentSection($lines), false);
6257
}
6358

6459
/**
@@ -81,57 +76,46 @@ protected function doWrite($message, $newline)
8176
return parent::doWrite($message, $newline);
8277
}
8378

84-
$newline = true;
85-
$buffer = $this->sectionReference->calculateBuffer($this);
79+
$erasedContent = $this->popStreamContentUntilCurrentSection();
8680

87-
if ($buffer > 0) {
88-
$this->moveUp($buffer);
89-
$this->clearOutput();
81+
foreach (explode(PHP_EOL, $message) as $lineContent) {
82+
$lineLength = Helper::strlenWithoutDecoration($this->getFormatter(), $lineContent);
83+
// calculates to how many lines does the line wrap to
84+
$this->lines += $lineLength ? ceil($lineLength / $this->terminal->getWidth()) : 1;
9085
}
9186

92-
$this->calculateLines($message, $newline);
93-
94-
parent::doWrite($message, $newline);
87+
$this->content .= $message.PHP_EOL;
9588

96-
$this->printOutput();
89+
parent::doWrite($message, true);
90+
parent::doWrite($erasedContent, false);
9791
}
9892

99-
private function calculateLines($messages, $newLine)
93+
/**
94+
* At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits
95+
* current section. Then it erases content it crawled through. Optionally, it erases part of current section too.
96+
*
97+
* @param int $numberOfLinesToClearFromCurrentSection
98+
* @return string
99+
*/
100+
private function popStreamContentUntilCurrentSection($numberOfLinesToClearFromCurrentSection = 0)
100101
{
101-
$total = 0;
102+
$numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection;
103+
$erasedContent = [];
102104

103-
$prevLines = explode("\n", $messages);
105+
foreach ($this->sections as $section) {
106+
if ($section === $this) {
107+
break;
108+
}
104109

105-
for ($i = 0; $i < count($prevLines); ++$i) {
106-
$total += ceil(Helper::strlen($prevLines[$i]) / $this->terminal->getWidth()) ?: 1;
110+
$numberOfLinesToClear += $section->lines;
111+
$erasedContent[] = $section->content;
107112
}
108113

109-
$this->lines += $total;
110-
$this->content .= $messages;
111-
112-
if ($newLine) {
113-
$this->content .= "\n";
114+
if ($numberOfLinesToClear > 0) {
115+
// Move cursor up n lines and erase to end of screen
116+
parent::doWrite(sprintf("\033[%dA\033[0J", $numberOfLinesToClear), false);
114117
}
115118

116-
return $total;
117-
}
118-
119-
private function moveUp($n)
120-
{
121-
parent::doWrite(sprintf("\033[%dA", $n), false);
122-
}
123-
124-
private function clearOutput()
125-
{
126-
parent::doWrite("\033[0J", false);
127-
}
128-
129-
private function printOutput()
130-
{
131-
$output = $this->sectionReference->getOutput($this);
132-
133-
if (!empty($output)) {
134-
parent::doWrite($output, false);
135-
}
119+
return implode('', array_reverse($erasedContent));
136120
}
137121
}

src/Symfony/Component/Console/Output/OutputSectionReference.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0