10000 bug #20372 [Console] simplified code (fabpot) · symfony/symfony@779a6df · GitHub
[go: up one dir, main page]

Skip to content

Commit 779a6df

Browse files
committed
bug #20372 [Console] simplified code (fabpot)
This PR was merged into the 2.8 branch. Discussion ---------- [Console] simplified code | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | ~no~ yes (even if the output is "better" with this PR) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Instead of adding blank character for shorter lines and having to keep the max length of messages, I propose to clear the line via an ANSI sequence like done in the progress 8000 bar. Code is cleaner and output is better as currently, then cursor can be "away" from the last character which is unexpected. Actually, this is a bug fix as the current code does not work well when using styles in the indicator message. Commits ------- 0aca9bf [Console] simplified code
2 parents d12f269 + 0aca9bf commit 779a6df

File tree

2 files changed

+6
-23
lines changed

2 files changed

+6
-23
lines changed

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class ProgressIndicator
2828
private $indicatorCurrent;
2929
private $indicatorChangeInterval;
3030
private $indicatorUpdateTime;
31-
private $lastMessagesLength;
3231
private $started = false;
3332

3433
private static $formatters;
@@ -125,7 +124,6 @@ public function start($message)
125124

126125
$this->message = $message;
127126
$this->started = true;
128-
$this->lastMessagesLength = 0;
129127
$this->startTime = time();
130128
$this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
131129
$this->indicatorCurrent = 0;
@@ -262,27 +260,12 @@ private function determineBestFormat()
262260
*/
263261
private function overwrite($message)
264262
{
265-
// append whitespace to match the line's length
266-
if (null !== $this->lastMessagesLength) {
267-
if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $message)) {
268-
$message = str_pad($message, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
269-
}
270-
}
271-
272263
if ($this->output->isDecorated()) {
273-
$this->output->write("\x0D");
264+
$this->output->write("\x0D\x1B[2K");
274265
$this->output->write($message);
275266
} else {
276267
$this->output->writeln($message);
277268
}
278-
279-
$this->lastMessagesLength = 0;
280-
281-
$len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $message);
282-
283-
if ($len > $this->lastMessagesLength) {
284-
$this->lastMessagesLength = $len;
285-
}
286269
}
287270

288271
private function getCurrentTimeInMilliseconds()

src/Symfony/Component/Console/Tests/Helper/ProgressIndicatorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function testDefaultIndicator()
4444
$this->generateOutput(' \\ Starting...').
4545
$this->generateOutput(' \\ Advancing...').
4646
$this->generateOutput(' | Advancing...').
47-
$this->generateOutput(' | Done... ').
47+
$this->generateOutput(' | Done...').
4848
PHP_EOL.
4949
$this->generateOutput(' - Starting Again...').
5050
$this->generateOutput(' \\ Starting Again...').
51-
$this->generateOutput(' \\ Done Again... ').
51+
$this->generateOutput(' \\ Done Again...').
5252
PHP_EOL,
5353
stream_get_contents($output->getStream())
5454
);
@@ -70,8 +70,8 @@ public function testNonDecoratedOutput()
7070

7171
$this->assertEquals(
7272
' Starting...'.PHP_EOL.
73-
' Midway... '.PHP_EOL.
74-
' Done... '.PHP_EOL.PHP_EOL,
73+
' Midway...'.PHP_EOL.
74+
' Done...'.PHP_EOL.PHP_EOL,
7575
stream_get_contents($output->getStream())
7676
);
7777
}
@@ -177,6 +177,6 @@ protected function generateOutput($expected)
177177
{
178178
$count = substr_count($expected, "\n");
179179

180-
return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected;
180+
return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
181181
}
182182
}

0 commit comments

Comments
 (0)
0